Chapter 2 Objects and Classes

Download Chapter 2 Objects and Classes

Post on 03-Oct-2015

226 views

Category:

Documents

8 download

DESCRIPTION

Objects and Classes

TRANSCRIPT

<ul><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>Class and ObjectQ1.What is class? How they are defined ?Describe themAns. A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use. When defining a class, we are a new abstract data type that can be treated like any other built-in data type. Generally, a class specification has two parts:</p><p>1. Class declaration2. Class function definitions</p><p>The class declaration describes the type and scope of its members. The class functions definitions describe below the class functions are implemented.The general form of a class declaration is:</p><p>class class_name{</p><p>private:variable declarations; //data membersfunction declarations; //member function</p><p>public:variable declarations; //data memberfunction declaration; // member Function</p><p>} ;</p><p>The class declaration is similar to a struct declaration. The keyword class specifies, that what follows is an abstract data of type class_name. The body of a class is enclosed within braces and terminated by a semicolon. The class body contains the declaration of variables and functions. These functions and variables are collectively called class members. They are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public. The keywords private and public are known as visibility labels. These keywords are followed by a colon.</p><p>The class members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside the class also. The data hiding (using private declaration) is the key feature of object-oriented programming. The use of the keyword private is optional. By default, the members of a class are private. If both the labels are missing, then, by default, all the </p><p>22Object and Classes</p><p>SyllabusSpecifying a class, Defining member functions, Arrays within a class, Creating objects, memory allocation for objects, static data &amp; member function, Arrays of objects, objects as function argument.</p><p>1</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>members are private. Such a class is completely hidden from the outside world and does not serve any purpose.</p><p>The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can have access to the private data members and private functions. However, the public members (both functions and data) can be accessed from outside the class. </p><p>Q2.What are the different ways member function can be defined?Ans. Member functions can be defined in two places:</p><p> Outside the class definition. Inside the class definition.</p><p>Irrespective of the place of definition, the function should perform same task. Therefore, the code for the function body would be identical in both the cases. However, there is a subtle difference in the way the function header is defined. </p><p>Q3.What is scope resolution operator ?state its functionAns. The symbol :: is called the Scope Resolution Operator. </p><p>The scope resolution operator (::) in C++ is used to define the already declared member functions (in the header file with the .cpp or the .h extension) of a particular class. In the .cpp file one can define the usual global functions or the member functions of the class. </p><p>To differentiate between the normal functions and the member functions of the class, one needs to use the scope resolution operator (::) in between the class name and the member function name i.e. ship::foo() where ship is a class and foo() is a member function of the class ship. </p><p>The other uses of the resolution operator is to resolve the scope of a variable when the same identifier is used to represent a global variable, a local variable, and members of one or more class(es). If the resolution operator is placed between the class name and the data member belonging to the class then the data name belonging to the particular class is referenced. </p><p>If the resolution operator is placed in front of the variable name then the global variable is referenced. When no resolution operator is placed then the local variable is referenced.Example</p><p>#include int n = 12; // A global variableint main() { int n = 13; // A local variable cout </p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>1. private and 2. public </p><p>To denote which of the members are private and which of them are public. The keywords private and public are known as visibility labels. These keywords are followed by a colon.</p><p>PrivateThe class members that have been declared as private can be accessed only from </p><p>within the class. Public</p><p>public members can be accessed from outside the class also. </p><p>As described above private data members of the class can be accessed through the public member functions only of the same class.</p><p>Object_name.function_name(actual_arguments)Is the syntax of accessing the data member of the class.</p><p>Following example is used to demonstrate the same#include#includeclass rectangle{ private : int len, br; public: void area_peri( )</p><p>{int a, p;a = len * br;p = 2 * (len +br);cout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>}};</p><p>//Main Program.. void main( ){</p><p>rectangle r1,r2,r3; //define 3 objects of class rectangler1.setdata(10,20); //set data in elements of the objectr1.displaydata( ); //display the data set by setdata( )r1.area_peri( ); // calculates and print area and Perimeterr2.setdata(5,8); r2.displaydata( ); r2.area_peri( ); r3.getdata( ); //receive data from keyboard r3.displaydata( ); r3.area_peri( ); </p><p>}</p><p>Q5.What do you mean by the term data member and member Function?Ans. The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can have access to the private data members and private functions. However, the public members (both functions and data) can be accessed from outside the class. Example</p><p>class class_name{</p><p>private:variable declarations; //data membersfunction declarations; //member function</p><p>public:variable declarations; //data memberfunction declaration; // member Function</p><p>} ;</p><p>4</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>Q6.List some of the header files used in C++?Ans.</p><p>Q7.Differentiate between object and classesAns.Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects. </p><p>1. A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change. </p><p>2. The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed. </p><p>3. An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change. </p><p>SrNo Classes Object1 Class is template or Object is software construct which binds </p><p>5</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>blueprint , its states how object should be and Behave</p><p>data and logic or methods /functions together</p><p>2 Class is user defined data type</p><p>Object is run time entity of class</p><p>3. Class cannot be passed as argument or parameter </p><p>Object can be passed as argument or parameter</p><p>4. Class is group of a object that share common property</p><p>Object is real time entity of class</p><p>5. Class can define object Object cannot define class6. Example</p><p>Class Demo { int num; public void getData(int x) { num = x ; } public void display() { cout </p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>5.Supports both overloaded functionsas well as overrided functions.</p><p>5.Supports only overloaded functions. </p><p>Q10.State the necessity of preprocessor directive? State importance of #include?Ans. A C++ (or C) compiler begins by invoking the preprocessor, a program that uses special statements, known as directives or control statements, that cause special compiler actions such as:</p><p> file inclusion, in which the file being preprocessed incorporates the contents of another file, exactly as if the included files text were actually part of the including file;</p><p> macro substitution, in which one sequence of text is replaced by another; conditional compilation, in which parts of the source files code can be eliminated </p><p>at compile time under certain circumstances.All preprocessor directives begin with the # symbol (known as pound or hash), which </p><p>must occur in the leftmost column of the line. A preprocessor directive that takes up more than one line needs a continuation symbol, n (backslash), as the very last character of every line except the last.</p><p>Following directive #include is used in the program. This directive causes the preprocessor to add the content of the iostream file to the program. It contains declaration for the identifier cout and the operator </p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>private :int num; //Data Member</p><p>public :void getNumber() // Member Function inside Class</p><p>{cout num;</p><p>}void dispData()</p><p>{cout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>Cout m &gt;&gt; n;</p><p>}void set :: display (void)</p><p>{cout </p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>my_array[8] = 100; my_array[9] = 100;</p><p>Luckily the number appearing between the brackets doesn't have to be a numeric literal. It can be a valid identifier that represents an integer value. This could be either a variable, a constant, or an expression that evaluates to an integer value. For example, a faster way of initializing all the elements of my_array to 100 would be to declare an integer variable, set its value to 0, use it to index my_array, then increment the variable by one and repeat the process until all the elements were set to 100. The following C++ code will do exactly that: </p><p>for(int i = 0; i</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>for( i = SIZE - 1; i &gt;= 0 ; i--){</p><p>cout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p> cout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p> void main ( ){</p><p> int i , j ; char letters[6][1] = { {'M'},{'i'},{'c'},{'k'},{'e'},{'y'} } ; cout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>for (i=0 ; i</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>{temp = Arr[j];Arr[j] = Arr[j+1];Arr[j+1] = temp;</p><p>}</p><p>}}</p><p>//Displaying the content of the arraycout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>}</p><p>//Displaying the content of the arraycout</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>number_of_year = noy;}</p><p>void calculate(){sint = )principle_Amount * rate_of_interest * number_of_year)}</p><p>void display(){</p><p> cout </p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>user defined functions such As vectors, date and time. Program object should be chosen such that they match closely the real World object eg. Elements of computer user environment are windows, menus and graphics, objects.</p><p>The physical objects are automobile in graphics stimulation, electronic component in circuit designing. The match between programming objects and a real world objects is a good resulting Objects offering the revolution in programming.For example : students objects includes the data members and functions as given:</p><p>Objects : studentData :</p><p>Roll-on NameDate of birth </p><p>Functions :Total( ) Average( )Percentage( )</p><p>This variable is known as object. This variable will be the type of class declared. To create at object you need to write</p><p>Classname objectname;</p><p>in the function main()</p><p>After creating objectname object, all data members are member functions declared in class gets initialised and member functions can be called with the statement </p><p>objectname.function();</p><p>For Example this is class Class student</p><p>{ private :int roll;int mark1, mark2;float average;</p><p>public:void getData()</p><p>{ ---------- }void calculate()</p><p>{------------}void displayData()</p><p>{------------}}</p><p>Declaration of ObjectStudent S1 , S2[10] , *S3 ;S1 is object of class student.S2[100] is array of 10 object of class student.S3 is pointer to an object of class student.</p><p>18</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>Another way of Declaring ObjectObject can also be declared or defined by just placing their names immediately </p><p>after closing brace of class ie in the following wayClass employee</p><p>{------------------------------</p><p>} e1 , e2[10] , *e3;</p><p>Q27.List and describe the access specifiers used in OOPAns.The access specifiers used in OOP are as follows </p><p>1. public2. private3. protected</p><p>Private The members are accessible only by the member functions or friend functions. </p><p>Protected These members are accessible by the member functions of the class and the classes which are derived from this class. </p><p>Public Accessible by any external member.</p><p>Access control helps prevent you from using objects in ways they were not intended to be used. This protection is lost when explicit type conversions (casts) are performed.The default access to class members (members of a class type declared using the class keyword) is private; the default access to struct and union members is public. For either case, the current access level can be changed using the public, private, or protected. </p><p>Access Specifier in More detailsPrivate</p><p> Can be data or method members Are accessible ONLY through other methods of the same class where they are </p><p>declared Cannot be accessed through a class instance Will not be inherited</p><p>Public Can be data or method members Are accessible globally from any function/method outside the class they are </p><p>declared, across the application Are accessible ONLY through a valid instance of the class in which they are </p><p>declared</p><p>19</p></li><li><p>Prof.Manoj S. Kavedia (9860174297) (urallalone@yahoo.com)</p><p>Protected Can be data and method members Exactly same as private members for all practical purposes, except for the </p><p>inheritance part These members are inherited by a child of the class in which they are declared Their behaviour in the child class depends on how the inheritance has been </p><p>defined If the inheritance is private, these are private in the child class, if it is public, </p><p>these are public in the child class, and if the inheritance in protected, these are protected in the child class thus allowing further inheritance.</p><p>Nesting of Member Function</p><p>Q28.What is Nesting of Mem...</p></li></ul>