C ++ object-oriented Example 6

Source: Internet
Author: User

Problem description:

Write a program to calculate the area of two given rectangles, in which the object is used as the parameter when designing the member function addarea () (used to calculate the total area of the two rectangles.

Program code:

#include<iostream>using namespace std;class Rectangular{public:Rectangular(double w,double l){width = w;length = l;}double getc(){circumference = width + length;return circumference;}double adddata(Rectangular &r){return (circumference + r.getc());}private:double width;double length;double circumference;}; int main(){Rectangular r1(2.5,3.5);cout<<"Circumference of r1 ="<<r1.getc()<<endl; Rectangular r2(2,3);cout<<"Circumference of r2 ="<<r2.getc()<<endl;cout<<"Circumference of r1+r2 ="<<r1.adddata(r2)<<endl;return 0; } 


Result output:

Circumference of r1 =6Circumference of r2 =5Circumference of r1+r2 =11


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.