Docsity

Polymorphism in C++: Implementation of Base and Derived Classes, Exercises of Object Oriented Programming

Toggle sidebar
bg1
1C:\DocumentsandSettings\usman.younis\my...2010\Projects\polymorph\polymorph\polymorph.cpp
1#include<iostream>
2usingnamespacestd;
3
4classBase
5{
6public:
7 Base()
8 {cout<<"Baseiscreated\n";}
9 ~Base()
10 {cout<<"Baseisdestructed\n";}
11
12 intbase
_
function(inta)
13 {
14 cout<<"Inbase\n";
15 returna;
16 }
17
18 virtualintincrement(inta){return++a;}
19 virtualintdecrement(inta){return‐‐a;}
20 };
21
22 classDerived1:publicBase
23 {
24 public:
25 Derived1()
26 {cout<<"Derived1isnowcreated\n";}
27
28 intincrement(inta)
29 {
30 cout<<"InDerived1\n";
31 return++a;
32 }
33
34 intsquare(inta)
35 {
36 cout<<"InDerived1\n";
37 returna*a;
38 }
39 ~Derived1()
40 {cout<<"Derived1isnowdestructed\n";}
41
42 };
43
44 classDerived2:publicDerived1
45 {
46 public:
47 Derived2()
48 {cout<<"Derived2isnowcreated\n";}
49
50 virtualintincrement(inta)
51 {
52 cout<<"InDerived2\n";
53 return++a;
54 }
55
56 intdecrement(inta)
57 {
58 cout<<"InDerived2\n";
59 return‐‐a;
60 }
61
62 intderv2
_
function(inta)
63 {
64 cout<<"InDerived2\n";
65 returna;
66 }
docsity.com
pf3

Partial preview of the text

Download Polymorphism in C++: Implementation of Base and Derived Classes and more Exercises Object Oriented Programming in PDF only on Docsity!

C:\Documents and Settings\usman.younis\my ... 2010\Projects\polymorph\polymorph\polymorph.cpp 1

1 #include 2 using namespace std; 3 4 class Base 5 { 6 public: 7 Base() 8 { cout<<"Base is created \n"; } 9 ~Base() 10 { cout<<"Base is destructed \n"; } 11 12 int base_function (int a) 13 { 14 cout<<"In base \n"; 15 return a; 16 } 17 18 virtual int increment(int a) { return ++a; } 19 virtual int decrement(int a) { return ‐‐a; } 20 }; 21 22 class Derived 1 : public Base 23 { 24 public: 25 Derived 1 () 26 { cout<<"Derived 1 is now created \n"; } 27 28 int increment(int a) 29 { 30 cout<<"In Derived 1 \n"; 31 return ++a; 32 } 33 34 int square(int a) 35 { 36 cout<<"In Derived 1 \n"; 37 return a*a; 38 } 39 ~Derived 1 () 40 { cout<<"Derived 1 is now destructed \n"; } 41 42 }; 43 44 class Derived 2 : public Derived 1 45 { 46 public: 47 Derived 2 () 48 { cout<<"Derived 2 is now created \n"; } 49 50 virtual int increment(int a) 51 { 52 cout<<"In Derived 2 \n"; 53 return ++a; 54 } 55 56 int decrement(int a) 57 { 58 cout<<"In Derived 2 \n"; 59 return ‐‐a; 60 } 61 62 int derv 2 _function(int a) 63 { 64 cout<<"In Derived 2 \n"; 65 return a; 66 }

C:\Documents and Settings\usman.younis\my ... 2010\Projects\polymorph\polymorph\polymorph.cpp 2

67 68 ~Derived 2 () 69 { cout<<"Derived 2 is now destructed \n"; } 70 }; 71 72 void main() 73 { 74 Base* ptr; 75 76 //Base Starts 77 ptr = new Base; 78 79 cout<<ptr‐>base_function(‐ 5 )<<endl; 80 cout<<ptr‐>increment( 3 )<<endl; 81 82 delete ptr; 83 //Base Ends 84 85 //Derived 1 Starts 86 ptr = new Derived 1 ; 87 88 cout<<ptr‐>base_function(‐ 5 )<<endl; 89 cout<<ptr‐>increment( 2 )<<endl; 90 cout<<ptr‐>decrement( 4 )<<endl; 91 92 delete ptr; 93 //Derived 1 Ends 94 95 //Derived 2 Start 2 96 ptr = new Derived 2 ; 97 98 cout<<ptr‐>increment( 3 )<<endl; 99 cout<<ptr‐>base_function( 98 )<<endl; 100 cout<<ptr‐>decrement( 54 )<<endl; 101 102 //ptr‐>derv 2 _function( 3 ); Not visible 103 104 delete ptr; 105 //Derived 2 Ends 106 107 Derived 1 * ptr 2 ; 108 109 //Derived 1 Starts 110 ptr 2 = new Derived 1 ; 111 112 cout<<ptr 2 ‐>square( 3 )<<endl; 113 cout<<ptr 2 ‐>base_function( 2 )<<endl; 114 cout<<ptr 2 ‐>increment( 3 )<<endl; 115 116 117 delete ptr 2 ; 118 //Derived 1 Ends 119 120 //Derived 2 Starts 121 ptr 2 = new Derived 2 ; 122 123 cout<<ptr 2 ‐>base_function( 45 )<<endl; 124 cout<<ptr 2 ‐>increment( 56 )<<endl; 125 cout<<ptr 2 ‐>decrement(‐ 9 )<<endl; 126 127 delete ptr 2 ; 128 //Derived 2 Ends 129 130 //ptr 2 = new Base; Can't assign this 131 }