0% found this document useful (0 votes)
1 views4 pages

Function Overriding

The document discusses function overriding in C++ inheritance, explaining how a derived class can override a base class function when both have the same method signature. It provides examples demonstrating how to call overridden functions and access base class functions using the scope resolution operator. The document also touches on issues that may arise with multiple inheritance and how to resolve them.

Uploaded by

lpshatriya123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Function Overriding

The document discusses function overriding in C++ inheritance, explaining how a derived class can override a base class function when both have the same method signature. It provides examples demonstrating how to call overridden functions and access base class functions using the scope resolution operator. The document also touches on issues that may arise with multiple inheritance and how to resolve them.

Uploaded by

lpshatriya123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
E in Overri When a funet rs or signature and Same same paramete ass is said to overt derived cl on in a derived class has the same return type as a function in its base class, then the finetion in the o the function in the base cl tions with the same member func with the In inheritance, there may be a possibility that a class may inert name from two or more base classes and the derive cass may afso ave the fanelons same name as those of its base classes. se class when both ‘The derived class member function is overriding the member function of the base cla shen be member functions have the same method signature cess the overridden member function then ss is used (0 a In this case, if an object of the derived ¢! it invokes the member function of the derived include using namespace std class Base { ic void display() { ‘cout << "This is base class" << endl; } } class Derived : public Base { public: void display() {/ Overridden the Base class display() function out << "This is derived class" << endl; ' } int main() { Derived d; / Object d is created to the class Derived ddisplay();/ Object d calls the display method of class Derived but not the display() of Base return 0; : : yd Bases de ploy J 1 Boao cps aaar Cal Iof ved class contains the function with the same name, In the above example, the Base class and Det c., the Derived class fanetion is the same number of arguments and the same return-type i overridden on Base class function. ss, that object directly invokes only the Derived class When an object is created to the Derived el member functions. So, d.display(); will call only the display() of Derived class but not the display() of Base class. If the user wants to access the Base class member function display() then it may be called using the class-name with scope resolution operator (::) a8: d.Base::display(); // It will access the display() of the Base class ‘ The same problem can also occur in mt level, multiple and hybrid inheritances but it can be resolved in the same manner as shown above. include using namespace std; class Base { public: void print() { ‘cout <= "Base Function" << endl; class Derived : public Base { public: void print() { cout << "Derived Function” << endl; 1 B int main() { Derived derived!; derived! print); return 0; Inction of the base class, we overridden function by using Sand then calling the use the scope resolution operator 4 pointer of the base class to point to an object function from that pointer We Iso access the of the derived e! H+ prog C++ program to access overridden function in main() using the scope resolution operator #include using namespace std; class Base { public void print() { ‘cout << "Base Function" << endl; class Derived : public Base { publ void print() { cout << "Derived Function” << endl; + h int main() { Derived derived!, derived2; : derived! print(); 1/ access print() function of the Base class derived2.Base::print(); return 0: Output Derived Function Base Function Example 3: C++ Call Overridden Function From Derived Class 1 C++ program to call the overridden function // from a member function of the derived class #include using namespace std. class Base { public: void print() { cout << "Base Function” << endl; } } class Derived : public Base { public void print() { cout << "Derived Function" << endl; // call overridden function Basez:print(); » % int main() { Derived derivedt; derived1 .print(); retuin 0; 3

You might also like