C + + abstract class detailed

Source: Internet
Author: User

Before we introduce the abstract class, let's introduce the pure virtual function.

1. Pure virtual function

Only declarations in the base class are not defined for virtual functions, but are implemented in derived classes. This virtual function is called a pure virtual function. A normal function is not compiled if it simply gives its declaration and does not implement its function body. Pure virtual functions do not have function bodies.

Pure virtual functions need to be added after a declaration = 0;

Class < base class name >

{

Virtual < type >< function name > (< parameter table >) = 0; ......

};

2. Abstract class

Classes that contain pure virtual functions are called abstract classes. An abstract class can only be the base class for a derived class, you cannot define an object, but you can define a pointer. After the derived class implements the pure virtual function, it defines a pointer to the abstract class object and points to or references the subclass object.

1 when the pure virtual function is defined, the realization part of the virtual function cannot be defined.

2 This function cannot be invoked until the pure virtual function has been redefined.

The only purpose of an abstract class is to provide a base class for derived classes, the role of pure virtual functions as the basis for member functions in derived classes, and to implement dynamic polymorphism. A derived class that inherits from an abstract class cannot implement all pure virtual functions in the base class, and the derived class becomes an abstract class. Because it inherits the abstract function of the base class, as long as the class containing pure virtual functions is an abstract class. A pure virtual function has defined the declaration of this method in an abstract class, which can only be implemented in other classes.

3. The difference between interface and abstract class

1 in C + + we generally say that the interface, representing the external supply of methods, provided to external calls. Communication is the external and internal bridge. is also provided in the form of a class, but generally this class has only member functions and does not have data members;

2 Abstract classes can contain both data members and methods.

an instance of an abstract class

1. Abstract class IShape as base class: only header file, no implementation file

#ifndef Shape_h

#define Shape_h

#include

Using Std::string;

Interface

Class IShape

{

Public

Virtual float Getarea () = 0; Pure virtual function, get area

Virtual String getName () = 0; Pure virtual function, returning the name of the graphic

};

#endif

2. Derived class Circle class inherits from abstract class IShape:

Circle.h header file:

#ifndef Circle_h

#define Circle_h

#include "Shape.h"

Class Ccircle:public IShape//Public inheritance from IShape class

{

Public

Ccircle (float radius); Constructors

Public

Virtual float getarea (); Implementation declaration to implement two basic classes of functions, the Declaration of the need to add the virtual keyword, the implementation of the time without the need to add virtual keyword.

Virtual string getName ();

Private

float M_fradius; A derived class can have its own members

};

#endif

Circle.cpp Implementation file:

#include "Circle.h"

Ccircle::ccircle (float radius)

: M_fradius (RADIUS)////using the constructor's initialization list to initialize the

{

}

float Ccircle::getarea ()//implement functions that implement two base classes

Virtual string getName ();

{

Return 3.14 * M_fradius * M_fradius;

}

String Ccircle::getname ()

{

return "Ccircle";

}

3. Derived class Rect class inherits from abstract class IShape:

Rect.h header file:

#ifndef Rect_h

#define Rect_h

#include "shape.h"

Class Crect:public IShape

{

Public

CRect (float nwidth, float nheight);

Public

Virtual float getarea ();

Virtual string getName ();

Private

float M_fwidth; The rectangle class has its own two properties, and the width and height

float M_fheight;

};

Rect.cpp Implementation file:

#include "Rect.h"

Crect::crect (float fwidth, float fheight)

: M_fwidth (Fwidth), M_fheight (Fheight)

{

}

Float Crect::getarea ()

{

return m_fwidth * m_fheight;

}

String Crect::getname ()

{

return "CRect";

}

4. Test file main.cpp:

#include

#include "Rect.h"

#include "Circle.h"

using namespace Std;

int main ()

{

ishape* pshape = NULL; Defines a pointer to an abstract class, noting that an abstract class cannot define an object but can define a pointer

Pshape = new Ccircle (20.2); The base class pointer points to the object of the derived class

Cout<getname () << "" <getarea () <<endl;

Delete Pshape; The memory of the Ccirle object is freed, but the pointer is not gone, and it is now a wild pointer that we must assign to it before we use it

Pshape = new CRect (20, 10); The base class pointer points to the object of the derived class

Cout<getname () << "" <getarea () <<endl;

return 0;

}

The results are as follows: As you can see, we call the same function using a pointer to the parent class, and we call the corresponding function of the two derived classes, which determines the method that is invoked depending on the type that the pointer is pointing to. Even if we need to add a few new classes later, we still call this method, which is the great fascination of polymorphism.



Original link: http://www.maiziedu.com/wiki/cplus/forms/

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.