C + + Review notes-polymorphism

Source: Internet
Author: User

Polymorphic

1 Concepts

Object-oriented system polymorphism refers to different objects that perform different operations when they receive the same message

Compile-time polymorphism

Polymorphism

Run-time polymorphism

Compile-time polymorphism is primarily implemented through function overloading and operator overloading .

Runtime polymorphism is mainly achieved by virtual functions .

Note

In C + +, the object pointer of a base class can point to its public-derived object, but when it points to a public-derived class object, the

It can only access members that are inherited from a base class in a derived class and cannot access members defined in a public derived class

For example

#include <iostream.h>//Example of 6.1-1 virtual function

Class Base {

int A, B;

Public

Base (int x,int y) {a=x; B=y; }

void Show ()

{cout<< "Call the base class base's show function \ n";

cout<< "a=" <<a<< "b=" <<b<<endl; }   };

Class Dirive:public Base {

int C;

Public

dirive (int x,int y,int z): Base (x, y) {c=z;}

void Show () {cout<< "calls the show function of the derived class dirive \ n";

cout<< "c=" <<c<<endl; }  };

void Main ()

{base MB (50,50), *MP; dirive MC (10,20,30);

mp=&mb; Mp->show ();

mp=&mc; Mp->show (); }

The running result is

Call the show function of base class base

A=50 b=50

Call the show function of base class base

a=10 b=20

2 Virtual functions

1) function and definition of virtual functions

1. The role of virtual function

A virtual function is a function that is described in the base class by the keyword and redefined in a derived class

For example

#include <iostream.h>//Example 5.2-1

Class Base {

int A, B;

Public

Base (int x,int y) {a=x; b=y;}

virtual void Show () {cout<< "Call the base class base's show function \ n";//

In the base class, show () is defined as a virtual function

cout<< "a=" <<a<< "b=" <<b<<endl; }  };

Class Dirive:public Base {

int C;

public:dirive (int x,int y,int z): Base (x, y) {c=z;}

void Show () {cout<< "calls the show function of the derived class dirive \ n"; cout<< "c=" <<c<<endl; }

};//

In a derived class, redefine the virtual function show ()

void Main ()

{base MB (50,50), *MP;

dirive MC (10,20,30);//

Call the show () function of base class base

mp=&mb;  Mp->show (); //

mp=&mc; Mp->show ()//

Call the show () function of the derived class Dirive

;}

3 Virtual destructor

#include <iostream>//Example 6.6-1 the use of virtual destructors.

using namespace Std;

Class b{

Public

Virtual ~b ()

{cout<< "call the destructor for base class B \ n";}

};

Class D:public b{

Public

~d ()

{cout<< "call the destructor for the derived class D \ n";}

};

int main ()

{B *p; Defines a pointer variable p that points to base class B

p= new D;

Delete p;

return 0; }

1 declaring a destructor for a base class as a virtual destructor

2 The virtual destructor is used, and the program performs dynamic linking, which realizes the polymorphism of operation.

(1) The virtual destructor has no type and no parameters .

(2) If the destructor of the base class is defined as a virtual function, the destructors of all derived classes derived from that base class are automatically created as virtual functions.

(3) in C + +, a fictitious constructor cannot be declared, but a virtual destructor can be declared.

4 pure virtual functions and abstract classes

1 The base class itself does not require this virtual function, but instead provides a common interface for derived classes so that derived classes can redefine virtual functions as needed

2

The characteristics of pure virtual function:

① pure virtual function has no function body ;

② the Last "= 0" does not mean that the return value of the function is 0, it only plays a formal role, telling the compilation system "This is pure virtual function";

③ pure virtual function does not have function, cannot be called

Example:

#include <iostream.h>

Class Circle {

Protected

int R;

Public

void Setr (int x) {r=x;}

virtual void Show () = 0;

};

Class Area:public circle{

Public

void Show ()

{cout<< "The area of this circle is:" <<3.14*r*r<<endl;}};

Class Perimeter:public circle{

Public

void Show ()

{cout<< "The circumference of this circle is:" <<2*3.14*r<<endl;}};

void Main ()

{Circle *ptr;

Area Ob1;

Perimeter Ob2;

OB1.SETR (10);

OB2.SETR (10);

ptr=&ob1; Ptr->show ();

ptr=&ob2; Ptr->show (); }

5 abstract class

1 function abstract The function of a class is as a common base class of a class family, and the related derived class is derived from this base class.

2 Some rules for using abstract classes

(1) because the abstract class contains at least one pure virtual function that does not have a function defined,

Abstract classes can therefore only be used as base classes for other classes, and abstract class objects cannot be established.

(2) It is not allowed to derive abstract classes from specific classes . The so-called concrete class is the ordinary class that does not contain pure virtual function;

(3) An abstract class cannot be used as the parameter type of a function, the return type of a function, or the type of an explicit conversion ;

(4) You can declare a pointer or reference to an abstract class that can point to its derived class for polymorphism.

(5) in a derived class, if the pure virtual function of the base class is not redefined, the function is still a pure virtual function in the derived class, and the derived class is still an abstract class.

For example:

#include <iostream>

using namespace Std;

Class figure{

Protected

Double R;

Public

Figure (Double x) {r=x;}

virtual void area () = 0; Pure virtual function

virtual void perimeter () = 0; Pure virtual function

};

Class Circle:public figure{

Public

Circle (Double x): Figure (x) {}

void Area ()

{cout<< "The area of the circle is" <<3.14*r*r<<endl; }

void Perimeter ()

{cout<< "Circumference of the circle is";

cout<<2*3.14*r<<endl; }

};

Class In_square:public figure{

Public

In_square (Double x): Figure (x) {}

void Area ()

{cout<< "The area of the inner square of the circle is";

Cout<<2*r*r<<endl;}

void Perimeter ()

{cout<< "The circumference of the inner square of a circle is";

cout<<4*1.414*r<<endl; }

};

Class Ex_square:public figure{

Public

Ex_square (Double x): Figure (x) {}

void Area ()

{cout<< "The area of the circle-circumscribed square is";

Cout<<4*r*r<<endl;}

void Perimeter ()

{cout<< "Circumference of the circle-circumscribed square is";

Cout<<8*r <<endl; }

};

int main ()

{

Figure *ptr; Defines a pointer to an abstract class figure PTR

Circle Ob1 (5); Defines the object of class circle Ob1

In_square OB2 (5); Defines a class In_square object Ob2

Ex_square OB3 (5); Defines a class Ex_square object ob3

ptr=&ob1; Ptr->area (); To find the area of a circle

Ptr->perimeter (); To find the circumference of a circle

ptr=&ob2; Ptr->area (); To find the area of a square within a circle

Ptr->perimeter (); To find the circumference of a square within a circle

ptr=&ob3; Ptr->area (); To find the area of a circle-circumscribed square

Ptr->perimeter (); To find the circumference of a circle and an circumscribed square

return 0; }

C + + Review notes-polymorphism

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.