14th weekly computer task project 2

Source: Internet
Author: User
01./* 02 .* Program Copyright and version description section 03. * copyright (c) 2013, Yantai University Computer college student 04. * All rightsreserved. 05. * file name: shape. CPP 06. * Author: Zhao guanzhe 07. * Completion Date: April 8, May 31, 2013. * version: V1.0 09. * input Description: 10. * Problem description: 11. */# include <iostream> using namespace STD; # define PI 3.1415926 class shape // declare the abstract base class shape {public: Virtual double area () = 0 ;}; class circle: public shape // declare the circle class {public: Circle (Double R): radius (r) {} virtual double area () {return pI * radius;} PRIVATE: double Radius ;}; class rectangle: Public shape // declare the rectangle class {public: rectangle (double L, double W): length (L), width (W) {} virtual double area () {return length * width;} PRIVATE: double length, width ;}; class triangle: Public shape // declare the triangle class {public: triangle (double L, double H): length (L), heigth (h) {} virtual double area () {return 0.5 * length * heigth;} PRIVATE: double length, heigth ;}; int main () {circle C1 (12.6), C2 (4.9); // create a circle Class Object C1, C2. The parameter is the circle radius rectangle r1 (4.5, 8.4), R2 (5.0, 2.5); // create rectangle class objects R1 and R2. The parameters are rectangular length, width, triangle T1 (4.5, 8.4), T2 (3.4, 2.8); // create a triangle Class Object T1 and T2. The parameter is the triangle bottom side length and height shape * PT [6] = {& C1, & C2, & R1, & R2, & T1, & T2}; // defines the base class pointer array PT, so that each element points to a derived class Object double areas = 0.0; // areas is the total area for (INT I = 0; I <6; I ++) {areas = areas + Pt [I]-> area ();} cout <"totol of all areas =" <areas <Endl; // return 0 ;}

Running result:

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.