13th Week "Project 2-pure virtual function in the shape class family"

Source: Internet
Author: User

Description of the problem:

Write a program. Defines the abstract base class shape, which derives 3 derived classes, Circle (circle), Rectangle (Rectangle), Triangle (triangle). Use the main () function such as the following. Find the area and the number of geometry defined.

int main () {    Circle C1 (12.6), C2 (4.9);//Create Circle Class object c1,c2 with radius    Rectangle R1 (4.5,8.4), R2 (5.0,2.5);// Establish the Rectangle class object R1,R2, the length of the rectangle, the width    Triangle T1 (4.5,8.4), T2 (3.4,2.8);//Build Triangle Class object T1,t2, the number of the triangle is the length of the bottom and the high    Shape *pt [6]= {&c1,&c2,&r1,&r2,&t1,&t2}; Defines the base class pointer array pt. Make it each element point to a derived class object    double areas=0.0;//areas for total area for    (int i=0; i<6; i++)    {        Areas=areas + pt[i]-> Area ();    }    cout<< "Totol of all areas=" <<areas<<endl;   Output total area    return 0;}

Code:
#include <iostream> #include <cmath> #define PI 3.14159using namespace Std;class shape{public:virtual double Area () = 0;}; Class Circle:public shape{private:double r;public:circle (double a=0): R (a) {} double area () {return r*r*pi;}}; Class Rectangle:public shape{private:double L,w;public:rectangle (double A, double b): L (a), W (b) {} double area () { return l*w;}; Class Triangle:public shape{private:double H,l;public:triangle (double a,double b): H (a), L (b) {} double area () {RET Urn H*L/2;}}; int main () {Circle C1 (12.6), C2 (4.9),//Create Circle Class object C1,c2, Radius Rectangle R1 (4.5,8.4), R2 (5.0,2.5),//Build Rectangle class object R1, R2, the parameters are rectangular length, width Triangle T1 (4.5,8.4), T2 (3.4,2.8); Establish the Triangle Class object T1,t2, which is the triangle bottom length and the high Shape *pt[6]= {&c1,&c2,&r1,&r2,&t1,&t2}; Defines the base class pointer array pt, which points each element to a derived class object double areas=0.0;    Areas for the total area for (int i=0; i<6; i++) {areas=areas + Pt[i]->area ();   } cout<< "Totol of all areas=" <<areas<<endl; //output total area return 0;} 

Execution Result:

13th Week "Project 2-pure virtual function in the shape class family"

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.