-
Notifications
You must be signed in to change notification settings - Fork 0
/
INHERITANCE.cpp
84 lines (80 loc) · 1.34 KB
/
INHERITANCE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<iostream>
#include<string>
using namespace std;
//base class
class Vehicle
{
public:
void start()
{
cout<<"starting engine...."<<"\n";
}
};
//single inheritance
class Car:public Vehicle
{
public:
void drive(){
cout<<"car is running..."<<"\n";
}
};
//multilevel inheritance
class ElectricCar:public Car
{
public:
void driving()
{
cout<<"electric car is running...."<<"\n";
}
};
//hierarchical inheritance
class MotorCycle:public Vehicle
{
public:
void riding()
{
cout<<"motor cycle is running..."<<"\n";
}
};
//multiple inheritance
class Boat//second base class
{
public:
void run()
{
cout<<"boat is running...."<<"\n";
}
};
class Ship:public Vehicle, public Boat
{
public:
void running()
{
cout<<"ship is running...."<<"\n";
}
};
int main()
{
cout<<"single inheritance"<<"\n";
Car mycar;
mycar.start();
mycar.drive();
cout<<"multilevel inheritance"<<"\n";
ElectricCar ec;
ec.start();
ec.drive();
ec.driving();
cout<<"hierarchichal inheritance"<<"\n";
Car c;
c.start();
c.drive();
MotorCycle mc;
mc.start();
mc.riding();
cout<<"multiple inheritance"<<"\n";
Ship s;
s.start();
s.run();
s.running();
return 0;
}