Examples show virtual functions and virtual base classes in C++ programming

  • 2020-05-09 18:52:52
  • OfStack

Virtual functions
1.


#include "stdafx.h"

#include <iostream>

using namespace std;

class B0// The base class B0 The statement 
{
public:
 void display(){cout<<"B0::display()"<<endl;}// Public member function 
};

class B1: public B0// Public derived class B1 The statement 
{
public:
 void display(){cout<<"B1::display()"<<endl;}// Public member function 
};

class D1: public B1// Public derived class D1 The statement 
{
public:
 void display(){cout<<"D1::display()"<<endl;}// Public member function 
};

void fun(B0 *ptr)// Common function 
{// The argument is a pointer to the base class object 
 ptr->display();//" Pointer to the object -> Member name "
}
void main()// The main function 
{
 B0 b0;// Statement of the base class B0 Class object 
 B1 b1;// The statement B1 Class object 
 D1 d1;// The statement D1 Class object 
 B0 *p;// The statement B0 Class pointer 
 p=&b0;//B0 Class pointer to B0 Class object, 
 fun(p);
 p=&b1;//B0 Class pointer to B1 Class object, a pointer to a base class can also point to a derived class. 
 fun(p);
 p=&d1;//B0 Class pointer to D1 Class object, a pointer to a base class can also point to a derived class. 
 fun(p);
}

Output results:


B0::display()
B0::display()
B0::display()
Press any key to continue

Examples of type compatibility rules


#include "stdafx.h"

#include <iostream>

using namespace std;

class B0// The base class B0 The statement 
{
public:
 virtual void display(){cout<<"B0::display()"<<endl;}// Public member function 
};

class B1: public B0// Public derived class B1 The statement 
{
public:
 void display(){cout<<"B1::display()"<<endl;}// Public member function 
};

class D1: public B1// Public derived class D1 The statement 
{
public:
 void display(){cout<<"D1::display()"<<endl;}// Public member function 
};

void fun(B0 *ptr)// Common function 
{// The argument is a pointer to the base class object 
 ptr->display();//" Pointer to the object -> Member name "
}
void main()// The main function 
{
 B0 b0;// Statement of the base class B0 Class object 
 B1 b1;// The statement B1 Class object 
 D1 d1;// The statement D1 Class object 
 B0 *p;// The statement B0 Class pointer 
 p=&b0;//B0 Class pointer to B0 Class object, 
 fun(p);
 p=&b1;//B0 Class pointer to B1 Class object, a pointer to a base class can also point to a derived class. 
 fun(p);
 p=&d1;//B0 Class pointer to D1 Class object, a pointer to a base class can also point to a derived class. 
 fun(p);
}

Output results:


B0::display()
B1::display()
D1::display()
Press any key to continue 

2.

Virtual function is a function assumed to achieve a certain function. Virtual function can only be a member function in the class, not a static member. The keyword virtual is used to indicate that this function is a virtual function in the class. Virtual functions are created to achieve object-oriented polymorphism, using virtual functions and polymorphism

Performance enough to simplify the code length, support a simpler order, easy to debug the program, maintenance.
Definition method of virtual function:


class A
{
  public:
    virtual void fun();  //define virtual function
};
void A::fun() { ... }      //member function describe

A virtual function is defined above and then described outside the class.

In class inheritance, when a virtual function is declared in a base class, even if no virtual function is declared in a derived class, it will be declared in subsequent inheritance structures

Virtual functions, of course if there is multiple inheritance, it is still recommended to explicitly declare each virtual function in each derived class.

To illustrate the use of virtual functions in derived classes, I'll write a snippet of code as an example:


#include "stdio"
class cbase
{
public:
 virtual void vfoo()
 {
 printf("vfoo from cbase/n");
 };
 void foo()
 {
 printf("foo from cbase/n");
 }
};

class cderivd : public cbase
{
public:
 virtual void vfoo()
 {
 printf("vfoo from cderivd/n");
 };
 void foo()
 {
 printf("foo from cderivd/n");
 };
};

int main(int argc, char* argv[])
{
 cbase* pbase = new cderivd();
 pbase->foo(); // A virtual function , Decide which to call based on the pointer type foo, This example pointer is of type cbase, So what's called is cbase::foo()
 pbase->vfoo(); // Virtual functions , The call is for a derived class vfoo
 delete pbase;

 cderivd* pd = new cderivd();
 pd->foo();    // A virtual function , This example pointer is of type cderivd*, So call cderivd::foo();
 pd->vfoo();
 delete pd;

 cderivd d;
 d.foo();
 d.vfoo();
 ((cbase)d).foo(); // will d Force cut into cbase, Whatever is called at this point foo or vfoo Will be base the 
 ((cbase)d).vfoo();

 getchar();
 return 0;
}

The program was compiled and passed under DevCPP, and output:


foo from cbase
vfoo from cderivd
foo from cderivd
vfoo from cderivd
foo from cderivd
vfoo from cderivd
foo from cbase
vfoo from cbase

Virtual base class


#include "stdafx.h"

#include<iostream>
#include<string>

using namespace std;

class person{// Statement of the base class  
protected:
 int age;
 char sex;
 string name;
public:
 person(int a,char s,string nam){
 age=a;
 sex=s;
 name=nam;
 }
};
class teacher:virtual public person
{
protected:
 string title;
public:
 teacher(int a,char s,string nam,string t):person(a,s,nam){
 title=t;
 }
};
class student:virtual public person
{
protected:
 float score;
public:
 student(int a,char s,string nam,float sc):person(a,s,nam){
 score=sc;
 }
};
class graduate:public teacher,public student
{
protected:
 float wdge;
public:
 graduate(int a,char s,string nam,string t,float sc,float wd):person(a,s,nam),teacher(a,s,nam,t),student(a,s,nam,sc){
 wdge=wd;
 }
 void show(){
 cout<<name<<endl;
 cout<<age<<endl;
 cout<<sex<<endl;
 cout<<title<<endl;
 cout<<score<<endl;
 cout<<wdge<<endl;
 }
};
int main(){
 graduate gr(22,'f',"k;asdjf;daf","klsdaf",89.5,79.5);
 gr.show();
 return 0;
}

Output results:


k;asdjf;daf
22
f
klsdaf
89.5
79.5
Press any key to continue 


Related articles: