C + + virtual functions, static and dynamic-linked, abstract classes

Source: Internet
Author: User



C + + virtual functions, static and dynamic-linked, abstract classes
#include <iostream>
#include <string>

using namespace Std;

class people
{
Private
String name;
int age;
Public
People (string name_, int age_): Name (name_), age (Age_) {}
People () {cout << "base class default constructor" << Endl;}
~people () {cout << "base class destructor" << Endl;}//Destructors not declared as virtual functions
Virtual ~people () {cout << "base class destructor" << Endl;}//destructor declared as virtual function
virtual void Show ()
{
cout << name << "<< age << Endl;
}
};

Class Student:public People
{
Private
int grade;
Public
Student (string name_, int age_, int grade_):P eople (name_, Age_), Grade (Grade_) {}
Student () {cout << "derived class default constructor" << Endl;}
Virtual ~student () {cout << "derived class destructor" << Endl;}//When a base class function is declared as a virtual function, the derived class function does not add the virtual keyword, but it is best to add that this function is a virtual function.
virtual void Show ()
{
cout << grade << Endl;
}
};

Class Animal
{
Private
String name;
Public
virtual void shout () = 0;
Virtual ~animal () {}
};

Class Dog:public Animal
{
Public
virtual void shout ()
{
cout << "Barking" << Endl;
}
};

Class Mao:public Animal
{
Public
virtual void shout ()
{
cout << "Meow" << Endl;
}
};

int main ()
{
The class variable is declared normally, and when the variable expires, the derived class destructor is called before the base class destructor is called.
Student s1{"Linukey", 20, 100};
S1.show ();

When a pointer to a base class is used to open the space of a derived class, if the destructor is not declared as a virtual function, only a destructor of the pointer type is called, causing a memory leak
When the destructor of a base class is declared as a virtual function, when the variable expires, the destructor of the derived class is called before the fictional function of the base class is called
people* p = new student{"Linukey", 20, 100};
P->show ();
Delete p;

When a derived class function overrides a base class method, the base class function is not declared as a virtual function, and the derived class object that is opened with the base class pointer is the method that invokes the pointer type class when the method is called
When a base class function is declared as a virtual function, the method that actually opens the memory type class is called
people* p = new student{"Linukey", 20, 100};
P->show ();
Delete p;


If a derived class needs to override a method of a base class, you need to declare the method of the derived class as a virtual function, so that when the pointer to the base class points to a variable of the derived class, when the method is called through the pointer, there is no error calling the base class method.
Student s2{"Linukey", 20, 100};
people* p = &s2;
P->show ();

Up-casting and down-casting
Student S1;
people* p = &s1; Up casting
People p1;
student* s2 = (student*) &p1; Cast down

If the class needs to be inherited, the destructor for the base class should be declared as a virtual function, thus avoiding a memory leak to some extent
If a method in a base class needs to be overridden in a derived class, the method in the base class should be declared as a virtual function and should not be declared as a virtual function if the methods in the base class do not need to be overridden, otherwise it will increase memory overhead and reduce efficiency

/*
function linking: The function call in the source code is interpreted as executing a specific function code block is called a function name of the binder;
Early linking/static linking: The compilation process is known as the early-riser linking;
Late linking/dynamic linking: Because of the virtual function, we are not able to confirm the type of the specific object during compilation, so we use dynamic linking for virtual function
*/
/*
virtual function Table:
Dynamically-linked classes add a hidden member to an object, and a pointer to an array of function addresses is stored in the hidden member, which is called the virtual function table;
Both the base class and the derived class contain a pointer to an array of their function addresses, and if the function of the base class is declared as a virtual function and there is no override in the derived class, the function address in the derived class's virtual function table remains the base class.
If a derived class is overridden, the function address in the virtual function table of the derived class is the address of the new function that is overridden
*/

/*
The abstract class, like its name, is a base class that is abstracted from the connections of several derived classes, which can inherit other abstract classes. Some methods cannot give a display implementation because of the relationships between several derived classes,
So we can only define a method that cannot be implemented, and we use pure virtual functions to define this method. A method defined by a pure virtual function can be implemented in a base class, but it must be implemented in a derived class.
Abstract classes cannot have instantiated objects, but they can open objects of derived classes through pointers to abstract classes. Abstract classes can contain common methods, but must contain at least one pure virtual method
*/

Just as the dog and Mao two classes, although all for animals, but the call is different, we can not give specific calls in the base class, so we define an abstract class, give a pure virtual function of the call interface, from the derived class to implement the specific method
Dog D;
D.shout ();
Mao m;
M.shout ();

Derived class objects can be opened by pointers to abstract classes
animal* a = new Dog;
A->shout ();

return 0;
}

C + + virtual functions, static and dynamic-linked, abstract classes

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.