Chapter 04 Inheritance
Inheritance
A
A
B
B
C
1. Single
2. Multiple
A
A B
B
C
D
3. Hierarchical
4. Multilevel
A B
C
A D
B C
C
D
5. Hybrid
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-1-
Chapter 04 Inheritance
Creating inheritance in C++ class derived-class : visibility-mode base-class { - - - - - // - - - - - // members of derived class - - - - - // }; private derivation: class ABC : private XYZ { members of ABC }; public derivation: class ABC : public XYZ { members of ABC }; default (private) derivation: class ABC : XYZ { members of ABC }; Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-2-
Chapter 04 Inheritance
class Maths { int a; public: int b; void get_ab( ) { a = 5; b = 10; } int get_a( ) { return(a); } void show_a( ) { cout<<"a="<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-3-
Chapter 04 Inheritance
Class Arithmetic private
c public
b
Inherited from ‘Maths’
get_ab( ) get_a( ) show_a( ) mul( ) display( )
Inherited members from base class to derived class in public derivation
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-4-
Chapter 04 Inheritance
Class Arithmetic private
c b get_ab( )
Inherited from ‘Maths’
get_a( ) show_a( )
public
mul( ) display( )
Inherited members from base class to derived class in private derivation
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-5-
Chapter 04 Inheritance
class Maths { int a; public: int b; void get_ab( ) { a = 5; b = 10; } int get_a( ) { return(a); } void show_a( ) { cout<<"a="<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-6-
Chapter 04 Inheritance
Making a private member inheritable
class Alpha { private:
// optional
-------
// visible to member functions
-------
// within its class
protected: -------
// visible to member functions
-------
// of its own & derived classes
public: -------
// visible to all functions
-------
// in the program
};
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-7-
Chapter 04 Inheritance
class B Not inheritable
private protected public
class D1:public B
class D2:private B
private
private
protected
protected
public
public
class X: public D1: protected D2 private protected public
Effect of inheritance on visibility controls
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-8-
Chapter 04 Inheritance
Visibility of inherited members Base class visibility
Derived class visibility Private Protected Public derivation derivation derivation
Private Protected Public
X
X
X
Protected
Private
Protected
Public
Private
Protected
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
-9-
Chapter 04 Inheritance
Multilevel Inheritance Base class
A
Grand Father
Intermediate base class
B
Father
Derived class
C
Child
class A { ----}; class B : public A { ----}; class C : public B { ----};
// base class
// B derived from A
// C derived from B
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 10 -
Chapter 04 Inheritance
class Student { protected: int roll; public: void getNumber(int x) { roll = x; } void putNumber( ) { cout<<"\nRoll:"<
- 11 -
Chapter 04 Inheritance
Multiple Inheritance B1
B2
Bn
D
class D : visibility B1, visibility B2 . . . { ----- - - - - // body of class D ----}; Example: class Maths: public Arith, public Geo { public: void display( ); } Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 12 -
Chapter 04 Inheritance
class Geometry { protected: int m; public: void getM(int x) { m = x; } }; class Arithmetics { protected: int n; public: void getN(int x) { n = x; } }; class Maths : public Geometry, public Arithmetics { public: void displaySquare( ) { cout<<"\nSquare of m:"<<(m*m); cout<<"\nSquare of n:"<<(n*n); } }; int main( ) { Maths x; int a, b; cout<<"Enter two values: "; cin rel="nofollow">>a>>b; x.getM(a); x.getN(b); x.displaySquare( ); return(0); } Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 13 -
Chapter 04 Inheritance
Hierarchical Inheritance Students
Arts
Engineering
Medical
class Students { // members of ‘Students’ }; class Arts : public Students { // members of ‘Arts’ }; class Engineering : private Students { // members of ‘Engineering’ }; class Medical : public Students { // members of ‘Medical’ }; Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 14 -
Chapter 04 Inheritance
class Students { float marks; public: void getMarks(float test1, float test2) { marks = test1 + test2; cout<<"\nAverage: "<<(marks/2); } }; class Arts : private Students { public: void accept(float x, float y) { getMarks(x, y); } }; class Medical : private Students { public: void input(float x, float y) { getMarks(x, y); } }; int main( ) { Arts a; Medical m; a.accept(56.20, 85.14); m.input(63.42, 46.97); return(0); } Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 15 -
Chapter 04 Inheritance
Hybrid Inheritance Students
Test
Result
Students
Sports
Test
Result
Sports
class Students { // - - - - }; class Test : public Students { // - - - - }; class Sports { // - - - - }; class Result : public Test, private Sports { // - - - - }; Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 16 -
Chapter 04 Inheritance
class Students { protected: int roll; public: void getNumber(int n) { roll = n; } void putNumber() { cout<<"\nRoll: "<
- 17 -
Chapter 04 Inheritance
void getScore(float s) { score = s; } void putScore() { cout<<"\nScore: "<<score; } }; class Result : public Test, public Sports { float total; public: void show() { total = test1 + test2; putNumber(); putMarks(); putScore(); cout<<"\nTotal: "<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 18 -
Chapter 04 Inheritance
Virtual Base Class GrandFather
Parent1
Parent2
Child class A { // - - - - }; class B1 : virtual public A { // - - - - }; class B2 : public virtual A { // - - - - }; class C : public B1, public B2 { // - - - - }; Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 19 -
Chapter 04 Inheritance
class Students { protected: int roll; public: void getNumber(int n) { roll = n; } void putNumber( ) { cout<<"\nRoll: "<
- 20 -
Chapter 04 Inheritance
{ protected: float score; public: void getScore(float s) { score = s; } void putScore( ) { cout<<"\nScore: "<<score; } }; class Result : public Test, public Sports { float total; public: void show( ) { total = test1 + test2; putNumber( ); putMarks( ); putScore( ); cout<<"\nTotal: "<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 21 -
Chapter 04 Inheritance
Constructors in the derived classes
Derived-constructor(arg1,arg2,…argn): Base-constructor-1(arg1), Base-constructor-2(arg3, arg2), ------Base-constructor-n(argn) { //body of derived constructor }
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 22 -
Chapter 04 Inheritance
Execution of base class Constructors
Method of inheritance class B : public A {
Order of execution A( ); B( );
}; class A : public B, public C B( { C( A( }; class A : public B, virtual public C C( { B( A( };
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
//base //derived
); ); );
//base1 //base2 //derived
); ); );
//virtual base //base //derived
- 23 -
Chapter 04 Inheritance
class Alpha { public: Alpha(int x) { cout<<"\nAlpha = "<<x; } }; class Beta { public: Beta(int y) { cout<<"\nBeta = "<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.
- 24 -