Publicidad

Inheritance in C++

Student at punjab university lahore
19 de Nov de 2016
Publicidad

Más contenido relacionado

Publicidad

Más de Adil Aslam(20)

Publicidad

Inheritance in C++

  1. Inheritance in C++ Lecture Slides By Adil Aslam My Email Address: adilaslam5959@gmail.com Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  2. Object Oriented Programming in C++ Lecture Slides By Adil Aslam About Me i am Student of BSCS My email address: adilaslam5959@gmail.com
  3. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Inheritance in C++ Outer Class
  4. Inheritance in C++ • Inherit Definition - Derive quality and characteristics from parents or ancestors. Like you inherit features of your parents. • Example: "She had inherited the beauty of her mother" • Inheritance in Object Oriented Programming can be described as a process of creating new classes from existing classes. • The process of obtaining the data members and methods from one class to another class is known as inheritance. It is one of the fundamental features of object-oriented programming. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  5. Inheritance in C++ • Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is called Child or Derived or Sub class. • Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused. Object Oriented Programming in C++ Lecture Slides By Adil Aslam NOTE : All members of a class except Private, are inherited
  6. Advantage of inheritance • If we develop any application using this concept than that application have following advantages, • Application development time is less. • Application take less memory. • Application execution time is less. • Application performance is enhance (improved). • Redundancy (repetition) of the code is reduced or minimized so that we get consistence results and less storage cost. • Use of Virtual Keyword Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  7. Real life Example of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  8. Diagram Object Oriented Programming in C++ Lecture Slides By Adil Aslam In the above diagram data members and methods are represented in broken line are inherited from faculty class and they are visible in student class logically.
  9. Basic Syntax of Inheritance • While defining a subclass like this, the super class must be already defined or at least declared before the subclass declaration. • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, private or protected. • : is operator which is used for inheriting the features of base class into derived class it improves the functionality of derived class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass_name : access_mode Superclass_name { // data members // methods }
  10. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  11. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Super Class/ Parent Class Sub Class Perimeter Sub Class Area
  12. Inheritance in C++ Program Example class Rectangle { ... .. ... }; class Area : public Rectangle { ... .. ... }; class Perimeter : public Rectangle { .... .. ... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  13. Example of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class Dog inherits the properties from its super class Animal Animal Dog
  14. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  15. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Animal is a Super Class
  16. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Dog is a sub Class
  17. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Sub class Dog inherit the Properties of Super class Animal
  18. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Create an object of sub class
  19. Example of Inheritance class Animal { public: int legs = 4; }; class Dog : public Animal { public: int tail = 1; }; int main() { Dog d; cout <<"Legs are: "<<d.legs<<endl; cout << "Tail is: "<<d.tail; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Legs are: 4 Tail is: 1
  20. Inheritance Visibility Mode • Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes. It can either be private, protected or public. • 1) Public Inheritance • This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and public becomes public. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass : public Superclass
  21. Inheritance Visibility Mode • 2) Private Inheritance • In private mode, the protected and public members of super class become private members of derived class. • 3) Protected Inheritance • In protected mode, the public and protected members of Super class becomes protected members of Sub class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class Subclass : Superclass // By default its private inheritance class subclass : protected Superclass
  22. Table showing all the Visibility Modes Object Oriented Programming in C++ Lecture Slides By Adil Aslam Derived Class Derived Class Derived Class Base class Public Mode Private Mode Protected Mode Private Not Inherited Not Inherited Not Inherited Protected Protected Private Protected Public Public Private Protected
  23. Access Control and Inheritance • A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Access public protected private Same class yes yes yes Derived classes yes yes no Outside classes yes no no
  24. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Private Private members of the base class are inaccessible to the derived class. Protected members of the base class become private members of the derived class. Public members of the base class become private members of the derived class.
  25. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Protected Private members of the base class are inaccessible to the derived class. Protected members of the base class become protected members of the derived class. Public members of the base class become protected members of the derived class.
  26. Inheritance Visibility Mode Object Oriented Programming in C++ Lecture Slides By Adil Aslam Member Access Specifier How Members of the Base Class Appear in the Derived Class Public Private members of the base class are inaccessible to the derived class. Protected members of the base class become protected members of the derived class. Public members of the base class become public members of the derived class.
  27. Access Control and Inheritance • A derived class inherits all base class methods with the following exceptions: • Constructors, destructors and copy constructors of the base class. • Overloaded operators of the base class. • The friend functions of the base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  28. class Base { public: int m_public; // can be accessed by anybody private: int m_private; // can only be accessed by Base members and friends (but not derived classes) protected: int m_protected; // can be accessed by Base members, friends, and derived classes }; class Derived: public Base { public: Derived() { m_public = 1; // allowed: can access public base members from derived class m_private = 2; // not allowed: can not access private base members from derived class m_protected = 3; // allowed: can access protected base members from derived class } }; int main() { Base base; base.m_public = 1; // allowed: can access public members from outside class base.m_private = 2; // not allowed: can not access private members from outside class base.m_protected = 3; // not allowed: can not access protected members from outside class } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  29. Types of Inheritance • Based on number of ways inheriting the feature of base class into derived class it have five types they are: • Single inheritance • Multiple inheritance • Hierarchical inheritance • Multiple inheritance • Hybrid inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  30. Types of Inheritance Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  31. Types of Inheritance • Single Inheritance • In single inheritance there exists single base class and single derived class. • It is the most simplest form of Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B
  32. Single Inheritance • In single inheritance there exists single base class and single derived class. • It is the most simplest form of Inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Parent Class/ Base Class Child Class/ Derived Class
  33. Single Inheritance Example • When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance. • For example, • Animal is derived from living things • Car is derived from vehicle • Typist is derived from staff • Manager derived from employee • Circle derived from shapes Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  34. Syntax of Inheritance in C++ cclass base_classname { properties... methods... }; class derived_classname : visibility_mode base_classname { properties... methods... }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  35. Single Inheritance Example • For example: Car, Bicycle, Motorcycle are all vehicles and have many similar properties like tire, brakes, seat, etc. So they can be derived from class Vehicle. • Therefore, vehicle is base class and car, bus, motorcycle are derived classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Vehicle MotorcycleBusCar
  36. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  37. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A is a Parent Class Class B is a Child Class
  38. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Public Data Member of Parent Class
  39. Single Inheritance Example class A { Public: int salary; }; class B: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Child class B inherit the properties of Parent class A After Inheritance Class A Member Variable goes in Class B as a Member Variable of Class B
  40. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  41. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class is Person Child Class Student Access the Properties of the Parent Class
  42. Example of Single Inheritance-1 class Person { public: string name; int age; }; class Student : public Person { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Student Look like this class Student : public Person { public: string name; int age; public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Age is: "<<age; } };
  43. Example of Single Inheritance-2 int main() { //Below Creating an object of Child Class(Student) Student s; s.name="Adil Aslam"; s.age=20; //Calling Child Class Function s.show(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  44. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  45. Another Example of Single Inheritance class Employee { public: int salary; }; class Developer : public Employee { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary; } }; int main() { Developer obj; //Creating an object of Developer Class (Child Class) obj.data(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  46. Another Example of Single Inheritance class Employee { public: int salary; }; class Developer : public Employee { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary; } }; int main() { Developer obj; //Creating an object of Developer Class (Child Class) obj.data(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class is Employee Child Class is Developer
  47. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  48. Same Previous Program with some Modifications-1 class Employee //Parent Class { public: int salary; }; class Developer : public Employee //Developer is Child Class { public: void data() { cout<<"Enter the Salary: "; cin>>salary; cout<<"Salary is: "<<salary<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  49. Same Previous Program with some Modifications-1 int main() { //Creating an object of Developer Class(Child Class) Developer d; d.data(); //Creating an object of Employee Class(Parent Class) Employee e; e.salary=1000000; cout<<"Now Salary is: "<<e.salary; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  50. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  51. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } Above which function is called
  52. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } Above which function is called Ans is typist class method is called Typist Class Method having high Preference
  53. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.display(); } What about if we want to call staff Class Method…???
  54. Ambiguity in Single Inheritance in C++ • If parent and child classes have same named method, parent name and scope resolution operator(::) is used. This is done to distinguish the method of child and parent class since both have same name. Object Oriented Programming in C++ Lecture Slides By Adil Aslam class staff { public: void getdata(){} void display(){} }; class typist: public staff { public: void getdata(){} void display(){} }; int main() { typist t; t.staff::display(); } What about if we want to call staff Class Method…??? Ans is use Scope Resolution Operator(::)
  55. Example of Single Inheritance-1 class staff { private: char name[50]; int code; public: void getdata(); void display(); }; class typist: public staff { private: int speed; public: void getdata(); void display(); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::)
  56. Example of Single Inheritance-2 void staff::getdata() { cout<<"Name:"; gets(name); cout<<"Code:"; cin>>code; } void staff::display() { cout<<"Name:"<<name<<endl; cout<<"Code:"<<code<<endl; } void typist::getdata() { cout<<"Speed:"; cin>>speed; } void typist::display() { cout<<"Speed:"<<speed<<endl; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::)
  57. Example of Single Inheritance-3 int main() { typist t; cout<<"Enter data"<<endl; t.staff::getdata(); //calling staff class method t.getdata(); cout<<endl<<"Display data"<<endl; t.staff::display(); //calling staff class method t.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this Example we use Scope Resolution Operator(::) In this example, typist class is derived and staff class is the base class. Public members of class staff such as staff::getdata() and staff::display() are inherited to class typist. Since the child is derived from a single parent class, it is single inheritance.
  58. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  59. One More Example of Single Inheritance-1 class person { //Parent class private: char fname[100],lname[100],gender[10]; protected: int age; public: void input_person(); void display_person(); }; class student: public person { //Child class private: char college_name[100]; char level[20]; public: void input_student(); void display_student(); }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  60. One More Example of Single Inheritance-2 void person::input_person() { cout<<"First Name: "; cin>>fname; cout<<"Last Name: "; cin>>lname; cout<<"Gender: "; cin>>gender; cout<<"Age: "; cin>>age; } void person::display_person() { cout<<"First Name : "<<fname<<endl; cout<<"Last Name : "<<lname<<endl; cout<<"Gender : "<<gender<<endl; cout<<"Age : "<<age<<endl; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  61. One More Example of Single Inheritance-3 void student::input_student() { person::input_person(); cout<<"College: "; fflush(stdin); gets(college_name); cout<<"Level: "; cin>>level; } void student::display_student() { person::display_person(); cout<<"College : "<<college_name<<endl; cout<<"Level : "<<level<<endl; } int main() { student s; cout<<"Input data"<<endl; s.input_student(); cout<<endl<<"Display data"<<endl; s.display_student(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  62. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  63. Explanation of Previous Program • In the previous example, we have a class person with attributes fname (first name), lname (last name), gender and age and methods input_person() to input data and display_person() to display data. Another class student is derived from person which has college_name and level as attributes and input_student() and display_student() as methods to input and display data respectively. • Here, person is base class and student is derived class. Since, person has publicly inherited student members, the private members fname, lname and gender are not inherited. The protected member age is inherited as a protected member in student, and public member functions input_person() and display_person() are inherited as public members. This is the simplest example of inheritance. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  64. Types of Inheritance • Multiple Inheritance • In this type of inheritance a single derived class may inherit from two or more than two base classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C
  65. Types of Inheritance • Multiple Inheritance • In this type of inheritance a single derived class may inherit from two or more than two base classes. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class B Class C Parent ClassParent Class Child Class
  66. Multiple Inheritance Example • When a class is derived from two or more base classes, such inheritance is called Multiple Inheritance. It allow us to combine the features of several existing classes into a single class. • For example, • Petrol is derived from both liquid and fuel. • A child has character of both his/her father and mother, etc. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  67. Syntax of Multiple Inheritance class base_class1 { properties; methods; }; class base_class2 { properties; methods; }; class derived_classname : visibility_mode base_class1, visibility_mode base_class2 { properties; methods; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  68. Multiple Inheritance Example class A { }; class B { }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  69. Multiple Inheritance Example class A { }; class B { }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Parent Class Parent Class Child Class Note: Class A and Class B Both are Parent in Context of Child Class B
  70. Multiple Inheritance Example class A { public: string name; }; class B { public: int marks; }; class C: public B, public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Child Class C inherit the Properties of both Class A and Class B After inheritance Child Class C look Like this class C: public B, public A { public: string name; public: int marks; };
  71. Example of Multiple Inheritance class A { public: string name; }; class B { public: int marks; }; class C: public B, public A { public: void show() { cout<<"Name is: "<<name<<endl; cout<<"Marks are: "<<marks; } }; int main() { C c; c.name="Adil Aslam"; c.marks=90; c.show(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  72. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  73. Ambiguity in Multiple Inheritance • In multiple inheritance, a single class is derived from two or more parent classes. So, there may be a possibility that two or more parents have same named member function. • If the object of child class needs to access one of the same named member function then it results in ambiguity. The compiler is confused as method of which class to call on executing the call statement. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  74. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  75. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A and Class B Both have Member Function with the same Name
  76. Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.display(); /*causes ambiguity*/ } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here we call the display function using object of class C but compiler confuse which display function it call because Class C inherit the Properties of both class A and class B Class C After Inheritance Look Like this class C: public A, public B { public: void display() { cout <<"This is method of A"; void display() { cout <<"This is method of B"; };
  77. Compiler View Object Oriented Programming in C++ Lecture Slides By Adil Aslam Compiler Error
  78. Ambiguity Resolution of Multiple Inheritance • This problem can be resolved by class name and using scope resolution operator to specify the class whose method is called. • In the previous example, if we want to call the method of class A then we can call it as below, • Similarly, if we need to call the method of class B then, Object Oriented Programming in C++ Lecture Slides By Adil Aslam derived_objectname.parent_classname::same_named_function([parameter]); sample.A :: display(); sample.B :: display();
  79. Solution of Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.A::display(); //Now Here A Class (Parent Class) Method is Called } Object Oriented Programming in C++ Lecture Slides By Adil Aslam This is method of A
  80. Solution of Ambiguity in Multiple Inheritance class A { public: void display() { cout <<"This is method of A"; } }; class B{ public: void display() { cout <<"This is method of B"; } }; class C: public A, public B { public: }; int main() { C sample; sample.B::display(); //Now Here B Class (Parent Class) Method is Called } Object Oriented Programming in C++ Lecture Slides By Adil Aslam This is method of B
  81. Solution of Ambiguity in Multiple Inheritance-1 class A { public: void display() { cout <<"This is method of An"; } }; class B { public: void display() { cout <<"This is method of B"; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  82. Solution of Ambiguity in Multiple Inheritance-2 class C: public A, public B { public: void output() { A::display(); B::display(); } }; int main() { C sample; sample.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  83. Solution of Ambiguity in Multiple Inheritance-2 class C: public A, public B { public: void output() { A::display(); B::display(); } }; int main() { C sample; sample.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here inside a function body we Set which function is called
  84. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  85. Another Example of Multiple Inheritance class Mammal { public: Mammal() { cout << "Mammals can give direct birth." << endl; } }; class WingedAnimal { public: WingedAnimal() { cout << "Winged animal can flap." << endl; } }; class Bat: public Mammal, public WingedAnimal { }; int main() { Bat b1; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  86. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  87. One More Example of Multiple Inheritance-1 class student { protected: int rno,m1,m2; public: void get() { cout<<"Enter the Roll no :"; cin>>rno; cout<<"Enter the two marks :"<<endl; cin>>m1>>m2; } }; class sports { protected: int sm; // sm = Sports mark public: void getsm() { cout<<"nEnter the sports mark :"<<endl; cin>>sm; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  88. One More Example of Multiple Inheritance-2 class statement:public student,public sports { int tot,avg; public: void display() { tot=(m1+m2+sm); avg=tot/3; cout<<"nntRoll No : "<<rno<<"ntTotal : "<<tot; cout<<"ntAverage : "<<avg; } }; int main() { statement obj; obj.get(); obj.getsm(); obj.display(); } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  89. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  90. Example of Multiple Inheritance-1 class liquid { float specific_gravity; public: void input() { cout<<"Specific gravity: "; cin>>specific_gravity; } void output() { cout<<"Specific gravity: "<<specific_gravity<<endl; } }; class fuel { float rate; public: void input() { cout<<"Rate(per liter): $ "; cin>>rate; } void output() { cout<<"Rate(per liter): $ "<<rate<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  91. Example of Multiple Inheritance-2 class petrol: public liquid, public fuel { public: void input() { liquid::input(); fuel::input(); } void output() { liquid::output(); fuel::output(); } }; int main() { petrol p; cout<<"Enter data"<<endl; p.input(); cout<<endl<<"Displaying data"<<endl; p.output(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  92. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  93. Types of Inheritance • Hierarchical Inheritance • In this type of inheritance, multiple derived classes inherits from a single base class. Object Oriented Programming in C++ Lecture Slides By Adil Aslam Class A Class CClass B Class D
  94. Hierarchical Inheritance Example • When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class. Problems where hierarchy has to be maintained can be solved easily using this inheritance. • For Example: • Civil, Computer, Mechanical, Electrical are derived from Engineer. • Natural language, Programming language are derived from Language. Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  95. Syntax of Hierarchical Inheritance class base_classname { properties; methods; }; class derived_class1:visibility_mode base_classname { properties; methods; }; class derived_class2:visibility_mode base_classname { properties; methods; }; ... ... ... class derived_classN:visibility_mode base_classname { properties; methods; }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  96. Hierarchical Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  97. Hierarchical Inheritance Example class A { }; class B: public A { }; class C: public A { }; class D: public A { }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Here A is Parent Class Class B, C and D are Child Classes of Class A
  98. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  99. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam Side is a Parent Class Class “Square” is Child of “Slide” Class Class “Cube” is Child of “Slide” Class
  100. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Square Look Like tis class Square: public Side { protected: int l; public: void set_values (int x) { l=x; } public: int sq() { return (l *l); } };
  101. Example of Hierarchical Inheritance-1 class Side { protected: int l; public: void set_values (int x) { l=x; } }; class Square: public Side { public: int sq() { return (l *l); } }; class Cube:public Side { public: int cub() { return (l *l*l); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam After Inheritance Class Cube Look Like tis class Cube: public Side { protected: int l; public: void set_values (int x) { l=x; } public: int cub() { return (l *l*I); } };
  102. Example of Hierarchical Inheritance-2 int main () { //Creating an Object of Class Square Square s; s.set_values (10); cout<<"---------Result----------"<<endl<<endl; cout << "The square value is::" << s.sq() << endl; //Creating an Object of Class Cube Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In the above example the two derived classes "Square", "Cube" uses a single base class "Side". Thus two classes are inherited from a single class. This is the hierarchical inheritance OOP's concept in C++.
  103. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  104. One More Example of Hierarchical Inheritance-1 class Shape { protected: float width, height; public: void set_data (float a, float b) { width = a; height = b; } }; class Rectangle: public Shape { public: float area () { return (width * height); } }; class Triangle: public Shape { public: float area () { return (width * height / 2); } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  105. One More Example of Hierarchical Inheritance-1 int main () { //Creating an Object of Class Rectangle Rectangle rect; //Creating an Object of Class Triangle Triangle tri; rect.set_data (5,3); tri.set_data (2,5); cout<<"------------Result------------"<<endl<<endl; cout <<"Area of Rectangle is: "<<rect.area() << endl; cout <<"Area of Triangle is: "<<tri.area() << endl; return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  106. Explanation of the Previous Program • The object of the class Rectangle contains : width, height inherited from Shape becomes the protected member of Rectangle. • set_data() inherited from Shape becomes the public member of Rectangle area is Rectangle’s own public member The object of the class Triangle contains : width, height inherited from Shape becomes the protected member of Triangle.set_data() inherited from Shape becomes the public member of Triangle area is Triangle’s own public member • set_data () and area() are public members of derived class and can be accessed from outside class i.e. from main() Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  107. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  108. Example of Hierarchical Inheritance-1 class person { char name[100],gender[10]; int age; public: void getdata() { cout<<"Name: "; fflush(stdin); /*clears input stream*/ gets(name); cout<<"Age: "; cin>>age; cout<<"Gender: "; cin>>gender; } void display() { cout<<"Name: "<<name<<endl; cout<<"Age: "<<age<<endl; cout<<"Gender: "<<gender<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  109. Example of Hierarchical Inheritance-2 class student: public person{ char institute[100], level[20]; public: void getdata() { person::getdata(); cout<<"Name of College/University: "; fflush(stdin); gets(institute); cout<<"Level: "; cin>>level; } void display() { person::display(); cout<<"Name of College/University: "<<institute<<endl; cout<<"Level: "<<level<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  110. Example of Hierarchical Inheritance-3 class employee: public person { char company[100]; float salary; public: void getdata() { person::getdata(); cout<<"Name of Company: "; fflush(stdin); gets(company); cout<<"Salary: Rs."; cin>>salary; } void display(){ person::display(); cout<<"Name of Company: "<<company<<endl; cout<<"Salary: Rs."<<salary<<endl; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  111. Example of Hierarchical Inheritance-4 int main() { student s; employee e; cout<<"Student"<<endl; cout<<"Enter data"<<endl; s.getdata(); cout<<endl<<"Displaying data"<<endl; s.display(); cout<<endl<<"Employee"<<endl; cout<<"Enter data"<<endl; e.getdata(); cout<<endl<<"Displaying data"<<endl; e.display(); return 0; } Object Oriented Programming in C++ Lecture Slides By Adil Aslam In this program student and employee classes are derived from person. Person has two public methods: getdata() and display(). These methods are inherited by both student and employee. Input is given using getdata() method and displayed using display() method. This is an example of hierarchical inheritance since two classes are derived from a single class.
  112. Output of the Previous Program is : Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  113. Hierarchical Inheritance Example class Number { private: int num; public: void getNumber() { cout << "Enter an integer number: "; cin >> num; } int returnNumber() { return num; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  114. Hierarchical Inheritance Example Cont. //Base Class 1, to calculate square of a number class Square : public Number { public: int getSquare() { int num, sqr; //get number from class Number num=returnNumber(); sqr=num*num; return sqr; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  115. Hierarchical Inheritance Example Cont. //Base Class 2, to calculate cube of a number class Cube : public Number { private: public: int getCube(){ int num, cube; //get number from class Number num=returnNumber(); cube=num*num*num; return cube; } }; Object Oriented Programming in C++ Lecture Slides By Adil Aslam
  116. Hierarchical Inheritance Example Cont. int main() { Square objS; Cube objC; int sqr, cube; objS.getNumber(); sqr =objS.getSquare(); cout << "Square of "<< objS.returnNumber() << " is: " << sqr << endl; objC.getNumber(); cube=objC.getCube(); cout << "Cube of "<< objS.returnNumber() << " is: " << cube << endl; return 0; } Object Oriented Programming in C++ Lecture