C + + Basic (vii) function overlay, virtual function, pure virtual function comparison

Source: Internet
Author: User

1. Function overrides

ClassA, ClassB, where ClassB inherited ClassA

The class definition is as follows:


#ifndef _classa_h#define _classa_h#include <iostream>using namespace Std;class classa{public:classa (void); ~ ClassA (void); void method ();}; #endif

#include "stdafx.h" #include "ClassA.h" Classa::classa (void) {}classa::~classa (void) {}void classa::method (void) {cout << "I am from Class Classa:methoda" <<ENDL;}


#ifndef _class_b#define _class_b#include "ClassA.h" CLASS classb:public classa{public:classb (void); ~classb (void); void method ();}; #endif

#include "stdafx.h" #include "ClassB.h" classb::classb (void) {}classb::~classb (void) {}void classb::method () {cout< < "I am from Class Classb:methoda" <<ENDL;}


2. Virtual function overrides

Virtualclassa, VIRTUALCLASSB, where Virtualclassb inherited Virtualclassa

The class definition is as follows:

#ifndef _virtual_class_a#define _virtual_class_a#include <iostream>using namespace Std;class VirtualClassA{ Public:virtualclassa (void); ~virtualclassa (void); virtual void method ();}; #endif

#include "stdafx.h" #include "VirtualClassA.h" Virtualclassa::virtualclassa (void) {}virtualclassa::~virtualclassa ( void) {}void virtualclassa::method () {cout<< "I am from Class Virtualclassa:method" <<ENDL;}

#ifndef _virtual_class_b#define _virtual_class_b#include "VirtualClassA.h" CLASS virtualclassb:public virtualclassa{ Public:virtualclassb (void); ~virtualclassb (void); virtual void method ();}; #endif


#include "stdafx.h" #include "VirtualClassB.h" virtualclassb::virtualclassb (void) {}VIRTUALCLASSB::~VIRTUALCLASSB ( void) {}void  virtualclassb::method () {cout<< "I am from Class Virtualclassb:method" <<ENDL;}


3. Pure virtual function overrides

Purevirtualclassa, PUREVIRTUALCLASSB, where Purevirtualclassb inherited Purevirtualclassa

The class definition is as follows:


#ifndef _pure_virtual_class_a#define _pure_virtual_class_a#include <iostream>using namespace Std;class Purevirtualclassa{public:purevirtualclassa (void); ~purevirtualclassa (void); virtual void method () =0;/* Typically, The pure virtual function of the abstract base class is not implemented; Of course, it can also provide implementation for pure virtual function; */}; #endif

#include "stdafx.h" #include "PureVirtualClassA.h" Purevirtualclassa::P Urevirtualclassa (void) {}purevirtualclassa:: ~purevirtualclassa (void) {}

#ifndef _pure_virtual_class_b#define _pure_virtual_class_b#include "Purevirtualclassa.h" CLASS PUREVIRTUALCLASSB: Public purevirtualclassa{public:purevirtualclassb (void); ~purevirtualclassb (void); virtual void method ();}; #endif

#include "stdafx.h" #include "PureVirtualClassB.h" PUREVIRTUALCLASSB::P urevirtualclassb (void) {}PUREVIRTUALCLASSB:: ~purevirtualclassb (void) {}void purevirtualclassb::method (void) {cout<< "I am from class Purevirtualclassb: Method "<<ENDL;}

The test code is as follows:

VirtualMethodTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include "ClassA.h" #include "ClassB.h" #include "VirtualClassA.h" #include "VirtualClassB.h" # Include "PureVirtualClassA.h" #include "PureVirtualClassB.h" #include <iostream>using namespace Std;int _tmain ( int argc, _tchar* argv[]) {cout<< "--------Overlay---------" <<endl; ClassA *pclassa=new ClassA ();p classa->method ();d elete (Pclassa); ClassB *pclassb=new ClassB ();p classb->method ();//pclassb->classa::method ();--You can call the method of the parent class implicitly//(*PCLASSB). Classa::method ();d elete (PCLASSB); ClassA *pclassb2=new ClassB ();p classb2->method ();d elete (pClassB2);cout<< "-------polymorphic----------" << Endl Virtualclassa *pvirtualclassa=new Virtualclassa ();p virtualclassa->method ();d elete (Pvirtualclassa); VIRTUALCLASSB *pvirtualclassb=new virtualclassb ();p virtualclassb->method ();d elete (PVIRTUALCLASSB); Virtualclassa *pvirtualclassb2=new virtualclassb ();p virtualclassb2->method ();d elete (pVirtualClassB2);cout< < "-------Pure virtual function----------"<<endl;//purevirtualclassa *ppurevirtualclassa=new Purevirtualclassa ();//ppurevirtualclassa->method ( );//delete (Ppurevirtualclassa); PUREVIRTUALCLASSB *ppurevirtualclassb=new purevirtualclassb ();pP urevirtualclassb->method ();d elete ( PPUREVIRTUALCLASSB); Purevirtualclassa *ppurevirtualclassb2=new purevirtualclassb ();pP urevirtualclassb2->method ();d elete ( pPureVirtualClassB2); return 0;}

The printing results are as follows:




Function overlay can not achieve polymorphism;

virtual function and pure virtual function can realize polymorphism.

Classes that contain pure virtual functions cannot be instantiated.


Code Address: http://download.csdn.net/detail/zz7zz7zz/8096231




C + + Basic (vii) function overlay, virtual function, pure virtual function comparison

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.