Small Test of C ++ polymorphism ~!

Source: Internet
Author: User

An important feature of C ++ object-oriented is polymorphism. The following Code demonstrates the polymorphism feature. Appendix:

The following is the program source code:

# Include <iostream>

Using namespace STD;

Class shape
{
// Question requirements
Public:
Virtual double area () = 0;
Virtual void printshapename () = 0;
};

// Declare circle class
Class circle: Public shape
{
Public:
Circle ();
Circle (int r );
Circle (const circle & CIR );
Public:
Virtual double area ();
Virtual void printshapename ();
PRIVATE:
Double _ R;
};

// Declare triangle class
Class triangle: Public shape
{
Public:
Triangle ();
Triangle (INT height, int width );
Triangle (const triangle & TRI );
Public:
Virtual double area ();
Virtual void printshapename ();
PRIVATE:
Double _ height;
Double _ width;
};

 

// Declare rectangle
Class rectangle: Public shape
{
Public:
Rectangle ();
Rectangle (INT length, int width );
Rectangle (const rectangle & TRI );
Public:
Virtual double area ();
Virtual void printshapename ();
PRIVATE:
Double _ length;
Double _ width;
};

// Declare Square
Class square: Public shape
{
Public:
Square ();
Square (INT length );
Square (const square & CIR );
Public:
Virtual double area ();
Virtual void printshapename ();
PRIVATE:
Double _ length;
};

 

 

// Class circle
Circle: Circle ()
: _ R (0.0)
{
// Empty
}

Circle: Circle (int r)
: _ R (r)
{
// Empty
}

Circle: Circle (const circle & CIR)
: _ R (CIR. _ r)
{
// Empty
}

Double Circle: Area ()
{
Return _ r * 3.14159;
}

Void circle: printshapename ()
{
Cout <"this is a circle" <Endl;
}

// Class triangle
Triangle: triangle ()
: _ Height (0.0)
, _Width (0.0)
{
// Empty
}

Triangle: triangle (INT height, int width)
: _ Height (height)
, _ Width (width)
{
// Empty
}

Triangle: triangle (const triangle & TRI)
: _ Height (TRI. _ height)
, _ Width (TRI. _ width)
{
// Empty
}

Double Triangle: Area ()
{
Return _ height * _ width/2;
}

Void triangle: printshapename ()
{
Cout <"this is a triangle" <Endl;
}

 

 

// Class rectangle
Rectangle: rectangle ()
: _ Length (0.0)
, _Width (0.0)
{
// Empty
}

Rectangle: rectangle (INT length, int width)
: _ Length (length)
, _ Width (width)
{
// Empty
}

Rectangle: rectangle (const rectangle & TRI)
: _ Length (TRI. _ length)
, _ Width (TRI. _ width)
{
// Empty
}

Double rectangle: Area ()
{
Return _ length * _ width;
}

Void rectangle: printshapename ()
{
Cout <"this is a rectangle" <Endl;
}

 

 

Class Trapezoidal: Public shape
{
Public:
Trapezoidal ();
Trapezoidal (INT upperlen, int lowerlen, int height );
Trapezoidal (const trapezoidal & TRI );
Public:
Virtual double area ();
Virtual void printshapename ();
PRIVATE:
Double _ upperlen;
Double _ lowerlen;
Double _ height;
};

 

Trapezoidal: trapezoidal ()
: _ Upperlen (0.0)
, _ Lowerlen (0.0)
, _ Height (0.0)
{
// Empty
}

Trapezoidal: trapezoidal (INT upperlen, int lowerlen, int height)
: _ Upperlen (upperlen)
, _ Lowerlen (lowerlen)
, _ Height (height)
{
// Empty
}

Trapezoidal: trapezoidal (const trapezoidal & TRI)
: _ Upperlen (TRI. _ upperlen)
, _ Lowerlen (TRI. _ lowerlen)
, _ Height (TRI. _ height)
{
// Empty
}

Double Trapezoidal: Area ()
{
Return (_ upperlen + _ lowerlen) * _ height/2;
}

Void Trapezoidal: printshapename ()
{
Cout <"this is a trapezoidal" <Endl;
}

// Class Square
Square: Square ()
: _ Length (0.0)
{
// Empty
}

Square: Square (INT length)
: _ Length (length)
{
// Empty
}

Square: Square (const square & CIR)
: _ Length (CIR. _ length)
{
// Empty
}

Double square: Area ()
{
Return _ length * _ length;
}

Void square: printshapename ()
{
Cout <"this is a square" <Endl;
}

 

Int main ()
{
Shape * pshape [5] = {new circle (5), New Triangle (), new rectangle (), new trapezoidal (, 5 ), new Square (7 )};
For (INT I = 0; I <5; ++ I)
{
Pshape [I]-> printshapename ();
Cout <"the area is:" <pshape [I]-> area () <Endl;
}
}

 

Engineering Environment: win7 + vs2008

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.