C STRUCTURES REVISITED • One of the unique facilities provided by C language is structure • It is used to group logically related data items • It is user defined data-type and once structure type is defined, variables of that type can be created 2
LIMITATIONS OF C STRUCTURE • They don’t allow data hiding • Structure members can be accessed using structure variable by any function any where in the scope • C does not allow the structure data type to be treated like built-in data type 3
AN INTRODUCTION TO CLASSES • A class is a building block of OOP. It is the way to bind the data and its logically related functions together. • An abstract data type that can be treated like any other built in data type.
CLASS DEFINITION Class head class name_of_class. Class body { data members; member functions; };
SPECIFYING A CLASS Specification of a class consists of two parts: • Class declaration • Function definition Syntax: class class_name { private: variable declarations function declarations public: variable declarations function declarations }; 6
CONTD… The key feature of OOP is data hiding. Generally, data within a class is made private and the functions are public. So, the data will be safe from accidental manipulations, while the functions can be accessed from outside the class However, it is not necessary that the data must be private and functions public 7
CREATING OBJECTS Once a class has been declared, variables of that type can be created by using the class name as data-type Test t1; //memory for t1 is allocated This statement creates a variable t1 of type test. The class variables are known as objects t1 is called object of class test. More than one object of a class can be created 8
CONTD… Objects are also called instances of class Objects can be created as follows: class employee { int id; char name[20]; public: void getname(); void putname(); }e1, e2, e3; 9
MEMBER FUNCTION Member function’s name is visible outside the class. It can be defined inside or outside the class. It can have access to private, public and protected data members of its class, but cannot access private data members of another class.
ACCESSING CLASS MEMBERS The class members are accessed using dot operator However it works only for the public members The dot operator is called member access operator The general format is: class-object.class-member; Eg: e1.getdata(); The private data and functions of a class can be accessed only through the member functions of that class 11
DEFINING MEMBER FUNCTIONS Member function can be defined in two ways Outside the class Inside the class The code for the function body would be identical in both the cases i.e perform same task irrespective of the place of definition 12
OUTSIDE THE CLASS In this approach, the member functions are only declared inside the class, whereas its definition is written outside the class General form: Return-type class-name::function-name(argument-list) { -------------- -------------- // function body -------------- } 13
CONTD… Eg: class A { int x, y; public: void getdata (); // function declaration inside the class } void A :: getdata() { cin>>x>>y; // function body } 14
INSIDE THE CLASS Function body can be included in the class itself by replacing function declaration by function definition If it is done, the function is treated as an inline function Hence, all the restrictions that apply to inline function, will also apply here 15
Eg: class A { int a, b; public: void getdata() { cin>>a>>b; } }; Here, getdata() is defined inside the class. So, it will act like an inline function A function defined outside the class can also be made ‘inline’ by using the qualifier ‘inline’ in the header line of a function definition 16
NESTED MEMBER FUNCTIONS An object of the class using dot operator, generally, calls a member function of a class However, a member function can be called by using its name inside another member function of the same class. This is known as “Nesting Of Member Functions” 17
PRIVATE MEMBER FUNCTIONS Member functions are in general made public But in some cases, we may need to make a function a private to hide them from outside world Private member functions can only be called by another function that is a member of its class Objects of the class cannot invoke it using dot operator 18
Eg: class A { int a, b; void read(); //private member function public: void update(); void write(); }; void A :: update() { read(); // called from update() function. no object used } If a1 is an object of A, then the following statement is not valid a1.read(); This is because, read is a private member function which cannot be called using object and dot operator 19
STATIC MEMBER FUNCTION In a class, functions can also be declared as static Properties of static functions are: • They can access only other STATIC members(functions or variables) declared in the same class • They can be called using class name ( instead of its object) • Eg: class_name::function_name 20
class A { int no; static int count; //static member public: void set_no() { count++; no=count; } void put_no() { cout<<“ No is:” <<no; } static void put_count() //static member function accessing static member { cout<<endl<<“Count:” <<count; } }; Int A::count; main() { A a1, a2; a1.set_no(); a2.set_no(); A::put_count(); a1.set_no(); a2.set_no(); A::put_count(); a1.put_no(); a2.put_no(); 21
ARRAY OF OBJECTS Array can be created of any datatype Since a class is also a user defined data-type, array of objects can be created Eg: a class Employee is specified. If we have to keep records of 20 employees in an organization having two departments, then instead of creating 20 separate variables, we can create array of objects as follows: Employee dept1[10]; Employee dept2[10]; 22
MEMORY ALLOCATION FOR OBJECTS Memory space for object is allocated when they are declared and not when the class is specified The member functions are created and placed in the memory only once, when they are defined as a part of a class specification All the objects belonging to the particular class will use same member functions when objects are created However, the data members will hold different values for different object, so, space for data member is allocated separately for each object 23
OBJECTS AS FUNCTION ARGUMENTS Like any other variable, objects can also be passed to the function, as an argument There are two ways of doing this • Pass by value • Pass by reference In the pass by value, the copy of the object is passed to the function So, the changes made to the object inside the function do not affect the actual object 24
Eg: class height { int feet, inches; public: void get_height() { cout<<“Enter height in feet and inches”; cin>>feet>>inches; } void put_height() { cout<<“Feet:”<<feet; cout<<and inches:”<<inches; } void sum(height, height); }; void height:: sum(height h1, height h2) { inches= h1.inches+h2.inches; feet=inches/12; inches=inches%12; feet=feet+h1.feet+h2.feet; } int main() { height h1, h2, h3; h1.get_height(); h2.get_height(); h3.sum(h1, h2); cout<<“Height 1: ” ; h1.put_height(); cout<<“Height 2: ” ; h2.put_height(); cout<<“Height 3: ” ; h3.put_height(); return(0); } 25
FRIEND FUNCTIONS AND FRIEND CLASSES friend declarations • To declare a friend function • Type friend before the function prototype in the class that is giving friendship friend int myFunction( int x ); should appear in the class giving friendship • To declare a friend class • Type friend class Classname in the class that is giving friendship • if ClassOne is granting friendship to ClassTwo, friend class ClassTwo; • should appear in ClassOne's definition
1 // Fig. 7.5: fig07_05.cpp 2 // Friends can access private members of a class. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // Modified Count class 9 class Count { 10 friend void setX( Count &, int ); // friend declaration 11 public: 12 Count() { x = 0; } // constructor 13 void print() const { cout << x << endl; } // output 14 private: 15 int x; // data member 16 }; 17 18 // Can modify private data of Count because 19 // setX is declared as a friend function of Count 20 void setX( Count &c, int val ) 21 { 22 c.x = val; // legal: setX is a friend of Count 23 } 24 25 int main() 26 { 27 Count counter; 28 29 cout << "counter.x after instantiation: "; 30 counter.print(); Changing private variables allowed.
31 cout << "counter.x after call to setX friend function: "; 32 setX( counter, 8 ); // set x with a friend 33 counter.print(); 34 return 0; 35 } counter.x after instantiation: 0 counter.x after call to setX friend function: 8

Object and class presentation

  • 2.
    C STRUCTURES REVISITED • Oneof the unique facilities provided by C language is structure • It is used to group logically related data items • It is user defined data-type and once structure type is defined, variables of that type can be created 2
  • 3.
    LIMITATIONS OF C STRUCTURE •They don’t allow data hiding • Structure members can be accessed using structure variable by any function any where in the scope • C does not allow the structure data type to be treated like built-in data type 3
  • 4.
    AN INTRODUCTION TOCLASSES • A class is a building block of OOP. It is the way to bind the data and its logically related functions together. • An abstract data type that can be treated like any other built in data type.
  • 5.
    CLASS DEFINITION Class headclass name_of_class. Class body { data members; member functions; };
  • 6.
    SPECIFYING A CLASS Specificationof a class consists of two parts: • Class declaration • Function definition Syntax: class class_name { private: variable declarations function declarations public: variable declarations function declarations }; 6
  • 7.
    CONTD… The key featureof OOP is data hiding. Generally, data within a class is made private and the functions are public. So, the data will be safe from accidental manipulations, while the functions can be accessed from outside the class However, it is not necessary that the data must be private and functions public 7
  • 8.
    CREATING OBJECTS Once aclass has been declared, variables of that type can be created by using the class name as data-type Test t1; //memory for t1 is allocated This statement creates a variable t1 of type test. The class variables are known as objects t1 is called object of class test. More than one object of a class can be created 8
  • 9.
    CONTD… Objects are alsocalled instances of class Objects can be created as follows: class employee { int id; char name[20]; public: void getname(); void putname(); }e1, e2, e3; 9
  • 10.
    MEMBER FUNCTION Member function’sname is visible outside the class. It can be defined inside or outside the class. It can have access to private, public and protected data members of its class, but cannot access private data members of another class.
  • 11.
    ACCESSING CLASS MEMBERS The classmembers are accessed using dot operator However it works only for the public members The dot operator is called member access operator The general format is: class-object.class-member; Eg: e1.getdata(); The private data and functions of a class can be accessed only through the member functions of that class 11
  • 12.
    DEFINING MEMBER FUNCTIONS Member functioncan be defined in two ways Outside the class Inside the class The code for the function body would be identical in both the cases i.e perform same task irrespective of the place of definition 12
  • 13.
    OUTSIDE THE CLASS Inthis approach, the member functions are only declared inside the class, whereas its definition is written outside the class General form: Return-type class-name::function-name(argument-list) { -------------- -------------- // function body -------------- } 13
  • 14.
    CONTD… Eg: class A { int x,y; public: void getdata (); // function declaration inside the class } void A :: getdata() { cin>>x>>y; // function body } 14
  • 15.
    INSIDE THE CLASS Functionbody can be included in the class itself by replacing function declaration by function definition If it is done, the function is treated as an inline function Hence, all the restrictions that apply to inline function, will also apply here 15
  • 16.
    Eg: class A { int a,b; public: void getdata() { cin>>a>>b; } }; Here, getdata() is defined inside the class. So, it will act like an inline function A function defined outside the class can also be made ‘inline’ by using the qualifier ‘inline’ in the header line of a function definition 16
  • 17.
    NESTED MEMBER FUNCTIONS An objectof the class using dot operator, generally, calls a member function of a class However, a member function can be called by using its name inside another member function of the same class. This is known as “Nesting Of Member Functions” 17
  • 18.
    PRIVATE MEMBER FUNCTIONS Member functionsare in general made public But in some cases, we may need to make a function a private to hide them from outside world Private member functions can only be called by another function that is a member of its class Objects of the class cannot invoke it using dot operator 18
  • 19.
    Eg: class A { int a,b; void read(); //private member function public: void update(); void write(); }; void A :: update() { read(); // called from update() function. no object used } If a1 is an object of A, then the following statement is not valid a1.read(); This is because, read is a private member function which cannot be called using object and dot operator 19
  • 20.
    STATIC MEMBER FUNCTION In aclass, functions can also be declared as static Properties of static functions are: • They can access only other STATIC members(functions or variables) declared in the same class • They can be called using class name ( instead of its object) • Eg: class_name::function_name 20
  • 21.
    class A { int no; staticint count; //static member public: void set_no() { count++; no=count; } void put_no() { cout<<“ No is:” <<no; } static void put_count() //static member function accessing static member { cout<<endl<<“Count:” <<count; } }; Int A::count; main() { A a1, a2; a1.set_no(); a2.set_no(); A::put_count(); a1.set_no(); a2.set_no(); A::put_count(); a1.put_no(); a2.put_no(); 21
  • 22.
    ARRAY OF OBJECTS Arraycan be created of any datatype Since a class is also a user defined data-type, array of objects can be created Eg: a class Employee is specified. If we have to keep records of 20 employees in an organization having two departments, then instead of creating 20 separate variables, we can create array of objects as follows: Employee dept1[10]; Employee dept2[10]; 22
  • 23.
    MEMORY ALLOCATION FOR OBJECTS Memoryspace for object is allocated when they are declared and not when the class is specified The member functions are created and placed in the memory only once, when they are defined as a part of a class specification All the objects belonging to the particular class will use same member functions when objects are created However, the data members will hold different values for different object, so, space for data member is allocated separately for each object 23
  • 24.
    OBJECTS AS FUNCTION ARGUMENTS Likeany other variable, objects can also be passed to the function, as an argument There are two ways of doing this • Pass by value • Pass by reference In the pass by value, the copy of the object is passed to the function So, the changes made to the object inside the function do not affect the actual object 24
  • 25.
    Eg: class height { int feet,inches; public: void get_height() { cout<<“Enter height in feet and inches”; cin>>feet>>inches; } void put_height() { cout<<“Feet:”<<feet; cout<<and inches:”<<inches; } void sum(height, height); }; void height:: sum(height h1, height h2) { inches= h1.inches+h2.inches; feet=inches/12; inches=inches%12; feet=feet+h1.feet+h2.feet; } int main() { height h1, h2, h3; h1.get_height(); h2.get_height(); h3.sum(h1, h2); cout<<“Height 1: ” ; h1.put_height(); cout<<“Height 2: ” ; h2.put_height(); cout<<“Height 3: ” ; h3.put_height(); return(0); } 25
  • 26.
    FRIEND FUNCTIONS AND FRIENDCLASSES friend declarations • To declare a friend function • Type friend before the function prototype in the class that is giving friendship friend int myFunction( int x ); should appear in the class giving friendship • To declare a friend class • Type friend class Classname in the class that is giving friendship • if ClassOne is granting friendship to ClassTwo, friend class ClassTwo; • should appear in ClassOne's definition
  • 27.
    1 // Fig.7.5: fig07_05.cpp 2 // Friends can access private members of a class. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 // Modified Count class 9 class Count { 10 friend void setX( Count &, int ); // friend declaration 11 public: 12 Count() { x = 0; } // constructor 13 void print() const { cout << x << endl; } // output 14 private: 15 int x; // data member 16 }; 17 18 // Can modify private data of Count because 19 // setX is declared as a friend function of Count 20 void setX( Count &c, int val ) 21 { 22 c.x = val; // legal: setX is a friend of Count 23 } 24 25 int main() 26 { 27 Count counter; 28 29 cout << "counter.x after instantiation: "; 30 counter.print(); Changing private variables allowed.
  • 28.
    31 cout <<"counter.x after call to setX friend function: "; 32 setX( counter, 8 ); // set x with a friend 33 counter.print(); 34 return 0; 35 } counter.x after instantiation: 0 counter.x after call to setX friend function: 8