Case Study of C + + polymorphism

Source: Internet
Author: User

Recently in the study of C + + polymorphism in the application, which encountered a few questions and problems, but finally the results are indisputable, the following record my learning process to commemorate this point of knowledge.

First, it starts with a case, the main idea is this:

Set the common class of a polygon, and then set the various derived classes of multiple graphs, and finally through the base class, according to the application of polymorphism through the base class, to find out the area of the graph

So according to the knowledge that I learned, we began to roughly outline the original prototype such as the following:

#include
using namespace Std;

class Shape //Base
 {
Public:
Virtual double area () = 0;
 };

int Main ()
{
  Circle C (5);       The round parameter assignment
  Rectangle R (3,4);    //Rectangle parameter Assignment
  Triangle T (3,4);     Triangle Assignment
  Shape *p;     //sets a pointer to a base class
  p=&c;      //Call method   for rounding area;   area for Circle
  cout<<p->area () <<endl;
  p=&r;       //Call the rectangular area method to find the rectangular area
  cout<<p-> Area () <<endl;
  p=&t;       //Call the method of triangular area to find the area of the Triangle
  cout<< P->area () <<endl;
  system ("pause");
  return 0;
}

Then, add one of the derived classes, adding the effect, such as the following:

#include
using namespace Std;
Class Shape
{
Public
Virtual Double area () = 0;
};

Class Circle:public Shape
{
Public
Circle (Double R): Radius (r) {}
Virtual double area ()
{
return radius*radius*3.14159;
}
Private
Double radius;
};
Class Rectangle:public Shape
{
Public
Rectangle (double x,double y): x (x), Y (y) {}
Virtual double area ()
{
return x*y;
}
Private
Double x, y;
};
Class Triangle:public Shape
{
Public
Triangle (double x,double y): x (x), Y (y) {}
Virtual double area ()
{
return X*Y/2;
}
Private
Double x, y;
};
int main ()
{
Circle c (1);
Rectangle R (3,4);
Triangle t (3,4);
Shape *p;
p=&c;
Cout<<p->area () <<endl;
p=&r;
Cout<<p->area () <<endl;
p=&t;
Cout<<p->area () <<endl;
System ("pause");
return 0;
}

Execution Result:

The same truth, can also be written in this way, just the habits of each person is different, code such as the following:

#include
using namespace Std;
Class shape
{
Public
virtual void area () = 0;
};

Class Trangle:public shape
{
Protected
Double A, B;
Public
Trangle (double a,double b): A (a), B (b) {};
virtual void area ()
{
cout<<a*b*0.5<<endl;
}
};
Class Rectangle:public shape
{
Protected
Double A, B;
Public
Rectangle (double a,double b): A (a), B (b) {};
virtual void area ()
{
cout<<a*b<<endl;
}
};
Class Circle:public shape
{
Protected
Double A;
Public
Circle (Double A): A (a) {};
virtual voidArea ()
{
cout<<3.14*a*a<<endl;
}
};
int main ()
{
Shape *p;
Trangle t (n);
Rectangle R (N);
Circle C (2);
p=&t; Triangular area
P->area ();
p=&r;Rectangular Area
P->area ();
p=&c;Circular area
P->area ();
return 0;
}

One of the points to be aware of is that it must be written as a public derivation , or the default is a private derivation. Also in the process of writing code, must be careful not to write the English letter wrong, one of the letters is wrong, it took me a long time, the last to check out, knocking the code of the prophase most will encounter this situation, we need to do is, serious and serious.

Case Study of C + + 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.