设计矩形类计算矩形周长与面积//C++设计矩形类【属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积】#include<>#include<cmath>usingnamespacestd;classRectangle{doubleleft,top;doubleright,bottom;public:Rectangle(doublel=0,doublet=0,doubler=0,doubleb=0);~Rectangle(){};//析构函数,在此函数体为空voidAssign(doublel,doublet,doubler,doubleb);doublegetLeft(){returnleft;}//以下四个函数皆为内联成员函数doublegetRight(){returnright;}doublegetTop(){returntop;}doublegetBottom(){returnbottom;}voidShow();doubleArea();doublePerimeter();};//构造函数,带缺省参数,缺省值为全0,在声明中指定Rectangle::Rectangle(doublel,doublet,doubler,doubleb){left=l;top=t;right=r;bottom=b;}voidRectangle::Assign(doublel,doublet,doubler,doubleb){//赋值left=l;top=t;right=r;bottom=b;}voidRectangle::Show(){//成员函数直接使用私有的数据成员cout<<"left-toppointis("<<left<<","<<top<<")"<<'\n';cout<<"right-bottompointis("<<right<<","<<bottom<<")"<<'\n';}doubleRectangle::Area(){returnfabs((right-left)*(bottom-top));}doubleRectangle::Perimeter(){return2*(fabs(right-left)+fabs(bottom-top));}int(){Rectanglerect;rect.Show();rect.Assign(100,200,300,400);rect.Show();Rectanglerect1(0,0,200,200);rect1.Show();Rectanglerect2(rect1);rect2.Show();cout<<"面积"<<rect.Area()<<'\t'<<"周长"<<rect.Perimeter()<<endl;return0;}c++如何直接输出一个对象悬赏分:0-解决时间:2009-11-119:57我看到一行程序中有:cout<<A<<endl;其中A是一个对象,我不理解,请问它是不是用了类似java中的toString()方法一样,若是的话是什么方法提问者:songhaohao3031-一级最佳答案检举给你看个例子就明白了。你自定义的类,只要是你重载了<<。则都可以输出的。//重载输出运算符"<<"#include<iostream>//有些编译系统可能是包含iostream,并指明名字空间std;usingnamespacestd;classCComplex{public:CComplex(){real=0.0;image=0.0;}CComplex(doublerv){real=rv;image=0.0;}CComplex(doublerv,doubleiv){real=rv;image=iv;}friendCComplexoperator+(CComplexc1,CComplexc2);//作为类的友元函数,重载加运算符,friendostream&operator<<(ostream&stream,CComplexc);//重载输出运算符"<<"~CComplex(){};private:doublereal;//复数的实部doubleimage;//复数的虚部};CComplexoperator+(CComplexc1,CComplexc2){CComplextemp;temp.real=c1.real+c2.real;temp.image=c1.image+c2.image;returntemp;}ostream&operator<<(ostream&stream,CComplexc){stream<<"("<<c.real<<"+"<<c.image<<"i)"<<endl;//以(a+bi)的格式输出复数returnstream;}intmain(){CComplexc1(1,5),c2(3);cout<<"c1="<<c1;//使用重载输出运算符"<<",输出复数c1cout<<"c2="<<c2;//使用重载输出运算符"<<",输出复数c2c1=c2+16;cout<<"执行语句c1=c2+16;之后,";cout<<"c1="<<c1;return0;}C++编程:用面向对象的方法求矩形面积.要求编写一个矩形Rectangle类悬赏分:0-解决时间:2009-5-811:13用面向对象的方法求矩形面积.要求编写一个矩形Rectangle类,数据成员有:长(Length)、宽(Width),函数成员有:(1)构造函数,功能是给长和宽初始化(2)成员函数setLW(),功能是给长和宽赋值(3)成员函数Area(),功能是求出矩形的面积。在main函数中声明该类的对象,求出该对象的面积。提问者:jdzyh-五级最佳答案#include<iostream>usingnamespacestd;classRetangle{public:Retangle(){Length=0;Width=0;}voidsetLW(){floatx,y;cout<<"inputthelengthandwidth:"<<endl;cin>>x>>y;Length=x;Width=y;}voidArea(){cout<<"Area="<<Length*Width<<endl;}private:floatLength;floatWidth;};voidmain(){Retangleabc;abc.setLW();abc.Area();}//main.cpp文件内容#include<cstdlib>#include<iostream>#include"rectangle.h"usingnamespacestd;intmain(intargc,char*argv[]){Rectanglerect1;rect1.setLengthAndWidth();rect1.showArea();system("PAUSE");return0;}//Rectangle.h文件内容#include<iostream>usingnamespacestd;classRectangle{public:voidshowArea();voidsetLengthAndWidth();private:floatlength,width;}//rectangle.cpp文件内容#include<iostream>#include"rectangle.h"usingnamespacestd;voidRectangle::showArea(){cout<<"该长方形的面积为:"<<length*width<<endl;}voidRectangle::setLengthAndWidth(){do{cout<<"请输入长方形的长和宽:";cin>>length>>width;if(length<0||width<0)cout<<"长方形边长不能为负数,请重新输入!"<<endl;}while(length<0||width<0);}//运行通过的,你也可以把这三部分放一个文件里。//其实并不长,只是为了可读性增加了空行,你可以去掉。//而且如果放一个文件里可以去掉重复声明的头文件。//但是为了更标准一些,建议你还是分三个文件放参考资料:自己写的