-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfun overidding.cpp
40 lines (31 loc) · 947 Bytes
/
fun overidding.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
#include <iostream>
using namespace std;
class Base {
public:
// Virtual function to be overridden
virtual void show() {
cout << "Base class show function called." << endl;
}
void display() {
cout << "Base class display function called." << endl;
}
};
class Derived : public Base {
public:
// Overriding the base class function
void show() override {
cout << "Derived class show function called." << endl;
}
// Not overriding display() from the base class
};
int main() {
Base *basePtr;
Derived derivedObj;
// Base class pointer pointing to derived class object
basePtr = &derivedObj;
// Function overriding
basePtr->show(); // Calls derived class's show() due to function overriding
// Non-virtual function call
basePtr->display(); // Calls base class's display() as it's not overridden
return 0;
}