InherteritanceMST
InherteritanceMST
Inheritance
1
Inheritance
• 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.
2
Inheritance (Cont…)
• New classes inherit some of the properties and behaviour of
the existing classes.
• The existing class that is "parent" of a new class is called a base
class.
• New class that inherits properties of the base class is called
a derived class(“child class”).
• Inheritance is a technique of code reuse.
3
Example: Insect Taxonomy
4
The "is a" Relationship
• Inheritance establishes an "is a" relationship between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
5
Base Class access control
• Derived class can be declared from a base
class with different access control, i.e., public
inheritance, protected inheritance or private
inheritance.
6
Protected Members and Class Access
7
Class Access Specifiers
public – object of derived class can be treated as object of base
class (not vice-versa)
protected – more restrictive than public, but allows derived
classes to know details of parents
private – prevents objects of derived class from being treated as
objects of base class.
8
Syntax
1. #include <iostream>
2. using namespace std;
3. class base
4. { .... ... .... };
5. class derived : access_specifier base
6. { .... ... .... };
10
Observations
• base class has three member variables: x, y and
z which are public, protected and private
member respectively.
• publicDerived inherits variables x and y as
public and protected. z is not inherited as it is a
private member variable of base class.
• protectedDerived inherits variables x and y.
Both variables become protected. z is not
inherited.
11
Observations (Cont…)
• If we derive a class
derivedFromProtectedDerived from
protectedDerived, variables x and y are also
inherited to the derived class.
• privateDerived inherits variables x and y. Both
variables become private. z is not inherited
• If we derive a class derivedFromPrivateDerived
from privateDerived, variables x and y are not
inherited because they are private variables of
privateDerived.
12
Accessibility in Public Inheritance
13
Accessibility in Protected Inheritance
14
Accessibility in Private Inheritance
15
Inheritance vs. Access
How inherited base class
members
Base class members appear in derived class
private: x private x is inaccessible
protected: y base class
private: y
public: z private: z
17
Inheritance vs. Access
class Grade class Test : protected Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
protected class access, it public members:
Test(int, int);
looks like this: protected members:
void setScore(float);
float getScore();
float getLetter();
18
Inheritance vs. Access
class Grade class Test : private Grade
private members: private members:
char letter; int numQuestions;
float score; float pointsEach;
void calcGrade(); int numMissed;
public members: public members:
void setScore(float); Test(int, int);
float getScore();
char getLetter();
private members:
int numQuestions:
When Test class inherits float pointsEach;
from Grade class using int numMissed;
private class access, it void setScore(float);
float getScore();
looks like this: float getLetter();
public members:
Test(int, int);
19
What Does a Child Have?
An object of the derived class has:
• all members defined in child class
• all members declared in parent class
20
Types of Inheritance
21
Thanks
22
Types of Inheritance
23
Single Inheritance
• Single Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from one base class.
24
Single Inheritance Syntax
25
Single Inheritance Example
1. #include <iostream.h>
2. using namespace std;
3. class Shape { Output
4. protected:
5. int width;
6. int height;
7. public:
8. void setWidth(int w) {
9. width = w; }
10. void setHeight(int h) {
11. height = h; } } ;
12. class Rectangle: public Shape {
13. public:
14. int getArea() {
15. return (width * height); } }
16. int main {
17. Rectangle Rect;
18. Rect.setWidth(5);
19. Rect.setHeight(7);
20. cout << "Total area: " << Rect.getArea() << endl; // Print the area of the object.
21. return 0;
22. }
26
#include <iostream.h> int main()
using namespace std; {
class base { derived ob(3);
int i, j; ob.set(1, 2); // access member of
public: base
void set(int a, int b) ob.show(); // access member of
{ i=a; j=b; } base
void show() ob.showk(); // uses member of
derived class
{ cout << i << " " << j << "\n"; } return 0;
}; }
class derived : public base {
int k;
public:
derived(int x) { k=x; }
void showk() { cout << k << "\n";
}
};
27
// This program won't public:
compile. derived(int x) { k=x; }
#include <iostream.h> void showk()
using namespace std; { cout << k << "\n"; }
class base { };
int i, j; int main()
public: {
void set(int a, int b) { i=a; j=b; derived ob(3);
} ob.set(1, 2);
void show() { cout << i << " " // error, can't access set()
<< j << "\n";}
}; ob.show();
// Public elements of base are // error, can't access show()
private in derived. return 0;
class derived : private base { }
int k;
28
Multiple Inheritance
• Multiple Inheritance: It is the inheritance
hierarchy wherein one derived class inherits
from multiple base class(es).
29
Syntax
30
Inheriting Multiple Base Classes
#include <iostream> // Inherit multiple base classes.
using namespace std; class derived: public base1, public
class base1 { base2 {
protected: public:
int x; void set(int i, int j) { x=i; y=j; }
public: };
void showx() { cout << x << "\n"; } int main()
}; {
class base2 { derived ob;
protected: ob.set(10, 20);
int y; // provided by derived
Public: ob.showx(); // from base1
void showy() {cout << y << "\n";} ob.showy(); // from base2
}; return 0;
}
31
//EXAMPLE // Derived class
#include <iostream.h> class Rectangle: public Shape, public
using namespace std; PaintCost {
// Base class Shape
public:
class Shape {
public: int getArea() {
void setWidth(int w) { return (width * height);
width = w; } } };
void setHeight(int h) { int main(void) {
height = h; }
Rectangle Rect;
protected:
int width; int area;
int height; Rect.setWidth(5);
}; Rect.setHeight(7);
// Base class PaintCost area = Rect.getArea();
class PaintCost {
// Print the total cost of painting
public:
int getCost(int area) { cout << "Total paint cost: $" <<
return area * 70; Rect.getCost(area) << endl;
}}; return 0; }
32
Multilevel Inheritance
• Multilevel Inheritance: It is the inheritance
hierarchy wherein subclass acts as a base class
for other classes.
33
Syntax
34
Multi-level Inheritance
#include <iostream> //derived2 class
using namespace std; class derived2 : public derived
//Base class { public:
class base { void display3(){
public: cout << "\n2nd Derived class
void display1() { content.";
cout << "\nBase class content."; } } };
}; int main()
//derived class {
class derived : public base derived2 D;
{ D.display3();
public: D.display2();
void display2() D.display1();
{ return(0);
cout << "1st derived class content."; }
} };
35
Hierarchical Inheritance
• Hierarchical Inheritance: It is the inheritance
hierarchy wherein multiple subclasses inherit
from one base class.
36
Hierarchical Inheritance
37
Hierarchical Inheritance
#include <iostream> void disp() {
#include <string.h> cout << "Age: " << age <<
using namespace std; endl; cout << "Gender: "
//Base Class << gender << endl; }
class member { };
char gender[10]; //derived from member
int age; class stud : public member
public: { char level[20];
void get() public:
{ cout << "Age: "; cin >> age; void getdata() {
member::get();
cout << "Gender: "; cin >> cout << "Class: ";
gender; }
cin >> level; }
Continue...
38
void disp2() void disp3() {
{ member::disp(); member::disp();
cout << "Level: " << level; cout << "Salary: Rs." <<
} }; salary << endl;
//staff class derived from } };
member
class staff : public member int main() {
{ float salary; //member M;
public: staff S;
void getdata() { stud s;
member::get(); s.getdata();
cout << "Salary: Rs."; s.disp();
cin >> salary; } S.getdata();
S.disp();
return(0); }
39
Hybrid Inheritance
• Hybrid Inheritance: The inheritance hierarchy
that reflects any legal combination of other
four types of inheritance.
40
Syntax
41
#include <iostream> class D : public B, public C
using namespace std; //D is derived from class B and
class A { class C
public: int x; { public:
}; void sum()
class B : public A { { cout << "Sum= " << x + y; }
public: B() //constructor to };
initialize x in base class A int main()
{ x = 10; } { D obj1; //object of derived
}; class D
class C { obj1.sum();
public: int y; C() //constructor to return 0; Output
initialize y { y = 4; } }
};
42
Thanks
43