Learning notes-put the class declaration in the *. h header file, and put the class definition in *. cpp. Main # include "*. H ".

Source: Internet
Author: User
// Shape. h. In the header file folder # include <iostream> using namespace STD; # define PI 3.1415926 class shape {public: Virtual void print () = 0; virtual float area () = 0 ;}; class circle: Public shape {float R; public: Circle (float R); void print (); float area () ;}; class rectangle: public shape {float length, width; public: rectangle (float length, float width); void print (); float area ();};

 

 

// Shape. place CPP in the source file folder # include "shape. H "circle: Circle (float R) {This-> r = r;} void circle: Print () {cout <"the r of this circle is" <r <Endl;} float circle: Area () {return (float) pI * r ;} rectangle: rectangle (float length, float width) {This-> length = length; this-> width = width;} void rectangle: Print () {cout <"the length is" <length <Endl <"the width is" <width <Endl;} float rectangle: Area () {return length * width ;}

 

 

// Put main into the source file folder # include "shape. H "int main () {shape * s; S = new circle (442); s-> Print (); cout <"The area of the circle is" <s-> area () <Endl; Delete s; S = new rectangle (10, 5 ); s-> Print (); cout <"The area of the rectangle is" <s-> area () <Endl; Delete s; S = NULL; int N; cin> N; return 0 ;}

Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.

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.