What Is Function Overriding?: Directly
What Is Function Overriding?: Directly
What Is Function Overriding?: Directly
#include <iostream>
using namespace std;
class Base
{ public:
void print() { cout << "Base Function" << endl; }
};
// OUTPUT
class Derived : public Base
Derived Function
{ public: Base Function
void print() { cout << "Derived Function" << endl; }
};
int main()
{ Derived derived1, derived2;
derived1.print();
// access print() function of the Base class with the object of derived class with the help of scope resolution operator ::
derived2.Base::print();
return 0;
}
// C++ program to access overridden function using pointer int main()
// of Base type that points to an object of Derived class {
#include <iostream> Derived derived1;
using namespace std;
class Base // pointer of Base type that points to derived1
{ public: Base* ptr = &derived1;
void print() { cout << "Base Function" << endl; }
}; // call function of Base class using ptr
class Derived : public Base
{ ptr->print();
public: return 0;
void print() { cout << "Derived Function" << endl; } }
};
Output
Base Function