Inherteritance MST
Inherteritance MST
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 {
{
int i, j; derived ob(3);
public: ob.set(1, 2); // access
void set(int a, int b)
{ i=a; j=b; }
member of base
void show() ob.show(); // access
{ cout << i << " " << j << "\n"; } member of base
};
ob.showk(); // uses
class derived : public base {
int k;
member of derived
public: class
derived(int x) { k=x; } return 0;
void showk() { cout << k << "\n"; }
}
};
27
// This program won't compile. public:
#include <iostream.h> derived(int x) { k=x; }
using namespace std; void showk()
class base { { cout << k << "\n"; }
int i, j; };
public: int main()
void set(int a, int b) { i=a; j=b; } {
void show() { cout << i << " " <<
derived ob(3);
j << "\n";}
ob.set(1, 2);
};
// error, can't access set()
// Public elements of base are
private in derived. ob.show();
class derived : private base { // error, can't access show()
int k; return 0;
}
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 {
public:
protected:
void set(int i, int j) { x=i; y=j; }
int x;
};
public:
int main()
void showx() { cout << x << "\n"; }
{
}; derived ob;
class base2 { ob.set(10, 20);
protected: // provided by derived
int y; ob.showx(); // from base1
Public: ob.showy(); // from base2
void showy() {cout << y << "\n";} 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();
cout << "1st derived class return(0);
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: " <<
gender << endl; }
//Base Class
};
class member {
//derived from member
char gender[10];
class stud : public member
int age; { char level[20];
public: public:
void get() void getdata() {
{ cout << "Age: "; cin >> age; member::get();
cout << "Gender: "; cin >> cout << "Class: ";
gender; } cin >> level; } Continue...
38
void disp2() void disp3() { member::disp();
{ member::disp(); cout << "Salary: Rs." << salary
cout << "Level: " << level; << endl;
} };
} };
//staff class derived from
int main() {
member
//member M;
class staff : public member
staff S;
{ float salary;
stud s;
public:
s.getdata();
void getdata() s.disp();
{ member::get();
S.getdata();
cout << "Salary: Rs."; S.disp();
cin >> salary; } 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