1 / 73

INHERITANCE

INHERITANCE. A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE) The existing class is referred to as the Base Class/ Parent Class/ Super Class.

keona
Télécharger la présentation

INHERITANCE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INHERITANCE

  2. A class may de defined to automatically include the data members and member functions of an already existing class. New data members and member functions can be included as well. (INHERITANCE) • The existing class is referred to as the Base Class/ Parent Class/ Super Class. • The new class the is being defined by inheriting from the existing class is referred to as the Derived Class/ Child Class/ Sub Class • Syntax: Class <name of derived class>:<access specifier> <name of base class> {…… // definition of class …. };

  3. Eg: Class B : Public A {// new features of class B }; Diagrammatic representation of Inheritance: A B

  4. class A { int x; public: void setx(const int=0); intgetx() const; }; class B: public A { int y; public: void sety(const int =0); intgety() const; }; void main() { cout<<sizeof(A)<<endl<<sizeof(B); B B1; B1. setx(1); B1.sety(3); cout<<B1.getx(); cout<<endl<<B1.gety(); }

  5. Effects of Inheritance: Inheritance affects the size and behaviour of the derived class in 2 ways: 1. An object of the derived class will contain: Data Members of Derived Class + Data Members of Base Class Thus, An object of the Derived Class is generally larger than an object of the Base Class. Exception???

  6. 2. We can call a member function of the Derived Class using an object of the Derived Class…. The same object can be used to call the public members of the Base Class also. Does not happen in some circumstances!!!! ____________________________________________________ Inheritance implements a is-a relationship. The process of Inheritance is also called SPECIALIZATION. Why??? Main benefit of Inheritance???

  7. Container Class: A class that contains an object of another class or a pointer to a data structure that contains a set of objects of another class. Containership implements a has-a relationship.

  8. Base and Derived Class Objects: • An object of the Derived Class is not at all related to another simultaneously existing object of the base class. 201 101 x x y A1 B1

  9. Accessing Members of the Base Class in the Derived Class • Only Public members of the Base Class can be accessed in the functions of Derived Class. Eg: if definition is: void B::sety() { x=y; } ERROR!!! Private members of the Base Class cannot be accessed. Eg: ‘A:: setx()’ and ‘A::getx()’ can be accessed in the member functions of the Derived Class . Why???

  10. Private members of the Base Class remain Private with respect to the member functions of the Derived Class. Eg: void B::sety(const int q) { y=q; setx(y) } Thus, Data Security is implemented in C++. Inheritance is used to add facilities to an existing class without reprogramming or recompiling it.

  11. class B; class A { friend class B; int x; }; class B { void fB (A *p) { p->x=0; } }; class C : public B { void fC(A *p) { p->x=0; } }; FRIENDSHIP IS NOT INHERITED!

  12. BASE CLASS AND DERIVED CLASS POINTERS • A Base Class Pointer can point to an object of Derived Class. • A Derived Class Pointer cannot point to an object of the Base Class. • A Base Class Pointer can safely point to an object of a Derived Class without the need foe typecasting. (Exceptions will be considered later) • A Derived Class Pointer can be made to point at an object of the Base Class only forcibly by typecasting. (Can result in run-time errors!)

  13. class A { public: int x; }; class B : public A { public : int y; }; void main() { A * Aptr; B B1; Aptr=&B1;// Base class ptr points to derived class object Aptr->x=10;//Access Base class member thru base class ptr Aptr->y=20;//Error! Y not found in class A }

  14. Aptr is of type ‘A *’. It is supposed to point at objects of base class A. Therefore, It cannot access ‘y’. ( There is no member of name ‘y’ in class A). • The fact that Aptr points at an object of class B is of no significance. • Although ‘Aptr ‘ points at ‘B1’(occupies 4 bytes), it is able to access only the value contained in the first 2 bytes. • Thus, Aptr cannot access area in the memory that hasn’t been allocated. A Base class pointer can safely point to an object of the derived class.

  15. 201 x 201 y Aptr Base class pointer points to an object of the derived class B1

  16. void main() { A A1; B * Bptr; Bptr=&A1; // Error!! Cannot convert from B* to A* Bptr->x=10;//ok. Derived class pointer //accesses base class member Bptr->y=20; //ok. Derived class pointer //accesses derived class member }

  17. Bptr is of type ‘B *’. It is supposed to point to objects of derived class B. It can access ‘y’ also. • Bptr is pointing to A1(occupies 2 bytes). There is a member of name ‘y’ in class B. • Thus, Bptr is able to access memory that has not been allocated. A Derived class Pointer cannot safely point to an object of the base class.

  18. 201 x 201 A1 y Bptr Y would be here in case of derived class objects Derived class pointer points to an object of the Base class

  19. A derived class pointer can be made to point at an object of base class by explicit typecasting. void main() { A A1; B * Bptr; // derived class pointer Bptr=(B *)&A1; // Explicit typecasting to make the //derived class pointer point at //base class object } Explicit Address manipulation should be avoided and is dangerous!

  20. Consider the following class definitions: class A { int x; public: void setx(const int =0); // rest of class A }; class B: public A { int y; public: void sety(const int=0); // rest of class B};

  21. void main() { A * Aptr; B B1; Aptr=&B1;// ok Base class ptr points to derived class obj Aptr->setx(10); //ok access base class member thru base //class ptr Aptr->sety(20); //Error! Sety() is not a member of class A }

  22. void main() { A A1; B * Bptr; Bptr=&A1;// Error! Cannot convert A* to B* Bptr->setx(10);//ok derived class pointer accesses base //class member Bptr->sety(20);// ok derived class pointer accesses //derived class member }

  23. Function Overriding • Defining a member function in the derived class with the name and signature matching those of the base class function. • Results in two functions with the same name and signature. ( one in base class + one in derived class)

  24. class A { public: void show() { cout<<“ show function in Class A called”; } }; class B:public A { public: void show() // overriding A::show() { cout<<“ show function in Class B called”; } };

  25. void main() { B B1; B1.show();// B::show() is called A A1; A1.show();// A::show() is called B1.A::show();// calling the overridden function forcibly //with respect to object of the derived class. }

  26. Whenever a function is called with respect to an object, the compiler first searches for the function prototype in the same class. • Only if the search fails, the compiler goes up the class hierarchy to look for the function prototype. • i.e. The show() function of class A is virtually hidden by the show function of class B (for the first call of show() in prev eg)

  27. Function Overriding is a form of overloading. Justify! Though the signatures of the overriding function and overridden function are the same, The This pointer brings in the apparent difference. Actual prototype of A::show() becomes void show(A * const); Actual prototype of B::show() becomes void show(B * const);

  28. BASE CLASS INITIALIZATION • When an object of derived class is created, the compiler implicitly embeds a call to the base class constructor and then the derived class constructor with respect to the object. Suppose A is the base class and B is its derived class: B B1; is converted into B B1; B1.A(); B1.B(); • Destructors are called in reverse order.

  29. class B:public A { int y; public: B (const int=0); void sety(const int =0); intgety() const; }; B::B(const int q) { y=q;} void B::sety(const int q) { y=q; } int B::gety()const { return y;} class A { int x; public: A (const int=0); void setx(const int =0); int getx() const; }; A::A(const int p) { x=p;} void A::setx(const int p) { x=p; } int A::getx()const { return x;} void main() { B B1(20); cout<<B1.getx()<<endl<<B1.gety()<< endl;}

  30. B B1(20); gets converted into B B1; // Memory allocated for object B1. A(); //Base class constructor called B1.B(20); //derived class constructor called How should the base class constructor be modified to make proper base class initialization? Class B : public A { public: B(const int=0,const int =0); …….}; B::B(const int p, const int q):A(p) { y=q; }

  31. void main() { B B1(10,20); cout<<B1.getx()<< endl<< B1.gety()<< endl; } B B1 (10,20) gets converted to B B1; B1. A(10); B1. B(20);

  32. It is not necessary that the first parameter should be passed to the Base class constructor. • Any of the parameter in the list can be passed to the base class constructor.

  33. The PROTECTED access specifier • ‘Protected ‘ members are inaccessible to nonmember functions. • They are accessible to the member functions of their own class and to member functions of the derived classes.

  34. class A { private: int x; protected: int y; public: int z;}; class B: public A { public: void xyz();}; void B::xyz() // member function of derived class { x=1; // Error: Private member of base class y=2; //Ok: Protected member of base class z=3; // Ok: Public member of base class }

  35. void main() { A * Aptr; Aptr->x=10; Aptr->y=20; Aptr->z=30; } // Non member function // Error: Private member // Error: Protected member //Ok: Public member

  36. Deriving by Different Access Specifiers Deriving by the Public Access Specifier - Deriving by the public access specifier retains the access level of the Base class members. Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

  37. Deriving by the Public Access Specifier…. Protected Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can also access them. * Non-member functions cannot access them.

  38. Deriving by the Public Access Specifier…. Public Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can access them. * Non-member functions can access them.

  39. class A { private: int x; protected: int y; public : int z; }; class B:public A{ //B is public derived class of A public: void f1() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=2; } }; //ok. Public member remains public class c:public B{ public: void f2() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=3;}}; //ok. Public member remains public

  40. void xyz(B *Bptr) // non-member function { Bptr->x=10; //Error! Private member remains private Bptr->y=20; //Error! Protected member remains protected Bptr->z=30; //ok. Public member remains public }

  41. A base class pointer can point to an object of a derived class that has been derived by using the public access specifier. void xyz() // non member function { B B1; //object of public derived class B1.z=100;// ok. z is public member of class B A *Aptr; //pointer of base class Aptr=&B1; //ok. Can make a base class pointer point at //an object of a public derived class. Aptr->z=100; //ok. Can access inherited public //member of the base class through a //base class pointer. }

  42. Deriving by Different Access Specifiers Deriving by the Protected Access Specifier - Deriving by the protected access specifier reduces the access level of the public Base class members to protected, while the access level of protected and private base class members remain unchanged.

  43. Deriving by the Protected Access Specifier Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

  44. Deriving by the Protected Access Specifier…. Protected Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can also access them. * Non-member functions cannot access them.

  45. Deriving by the Protected Access Specifier…. Public Members of the Base class: * Members of the derived class can access them. * Member functions of the subsequently derived classes can access them. * Non-member functions cannot access them.

  46. class A { private: int x; protected: int y; public : int z; }; class B:protected A{ //B is a protected derived class of A public: void f1() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=2; } }; //ok. Public member remains public class c:public B{ public: void f2() { x=1; //ERROR! Private member remains private y=2; //ok. Protected member remains protected z=3;}}; //ok. Protected member remains protected

  47. void xyz(B *Bptr) // non-member function { Bptr->x=10; //Error! Private member remains private Bptr->y=20;//Error! Protected member remains protected Bptr->z=30; //Error! Public member becomes protected }

  48. A base class pointer cannot point to an object of a derived class that has been derived by using the protected access specifier. void xyz() // non member function { B B1; //object of derived class B1.z=100;// ERROR! Cannot access public member of base //class through an object of the protected derived class. A *Aptr; //pointer of base class Aptr=&B1; //ERROR! Cannot make a base class pointer point //at an object of a protected derived class. Aptr->z=100; //ok. Can access inherited public member of the //base class through a base class pointer. }

  49. Deriving by Different Access Specifiers Deriving by the Private Access Specifier - Deriving by the private access specifier reduces the access level of the public and protected Base class members to private, while the access level of private base class members remain unchanged.

  50. Deriving by the Private Access Specifier Private Members of the Base class: * Members of the derived class cannot access them. * Member functions of the subsequently derived classes cannot access them. * Non-member functions cannot access them.

More Related