-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupcasting.cpp
58 lines (45 loc) · 962 Bytes
/
upcasting.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
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class base{
public:
virtual void show() { cout << "I am base class object" << endl; }
};
class derived: public base{
public:
void show() { cout << "I am dervied class object" << endl; }
};
void passByValue(base);
void passByReference(base &);
void passByAddress(base *);
int main(int argc, char *argv[])
{
base b;
derived d;
base *bd = &d;
// bd is upcasting from derived class to base class
bd->show();
// pass by value
passByValue( d );
// pass by reference
passByReference( d );
// pass by address
passByAddress( &d );
return 0;
}
void passByValue(base b)
{
cout << "This in pass by value function" << endl;
b.show();
}
void passByReference(base &b)
{
cout << "This in pass by reference function" << endl;
b.show();
}
void passByAddress(base *b)
{
cout << "This in pass by address function" << endl;
b->show();
}