-
Notifications
You must be signed in to change notification settings - Fork 0
/
33.polymorphism.cpp
54 lines (49 loc) · 1.43 KB
/
33.polymorphism.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
#include <iostream>
using namespace std;
class Car
{
public:
// virtual void start(){
// cout<<"Car started"<<endl;
// }
// virtual void stop(){
// cout<<"Car stopped"<<endl;
// }
// pure virtual functionsx
virtual void start() = 0;
virtual void stop() = 0;
};
class Innova: public Car
{
public:
void start(){
cout<<"Innova started"<<endl;
}
void stop(){
cout<<"Innova stopped"<<endl;
}
};
class Swift: public Car
{
public:
void start(){
cout<<"Swift started"<<endl;
}
void stop(){
cout<<"Swift stopped"<<endl;
}
};
// 1. So, as far as now, the class car is useless since both innova and swift are overriding the functions of the car class.
// 2. The car class is more like a abstract concept there is nothing which is just called, more like generalisation, like men, humans etc
// 3. The concept of polymorphism is that using the same car we are staring both innova and swift
// 4. pure virtual function. the function of the parent class is set to 0, hence the derived class should always override it
// if not the class becomes abstract
int main(){
Car *myCar = new Innova(); // this is an important statment right have a look.
myCar->start();
myCar->stop();
myCar = new Swift();
myCar->start();
myCar->stop();
return 1;
}