Virtual functions in C ++

Source: Internet
Author: User

I finally had a holiday. What can I do? I thought of several blogs this morning and mentioned that I used the parent class pointer to access sub-class functions .. This morning, let's verify it.


[Cpp]
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Void name ()
{
Cout <"Base Class name" <endl;
}
 
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
 
Base * BaseClassPointer;
BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
// This is equivalent to converting the subclass to the parent class type, and then calling the name function of the parent class
Return 0;
}

# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Void name ()
{
Cout <"Base Class name" <endl;
}

};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal

Base * BaseClassPointer;
BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
// This is equivalent to converting the subclass to the parent class type, and then calling the name function of the parent class
Return 0;
}

Running result


[Cpp]
Initial Base Class
Initial Derived Class // initialize the Deerived Class
Derived1 Class name // call the name () of the Derived Class ()
Base Class name // when forced conversion occurs, the Base Class name () is called ()

Initial Base Class
Initial Derived Class // initialize the Deerived Class
Derived1 Class name // call the name () of the Derived Class ()
Base Class name // when forced conversion occurs, the Base Class name () is called ()
Without using a virtual function, use the pointer of the base class to access the function of the subclass. This is equivalent to forcibly converting objects of the Derived1 type to the Base type. Therefore, the function finally calls the name () function in the Base class.

Using a virtual function, you can use the pointer of the base class to access the function of the subclass. The Code is as follows:


[Cpp]
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Virtual void name () // implemented using virtual functions
{
Cout <"Base Class name" <endl;
}
Virtual ~ Base ()
{
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
~ Derived1 ()
{
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal
 
Base * BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
Return 0;
}

# Include <iostream>
Using namespace std;
Class Base
{
Public:
Base ()
{
Cout <"initial Base Class" <endl;
}
Virtual void name () // implemented using virtual functions
{
Cout <"Base Class name" <endl;
}
Virtual ~ Base ()
{
}
};
Class Derived1: public Base
{
Public:
Derived1 ()
{
Cout <"initial Derived Class" <endl;
}
Void name ()
{
Cout <"Derived1 Class name" <endl;
}
~ Derived1 ()
{
}
};
Int main ()
{
Derived1 Derived1Class;
Derived1Class. name ();
// Of course this is normal

Base * BaseClassPointer = (Base *) & Derived1Class;
BaseClassPointer-> name ();
Return 0;
}
Running result:


[Cpp]
Initial Base Class
Initial Derived Class // initialize two parts of the Derived Class
Derived1 Class name
Derived1 Class name // The name () of the Derived Class is called ()
 
Process returned 0 (0x0) execution time: 0.018 s
Press any key to continue.

Initial Base Class
Initial Derived Class // initialize two parts of the Derived Class
Derived1 Class name
Derived1 Class name // The name () of the Derived Class is called ()

Process returned 0 (0x0) execution time: 0.018 s
Press any key to continue.

 

In class inheritance, if a base class Pointer Points to a derived class, when the base class pointer is deleted, if it is not necessarily a virtual function, the derived part of the derived class cannot be destructed.

Therefore, if you design a class that may be a base class, it is best to set one of its destructor as a virtual function, otherwise it may cause memory leakage.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.