File tree Expand file tree Collapse file tree 4 files changed +172
-0
lines changed Expand file tree Collapse file tree 4 files changed +172
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Parent {
5
+ public:
6
+ void display (){
7
+ cout<<" display of base" <<endl;
8
+ }
9
+ };
10
+
11
+ class Child :public Parent {
12
+ public:
13
+ void display (){
14
+ cout<<" display of child" <<endl;
15
+ }
16
+ };
17
+
18
+ int main (){
19
+ Parent p;
20
+ p.display (); // parent display function
21
+
22
+ Child c;
23
+ c.display (); // child display fucntion
24
+ c.Parent ::display (); // calling parent display class using scope operator
25
+
26
+ return 0 ;
27
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Parent {
5
+ public:
6
+ virtual void func (){
7
+ cout<<" function of parent" <<endl;
8
+ }
9
+ };
10
+
11
+ class Child :public Parent {
12
+ public:
13
+ void func (){
14
+ cout<<" function of child" <<endl;
15
+ }
16
+ };
17
+
18
+ int main (){
19
+ Parent *ptr = new Child ();
20
+ ptr->func (); // normally without the virtual keyword fucntion of Parent class would have been called because of the pointer.
21
+ return 0 ;
22
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ Polymorphism must have three things :
3
+ * A generalized class with a pointer pointing to the object of a child class.
4
+ (this helps in dynamic programming and achieving less memory usage)
5
+
6
+ * Function overriding in both the generalized as well as the child classes.
7
+
8
+ * Virtual keyword must be used in order to locate the function call to its own object class.
9
+
10
+
11
+ There are three types of Base class :
12
+ * All concrete functions.
13
+ purpose : reusablity.
14
+
15
+ * Some concrete and some pure vitual functions. // Abstract Class
16
+ purpose : reusablity and polymorphism
17
+
18
+ * All pure virtual functions. // Abstract Class : Interface
19
+ purpose : polymorphism.
20
+
21
+ */
22
+
23
+ #include < iostream>
24
+ using namespace std ;
25
+
26
+ // generalized / abstract class : no object can be created.
27
+ class Car {
28
+ public:
29
+ // pure virtual class, since assigned to 0. If not called it becomes abstract.
30
+ virtual void start () = 0;
31
+ virtual void stop () = 0;
32
+ };
33
+
34
+ class Innova :public Car {
35
+ public:
36
+ void start (){
37
+ cout<<" Innova started." <<endl;
38
+ }
39
+ void stop (){
40
+ cout<<" Innova stopped." <<endl;
41
+ }
42
+ };
43
+
44
+ class Swift :public Car {
45
+ public:
46
+ void start (){
47
+ cout<<" Swift started." <<endl;
48
+ }
49
+ void stop (){
50
+ cout<<" Swift stopped." <<endl;
51
+ }
52
+ };
53
+
54
+ int main (){
55
+ Car *car = new Innova ();
56
+ car->start ();
57
+ car->stop ();
58
+
59
+ car = new Swift ();
60
+ car->start ();
61
+ car->stop ();
62
+
63
+ return 0 ;
64
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Shape {
5
+ public:
6
+ virtual float area ()=0;
7
+ virtual float perimeter ()=0;
8
+ };
9
+
10
+ class Rectangle :public Shape {
11
+ private:
12
+ float length;
13
+ float breadth;
14
+ public:
15
+ // constructor
16
+ Rectangle (float length = 1 , float breadth = 1 ){
17
+ this ->length = length;
18
+ this ->breadth = breadth;
19
+ }
20
+
21
+ float area (){
22
+ return length*breadth;
23
+ }
24
+
25
+ float perimeter (){
26
+ return 2 * (length+breadth);
27
+ }
28
+ };
29
+
30
+ class Circle :public Shape {
31
+ private:
32
+ float radius;
33
+ public:
34
+ // constructor
35
+ Circle (float radius = 1 ){
36
+ this ->radius = radius;
37
+ }
38
+
39
+ float area (){
40
+ return 3.14 *radius*radius;
41
+ }
42
+
43
+ float perimeter (){
44
+ return 2 *3.14 *radius;
45
+ }
46
+ };
47
+
48
+ int main (){
49
+ Shape *s;
50
+ s = new Rectangle (1.2 ,4 );
51
+ cout<<" Area of rectangle " <<s->area ()<<endl;
52
+ cout<<" Perimeter of rectangle " <<s->perimeter ()<<endl;
53
+ cout<<endl;
54
+ s = new Circle (4.2 );
55
+ cout<<" Area of circle " <<s->area ()<<endl;
56
+ cout<<" Circumference of circle " <<s->perimeter ()<<endl;
57
+
58
+ return 0 ;
59
+ }
You can’t perform that action at this time.
0 commit comments