Oop - Transperencies Ch 04

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Oop - Transperencies Ch 04 as PDF for free.

More details

  • Words: 1,589
  • Pages: 24
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="<
- 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 -

Related Documents