-
Notifications
You must be signed in to change notification settings - Fork 12
/
shadowing.cpp
executable file
·51 lines (44 loc) · 1.62 KB
/
shadowing.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
#include <iostream>
/*
If a method in the derived class has the same name as base, we have shdowed
that method from base class. Here we have shadowed info() If we need to
overload a function from base class we need to use scope resolution operator ::
Here we have overloader print()
*/
class base {
public:
void info() { std::cout << "info from base" << std::endl; }
void print() { std::cout << "print from base" << std::endl; }
};
class derived : public base {
public:
void info() { std::cout << "info from derived" << std::endl; }
void print(int id) {
std::cout << "print from derived, the given id is: " << id << std::endl;
}
void print() { std::cout << "the print func from derived" << std::endl; }
};
int main() {
base baseObject;
derived derivedObject;
std::cout << "===========info() from base===========" << std::endl;
baseObject.info();
std::cout << "===========print() from base===========" << std::endl;
baseObject.print();
std::cout << "===========info() in the drived class has shadowed the base "
"info()==========="
<< std::endl;
derivedObject.info();
std::cout << "===========calling the shadowed method (print() from base) in "
"derived class==========="
<< std::endl;
derivedObject.base::print();
std::cout << "===========calling the print() which shadows print() from base "
"class==========="
<< std::endl;
derivedObject.print();
std::cout << "===========calling the print() which overloads the print() "
"from base class==========="
<< std::endl;
derivedObject.print(10);
}