In C++, Sometimes ignoring compiler warning leads to a great trouble and create confusion. Below given two sample codes make the difference.
With Static Object :-
1) Override function i.e display() is having same signature in both base and Derived class
class B
{
public:
virtual void display() //Base class function
{ ........ }
};
class D: public B
{
public:
virtual void display()//Derived class function
{ ........ }
};
When Code is compile and executed as below:-
main()
{
D dobj;
dobj.display();//call the Derived class function
}
Here output of the program is normal i.e "dobj.display()" calls derived class display() function.
2) Override function i.e display() is having different signature in both base and Derived class
class B
{
public:
virtual void display(int i) //Base class function
{
.................
}
virtual void display() //Base class function with different signature
{ ................... }
};
class D: public B
{
public:
virtual void display() //Derived class function
{ .................. }
};
Get compiler warning with unexpected output on call of "dobj.display(1)" as mentioned above.
Here all the base class functions are not re-declared in derived class as it has different signature the function declared in the derived class. So the statement "dobj.display(1)", will throw error as the function is not re declared in the Derived class.
Bit Complex Example:-
class Base {
public:
int f() const {
cout << "Base::f()"; return 1; } int f(string) const { return 1; } void g() {} }; class Derived1 : public Base { public: void g() const {} }; class Derived2 : public Base { public: // Redefinition: int f() const { cout << "Derived2::f()\n"; return 2; } }; class Derived3 : public Base { public: // Change return type: void f() const { cout << "Derived3::f()\n"; } }; class Derived4 : public Base { public: // Change argument list: int f(int) const { cout << "Derived4::f()\n"; return 4; } }; main() { string s("hello"); Derived1 d1; int x = d1.f();//Here Both overloaded functions are called as not a single d1.f(s); //overloaded function is re declared in derived class Derived2 d2; x = d2.f(); //! d2.f(s); // string version of the overloaded function is hidden as only one is defined in Derived2 class Derived3 d3; //! x = d3.f(); // Both the overloaded functions are hidden as signature i.e return type is changed in class Derived3 Derived4 d4; //! x = d4.f(); // Both the overloaded functions are hidden as signature i.e argument is changed in class Derived4 x = d4.f(1); } In general, we can say that anytime you redefine an overloaded function name from the base class, all the other versions are automatically hidden in the new class. Same scenario will happen in normal overloaded function i.e if functions are not virtual also. With Dynamic Object :-
Here the same example mentioned in the section (2) will not throw error and do call the base call overloaded function.
class B
{
public:
virtual void display(int i) //Base class function
{ ................. }
virtual void display() //Base class function with different signature
{ ................... }
};
class D: public B
{
public:
virtual void display() //Derived class function
{ ................... }
};
main()
{
B *bptr = new D;
bptr->display(1);//This ll call the Base class overloaded function.
}
Normal behavior happen with pointer pointing to derived class object.
SEEMS TO BE AWESOME CODING. PROBABLY I NEED TO REVISE KARNITHKAR BEFORE UNDERSTANDING EVEN A SINGLE WORD OF IT...)))
ReplyDelete