The difference and usage essentials of ordinary member function, virtual function and pure virtual function in C + + _c language

Source: Internet
Author: User
Tags class definition

Normal member functions are statically compiled, with no run-time polymorphism, only based on the pointer or reference to the "literal" class object, call your own ordinary function; Virtual functions are defined in the base class for overload and polymorphism, even if the definition is null; a pure virtual function is a virtual function declared in a base class, which can be defined in the base class. and the derived class must define its own implementation method.

Suppose we have three classes of person, Teacher, student the relationships between them are as follows:



Diagram of a class

Normal member function

"Demo1"
Based on this class diagram, we have the following code implementation

#ifndef __objedt_h__ #define __OBJEDT_H__ #include <string> #include <iostream> class Person {Public:per Son (const string& name, int age): M_name (name), M_age (age) {} void Showinfo () {cout << "name:" << m
 _name << Endl;
 cout << "Age:" << m_age << Endl; } protected:string M_name; name int m_age;


AGE};  Class Teacher:public Person {public:teacher (const string& name, int age, const string& title): Person (name,
 Age), M_title (title) {} void Showinfo () {cout << "name:" << m_name << Endl;
 cout << "Age:" << m_age << Endl;
 cout << "title:" << m_title << Endl; } private:string M_title;

Title}; Class Student:public Person {public:student (const string& name, int age, int Studyid): Person (name, age), M_stu
 Dyid (Studyid) {} void Showinfo () {cout << "name:" << m_name << Endl; cout << "Age:" << m_age <<Endl
 cout << "School No.:" << M_studyid << Endl;  } Private:int M_studyid;

Learning number};
 #endif//__objedt_h__

Test code:

void Test ()
{
 person* Pperson = new Person ("John");
 teacher* pteacher = new Teacher ("Dick", 35, "Associate Professor");
 student* pstudent = new Student ("Harry", 20151653);
 Pperson->showinfo ();
 cout << Endl;
 Pteacher->showinfo ();
 cout << Endl;
 Pstudent->showinfo ();
 cout << Endl;
 Delete Pperson;
 Delete Pteacher;
 Delete pstudent;
}

Results:

Name: John
Age: 22

Name: Dick
Age: 35
Title: Associate Professor

Name: Harry
Age: 18
School Number: 20151653
Description
The showinfo here is a normal function. Pperson, Pteacher, and pstudent three objects invoke showinfo to display their own information separately.
We know that a pointer to a parent class is an object that can point to a subclass. Let's change the above test code a little bit:
"Demo2"

void Test ()
{
 person* Pperson = new Person ("John");
 person* pteacher = new Teacher ("Dick", 35, "Associate Professor");
 person* pstudent = new Student ("Harry", 20151653);
 Pperson->showinfo ();
 cout << Endl;
 Pteacher->showinfo ();
 cout << Endl;
 Pstudent->showinfo ();
 cout << Endl;
 Delete Pperson;
 Delete Pteacher;
 Delete pstudent;
}

Results:

Name: John
Age: 22

Name: Dick
Age: 35

Name: Harry
Age: 18
At this point, pteacher and pstudent only output the name and age, and there is no output subclass of the characteristics (title and school number). This should not be the result you expect, you may expect pteacher and pstudent to output the complete information of the teacher and students, then you need to use a virtual function.

virtual function

We change the Showinfo member in person to a virtual function (in front plus virtual), and the code is as follows:
"Demo3"

Class person
{public
: Person
 (const string& name, int age): M_name (name), M_age (age)
 {
 }

 virtual void Showinfo ()
 {
 cout << "Name:" << m_name << Endl;
 cout << "Age:" << m_age << Endl;
 }

Protected:
 string m_name;//name
 int m_age;//Age
};

In executing the test code in "Demo2" above, we get the result we thought:

Name: John
Age: 22

Name: Dick
Age: 35
Title: Associate Professor

Name: Harry
Age: 18
School Number: 20151653
Essentials of virtual function usage:

    1. the declaration of a virtual function:virtual return_type functionname (ARGS parameter list);
    2. Virtual function: The polymorphism in reality C + +, dynamic binding (the parent pointer can point to the object of the subclass) until the runtime knows which version to call (which class definition) function;
    3. We need to define the virtual function;
    4. Once the member function of the parent class declares virtual, the function of its subclass is a virtual function, whether declared as virtual or not;
    5. If a virtual function uses a default argument, the default arguments defined by the parent class and the subclass are best consistent.

