The document discusses various object-oriented programming concepts related to inheritance including multiple inheritance, hierarchical inheritance, hybrid inheritance, virtual base classes, and constructors in derived classes. It provides examples to illustrate each concept. Multiple inheritance allows a class to inherit from more than one parent class. Hierarchical inheritance involves a base class being inherited by multiple derived classes. Hybrid inheritance combines multiple forms of inheritance. Virtual base classes resolve ambiguities that can occur in multiple inheritance involving a common base class. Constructors are executed in a defined order during inheritance.
The document discusses various object-oriented programming concepts related to inheritance including multiple inheritance, hierarchical inheritance, hybrid inheritance, virtual base classes, and constructors in derived classes. It provides examples to illustrate each concept. Multiple inheritance allows a class to inherit from more than one parent class. Hierarchical inheritance involves a base class being inherited by multiple derived classes. Hybrid inheritance combines multiple forms of inheritance. Virtual base classes resolve ambiguities that can occur in multiple inheritance involving a common base class. Constructors are executed in a defined order during inheritance.
The document discusses various object-oriented programming concepts related to inheritance including multiple inheritance, hierarchical inheritance, hybrid inheritance, virtual base classes, and constructors in derived classes. It provides examples to illustrate each concept. Multiple inheritance allows a class to inherit from more than one parent class. Hierarchical inheritance involves a base class being inherited by multiple derived classes. Hybrid inheritance combines multiple forms of inheritance. Virtual base classes resolve ambiguities that can occur in multiple inheritance involving a common base class. Constructors are executed in a defined order during inheritance.
The document discusses various object-oriented programming concepts related to inheritance including multiple inheritance, hierarchical inheritance, hybrid inheritance, virtual base classes, and constructors in derived classes. It provides examples to illustrate each concept. Multiple inheritance allows a class to inherit from more than one parent class. Hierarchical inheritance involves a base class being inherited by multiple derived classes. Hybrid inheritance combines multiple forms of inheritance. Virtual base classes resolve ambiguities that can occur in multiple inheritance involving a common base class. Constructors are executed in a defined order during inheritance.
•Hierarchical inheritance •Hybrid Inheritance •Virtual base class •Constructors in derived classes
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Multiple inheritance
If a class is derived from two or more classes is called
multiple inheritance.
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Multiple inheritance Syntax of derived class is: class derived_class : visibility_mode Base_class_1, visibility_mode Base_class_2, - - - - - - - visibility_mode Base_class_n { Members of derived class };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Multiple inheritance-Example class B1 { protected: int x; public: void get_x(int a) { x=a; } }; class B2 { protected: int y; public: void get_y(int b) { y=b; } };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Multiple inheritance-Example class D : public B1, public B2 { int z; public: void multiply(void) { z=x*y; } void display(void) { cout<<“x=”<<x<<“\n”; cout<<“y=”<<y<<“\n”; cout<<“z=”<<z<<“\n”; } };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Multiple inheritance-Example int main() { D d; d.get_x(5); d.get_y(3); d.multiply(); d.display(); return 0; } Output x=5 y=3 z=15 Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Multiple inheritance-Example The derived class ‘D’ has the following members after this derivation.
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Hierarchical inheritance When a base class is inherited by two or more derived classes, is known as hierarchical inheritance. Example-
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Hierarchical inheritance-Example class person { protected: char name[20]; int age; public: void get_person(const char *n, int a) { strcpy(name,n); age=a; } void show_person(void) { cout<<“Name:”<<name<<“\t”<<“Age:”<<age<<“\t”; } };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Hierarchical inheritance-Example class teacher : public person { char post[10]; public: void get_post(const char *p) { strcpy(post,p); } void show_teacher(void) { show_person(); cout<<“post:”<<post<<“\n”; } }; Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Hierarchical inheritance-Example class student : public person { int standard; public: void get_standard(int s) { standard=s; } void show_student(void) { show_person(); cout<<“Standard:”<<standard<<“\n”; } }; Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Hierarchical inheritance-Example int main() { teacher t; t.get_person(“Ramesh”,30); t.get_post(“TGT”); student s; s.get_person(“Suresh”,17); s.get_standard(12); t.show_teacher(); s.show_student(); return 0; } Output Name: Ramesh Age: 30 Post: TGT Name: Suresh Age: 17 Standard: 12
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Hybrid Inheritance The combination of two or more forms of inheritance is known as hybrid inheritance. Example- The combination of multilevel inheritance and multiple inheritance.
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Ambiguity resolution in Inheritance In multiple inheritance, when a function with same name appears in more than one base class. Example- class M { public: void display() { cout<<“Class M”;} }; class N { public: void display() { cout<<“Class N”;} }; Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Ambiguity resolution in Inheritance class P : public M, public N { public: void display() // overrides display() of M and N {M:: display();} }; int main() { P p; p. display(); } Output Class M
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Virtual base class Consider the following hybrid inheritance.
This situation creates a problem that all public and
protected members of ‘person’ are inherited into ‘TA’ twice. Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Virtual base class This ambiguity can be resolved by making the common base class as virtual base class while declaring the direct base classes. class person { -------- }; class teacher : virtual public person { -------- }; class student : virtual public person { -------- }; class TA: public teacher, public student { -------- };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Constructors in derived classes When both the base and derived classes contain constructors, the base class constructor is executed first and then the derived class constructor. In multiple inheritance, the constructors in base classes are executed in which they appear in the declaration of the derived class. In multilevel inheritance, the constructors will be executed in the order of inheritance. The constructors in virtual base classes are executed before any non-virtual base classes. Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Constructors in derived classes Example- class alpha { int x; public: alpha(int i) { x = i; cout << "\n alpha initialized \n"; } void show_x() { cout << "\n x = "<<x; } }; Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Constructors in derived classes class beta { float y; public: beta(float j) { y = j; cout << "\n beta initialized \n"; } void show_y() { cout << "\n y = "<<y; } ); Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Constructors in derived classes class gamma : public beta, public alpha { int n,m; public: gamma(int a, float b, int c, int d): alpha(a), beta(b) { m = c; n = d; cout << "\n gamma initialized \n"; } void show_mn() { cout << "\n m = "<<m; cout << "\n n = "<<n; } };
Prepared by: Anil Kumar Tailor, Assistant Prof.
Engineering College Ajmer Constructors in derived classes void main() { gamma g(5, 7.65, 30, 100); cout << "\n"; g.show_x(); g.show_y(); g.show_mn(); } Output beta initialized alpha initialized gamma initialized x=5 y = 7.65 m = 30 n = 100 Prepared by: Anil Kumar Tailor, Assistant Prof. Engineering College Ajmer Thank you