Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need...

24
Inheritance : Extending Classes Chapter 06 Class XII [CS]

Transcript of Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need...

Page 1: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Inheritance : Extending Classes

Chapter 06

Class XII [CS]

Page 2: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Need of Inheritance

• Implement Real-World Concept

• Reusability of Code

• To have a Transitive nature

A -> B -> C

So, A -> C

Page 3: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Types of Inheritance

1. Single Inheritance

Base Class

Derived Class

Page 4: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

2. Multiple Inheritance

Base Class

Derived Class

Base Class

Page 5: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

3. Hierarchical Inheritance

Base Class

Derived Class Derived Class Derived Class

Page 6: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

4. Multilevel Inheritance

Base Class

Derived Class

Derived Class

Page 7: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

5. Hybrid Inheritance

Base Class

Derived Class

Derived Class

Derived Sub Class

Base Class

Base Class

Base Class

Derived Class

Base Class

Base Class

Base Class

1 2

Page 8: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

C++ eg. of Inheritance

1. Single Inheritance

class Account { public : int Account_no; chat Acc_Type; double Balance; void Deposit(); };

class Acc_Holder : public Account { public : void Withdrawal(); };

Page 9: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

2. Multiple Inheritance

class Account { public : int Account_no; chat Acc_Type; double Balance; void Deposit(); };

class Acc_Holder : public Bank, public Account { public : void Withdrawal(); };

class Bank { public : char Bank_Name[30]; char grade; };

Page 10: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

3. Hierarchical Inheritance

class Account { public : int Account_no; double Balance; };

class Acc_Saving : public Account { public : void Once_Deposit() void Once_Withdrawal(); };

class Acc_Current : public Account { public : void More_Deposit(); void More_Withdrawal(); };

Page 11: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

4. Multilevel Inheritance

class Account { public : int Account_no; double Balance; };

class Acc_Holder : public Acc_Current { public : char Holder_Name[30]; int Account_Mode; };

class Acc_Current : public Account { public : void More_Deposit(); void More_Withdrawal(); };

Page 12: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

5. Hybrid Inheritance

Combination of two or more type of inheritance.

Page 13: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Visibility Mode in Inheritance

Page 14: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Inheritance

private section

public section

protected section

private section

public section

protected section

Base Class Derived Class

Page 15: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

private inheritance

private section

public section

protected section

private section

public section

protected section

Base Class Derived Class

Page 16: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

public inheritance

private section

public section

protected section

private section

public section

protected section

Base Class Derived Class

Page 17: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

protected inheritance

private section

public section

protected section

private section

public section

protected section

Base Class Derived Class

Page 18: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Inheritance Constructor and Destructor

• Constructor and Destructor can never be inherited.

• Then, if there is the parameterized constructor in base class then we have to pass value from derived class separately followed by base class name.

Page 19: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

eg. class BASE { int a; int b; public : BASE ( int i, int j ) { a = i; b = j; } };

class DERIVED : public BASE { int x; int y; public : DERIVED ( int m, int n, int p, int q ) : BASE (p, q) { x = m; y = n; } };

Page 20: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

eg. Execution of Constructor class BASE_1 { int a; int b; public : BASE ( int i, int j ) { a = i; b = j; } };

Continued……………

class BASE_2 { int c; int d; public : BASE ( int k, int l ) { c = k; d = l; } };

Page 21: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

class DERIVED : public BASE_1, public BASE_2 { int x; int y; public : DERIVED ( int m, int n, int p, int q, int r, int s ) : BASE _1(p, q), BASE_2(r, s) { x = m; y = n; } };

Constructor runs 1st

Constructor runs 2nd

Constructor runs 3rd

Page 22: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

class DERIVED : public BASE_1, public BASE_2 { int x; int y; public : DERIVED ( int m, int n, int p, int q, int r, int s ) : BASE_2(r, s) , BASE _1(p, q) { x = m; y = n; } };

Constructor runs 1st

Constructor runs 2nd

Constructor runs 3rd

NOTE : Which Base Class will be used first , the constructor will run first and at end the derived class constructor will run.

Page 23: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Virtual Base Class class A { public : int a; };

class B1 : public A { public : int x; };

class B2 : public A { public : int p; };

class C : public B1, public B2 { public : int q; };

class A { public : int a; };

class B1 : virtual public A { public : int x; };

class B2 : virtual public A { public : int p; };

class C : public B1, public B2 { public : int q; };

Page 24: Inheritance : Extending Classes · Inheritance : Extending Classes Chapter 06 Class XII [CS] Need of Inheritance •Implement Real-World Concept •Reusability of Code •To have

Thanks…….

By : Dinesh Patel PGT [Computer Science] Kendriya Vidyalaya, NAD, Karanja