C++面向对象类的实例题目六

问题描述:

编写一个程序计算两个给定长方形的面积,其中在设计类成员函数addarea()(用于计算两个长方形的总面积)时使用对象作为参数。

程序代码:

#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; 	
} 


结果输出:

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


posted @ 2014-01-01 00:19  千手宇智波  阅读(431)  评论(0编辑  收藏  举报