80 likes | 94 Views
Learn about inheritance, the “Is-a” relationship between classes, types of inheritance, and examples to grasp this concept in OOP.
E N D
Inheritance • What is inheritance? • An “Is a” relationship between two classes of objects in which a class • (the derived class) inherits all members of another class (the base class) • Can be used to develop a hierarchy of classes based on abstractions in a top down fashion, e.g. the Location-Point-Circle-Arch hierarchy introduced before • It is also needed for code refinement, expandability, and reuse by • a) Refining the implementation of a class by building a refined • derived class, were some member functions are redefined, • b) Expanding the functionality of the a class by adding new functions • c) Reusing the code in a base class in several derived classes
Inheritance • Types of inheritance • Types of inheritance can be defined depending on the access attributes • of class members as viewed in the derived class, each class may have • private members (can only be accessed by class members or friends), • protected members (as private but can also be accessed by derived classes) • public members ( can be accessed by all) • The types of inheritance are: • a) Public derivation, which is the default derivation type for structs • class D: public B { // define class D…}, also struct D: B{// define D} • leaves the access level unchanged, i.e., • public members of B are public in D • protected members of B are protected in D • private members of B remain private to B (can not be accessed in • D but they are still inherited in D)
Inheritance Types of inheritance (cont.) b) private derivation, which is the default derivation type for classes, class D: private B { // define class D… }, or class D: B {// define D..} converts the public and protected members of the base class into private members of the derived class public members of B are private in D protected members of B are private in D private members of B remain private to B c) protected derivation, class D: protected B { // define class D… } converts the public and protected members of the base class into protected members of the derived class public members of B are protected in D protected members of B are protected in D private members of B remain private to B In all cases, note that private members of B remain private to B
Inheritance • Types of inheritance (cont.) • suppose we have the following members of a base class • class Parent{ private: d1;m1; protected: d2;m2; • public: d3; m3;} • An object of type Parent can access the public members d3 and m3 • class child_1: public Parent{private: d4;m4; • protected: d5; m5; public: d6;m6;} • Members of child1 have also direct access to d2,m2, d3, and m3, the protected and public members of Parent • An object of type child1can access d6, m6, d3,and m3
Inheritance • class child2: private Parent{private:d7;m7; protected: d8;m8; • public: d9;m9;} • Members of child2 also have direct access to d2, d3, m2, • and m3 of members of Parent same as members of Child_1 • An object of type child2 can only access d9, and m9 • class child3: protected Parent{private: d10; protected d11; • public: d12; m12;} • Members of child3 and classes derived from child3 can have • also direct access to d2,m2, d3, and m3 of Parent • Objects of type child 3 can access only d12, and m12 • Note that d1, and m1 are private to Parent and remain • private to Parent
Inheritance Examples in inheritance class B{ int a; public: int b,c; int Bfun(void);}; class X: private B {int d; public: B::c; //c inherited as private now is public int Xfun(void){ int I = Bfun(); b = 100; // OK, b is declared as public in B a = 10; // Error a is declared as private in B} Friend Relations under inheritance The access privilege of friend functions of a class is similar to that of the member functions of the class. Friend functions of a derived class can access the inherited protected and public members of the base class. Class Base {int x; friend int fbase(void); protected: float g; public: fun();..}; class X: private Base { int a,b; friend int xf(void); public: X(int); fun();…}; int xf(void){ X z(10); z.a = 100; z.g = 0.01; // OK z.x = 100; //error.. }
Inheritance • Friend Relations under inheritance (cont.) • A friend function to the base class can access the base class members inherited in the instance of an object of the derived class, • int fbase(void){ X z(100); z.x = 10; z.g = 0.001; //OK • z.a = 100; // Error….} • Multiple Inheritance • A class can have several base classes • class B1{int x,y; public: B1(int I):x(I){y = 0;} }; • class B2{int x; public: B2(int I){x = I;} }; • class X : public B1, private B2{int a,b; • public: X(int I, int j): B1(I), B2(j) {a=b=0;} }; • X z; // the instance of z contains B1::x, B1::y, B2::x, X::a, and X::b
Inheritance • Multiple Inheritance (cont.) • Suppose that both classes B1 AND B2 are derived from the same base class B, i.e., • class B{ int u,v; public: B(int i){u=v=i;} }; • class B1: public B {……. public: B1(int I):B(I),x(I){y=0;} • class B2: public B {…… public: B2(int I): B(I) {x = I;} • X z; // the instance of z contains B1::x, B2::x, X::a,b, and two different copies of the inherited data members of B, one is initialized thr’ B1 and the other is initialized thr’ B2. • Virtual Base Class • is used to prevent the above problem by having only one copy of the base inherited by any derived class in complex class hierarchy, • class B1: virtual public B{….}; class B2: virtual public B{…} • The virtual base class is initialized by its most derived class in the class hierarchy, the initialization of B from B1 and B2 are completely ignored