"Demo4": For the 4th note:

Class person
{public
: Person
 (const string& name, int age): M_name (name), M_age (age)
 {
 }

 virtual void Showinfo ()
 {
 cout << "Name:" << m_name << Endl;
 cout << "Age:" << m_age << Endl;
 }

 String GetName (); Correct, if the normal function is not used, you can only declare that the
 virtual int getage () is not defined;///error, the virtual function must have a definition, even an empty implementation, because the compiler cannot determine which function to use


protected:
 string m_name//name
 int m_age;//Age
};

"Demo5": For the 5th Description:
The design of our class is defined below.

Class person
{public
:
 virtual void setage (int age = 0)
 {
 m_age = age;
 }
 //... Omitted
};


Class Teacher:public person
{public
:
 virtual void setage (int age = 1)
 {
 m_age = age;
 }

 //... Omitted
};

Class Student:public person
{public
:
 virtual void setage (int age = 2)
 {
 m_age = age;
 }

 //... Omitted
};

Test 1:

void Test ()
{
 person* Pperson = new Person ("John");
 teacher* pteacher = new Teacher ("Dick", 35, "Associate Professor");
 student* pstudent = new Student ("Harry", 20151653);
 Pperson->setage ();
 Pteacher->setage ();
 Pstudent->setage ();

 Pperson->showinfo ();
 cout << Endl;
 Pteacher->showinfo ();
 cout << Endl;
 Pstudent->showinfo ();
 cout << Endl;
 Delete Pperson;
 Delete Pteacher;
 Delete pstudent;
}

Results:

Name: John
Age: 0

Name: Dick
Age: 1
Title: Associate Professor

Name: Harry
Age: 2
School Number: 20151653
Test 2:

void Test ()
{
 person* Pperson = new Person ("John");
 person* pteacher = new Teacher ("Dick", 35, "Associate Professor");
 person* pstudent = new Student ("Harry", 20151653);
 Pperson->setage ();
 Pteacher->setage ();
 Pstudent->setage ();

 Pperson->showinfo ();
 cout << Endl;
 Pteacher->showinfo ();
 cout << Endl;
 Pstudent->showinfo ();
 cout << Endl;
 Delete Pperson;
 Delete Pteacher;
 Delete pstudent;
}

Results:

Name: John
Age: 0

Name: Dick
Age: 0
Title: Associate Professor

Name: Harry
Age: 0
School Number: 20151653
pure virtual function

In the above example, we assume that all people have to work, but different people work in different ways. So we're going to force the requirement that the subclasses that inherit from the person have a working method, which requires pure virtual functions. The definition is as follows:
"Demo6"

Class person
{public
:
 //... Omit
 virtual void DoWork () = 0;
 //... Omitted
};

But at this point we compile

person* Pperson = new Person ("John", 22);


This sentence will be an error: Error C2259: ' person ': cannot instantiate abstract class
This is because we do not implement the DoWork method for person, and the class containing pure virtual functions is an abstract class and the abstract class cannot be instantiated.

So we implement it in subclasses as follows:
"Demo7"

Class Teacher:public person
{public
:
 //... Omit
 virtual void DoWork ()
 {
 cout << teach ... "<< Endl
 }
 //... Omitted
};

Class Student:public person
{public
:
 //... Omit
 virtual void DoWork ()
 {
 cout << "Learning ..." << Endl
 }
 //... Omitted
};

No Use DoWork method:

void Test ()
{
 person* pteacher = new Teacher ("Dick", 35, "Associate Professor");
 person* pstudent = new Student ("Harry", 20151653);

 Pteacher->dowork ();
 cout << Endl;
 Pstudent->dowork ();
 cout << Endl;

 Delete Pteacher;
 Delete pstudent;
}

Results:

Teaching...

Learn...
Essentials of pure virtual function usage:

    1. Declaration mode of pure virtual function: Virtual return_type functionname (ARGS parameter list) = 0;
    2. A class that contains pure virtual functions is an abstract class, and an abstract class cannot be instantiated.
    3. Abstract classes that contain pure virtual functions are often used as external interfaces to illustrate what functions this class has, without specific implementations, and the implementation of the matrix is done by subclasses.

Through the above to the ordinary member function, the virtual function as well as the pure virtual function introduction, hoped can be helpful to everybody.

Related Article

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.