c++程序设计 设计一个矩形类(rect),

设计一个矩形类(rect),具有长,宽的属性,类还具有求解并显示矩形的周长和面积的功能,以及求两个矩形面积和的功能(实现该功能时,要求用对象做参数和返回值)

#include<iostream>
using namespace std;

class rect
{
public:
rect(int width, int length) : width(width),length(length){}
int Area()
{
return length * width;
}
int Perimeter()
{
return 2 * (length + width);
}
friend int add(rect &r1, rect &r2);
private:
int length;
int width;
};

int add(rect &r1,rect &r2)
{
return r1.Area() + r2.Area();
}

int main()
{
rect rect1(2,4), rect2(4,6);
cout<<"rect1的面积是"<<rect1.Area()<<endl;
cout<<"rect1的周长是"<<rect1.Perimeter()<<endl;
cout<<"rect2的面积是"<<rect2.Area()<<endl;
cout<<"rect2的周长是"<<rect2.Perimeter()<<endl;
cout<<"rect1 和rect2的面积之和为"<<add(rect1,rect2)<<endl;
return 0;
}

运行结果
rect1的面积是8
rect1的周长是12
rect2的面积是24
rect2的周长是20
rect1 和rect2的面积之和为32
Press any key to continue
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-06-14
class rect{
int x,y;
public rect(int x,int y){
this.x=x;
this.y=y;
}
public int area(){
return this.x*this.y;
}
public int primeter(){
return 2*(this.x+this.y);
}
public void display(){
cout<<"面积="<<area()<<",周长="<<primeter()<<endl;
}
public int add(rect a){
return this.area()+a.area();
}
}
第2个回答  2012-06-14
class rect{
public:
long length;
long weigth;
rect(long length,long weigth){
length=0;
weigth=0;
}

long getlength(long total){
total=length+weigth;
}

long setlength(){
retrun total;
}

long getji(long cy){
cy=length*weigth;
}

long setji()
{
retrun cy;
}

long gethe(const rect& r1,const rect&r2,long sum)
{
sum=r1.setji()+r2.setji();
}

long sethe()
{
return sum;
}
}
第3个回答  2019-03-29
#include<iostream>
using
namespace
std;
class
rect
{
public:
rect(int
width,
int
length)
:
width(width),length(length){}
int
Area()
{
return
length
*
width;
}
int
Perimeter()
{
return
2
*
(length
+
width);
}
friend
int
add(rect
&r1,
rect
&r2);
private:
int
length;
int
width;
};
int
add(rect
&r1,rect
&r2)
{
return
r1.Area()
+
r2.Area();
}
int
main()
{
rect
rect1(2,4),
rect2(4,6);
cout<<"rect1的面积是"<<rect1.Area()<<endl;
cout<<"rect1的周长是"<<rect1.Perimeter()<<endl;
cout<<"rect2的面积是"<<rect2.Area()<<endl;
cout<<"rect2的周长是"<<rect2.Perimeter()<<endl;
cout<<"rect1
和rect2的面积之和为"<<add(rect1,rect2)<<endl;
return
0;
}
运行结果
rect1的面积是8
rect1的周长是12
rect2的面积是24
rect2的周长是20
rect1
和rect2的面积之和为32
Press
any
key
to
continue

c++程序设计 设计一个矩形类(rect),
cout<<"rect2的周长是"<<rect2.Perimeter()<<endl;cout<<"rect1 和rect2的面积之和为"<<add(rect1,rect2)<<endl;return 0;} 运行结果 rect1的面积是8 rect1的周长是12 rect2的面积是24 rect2的周长是20 rect1 和rect2的面积之和为32 Press any key to continue ...

C++设计 定义一个矩形类Rect 急求啊
Rect * rect = new Rect(10,20);cout<<rect->getArea()<<endl;getch();return 0;} [\/code]

c++如何设计一个矩形类(rect)?
cout<<"rect1 和rect2的面积之和为"<<add(rect1,rect2)<<endl;return 0;} 运行结果 rect1的面积是8 rect1的周长是12 rect2的面积是24 rect2的周长是20 rect1 和rect2的面积之和为32 Press any key to continue

C++程序设计 矩形Rectangle类,通过Rectangle类计算周长和面积。_百度...
rect.setlength(10);rect.Setwidth(15);std::cout << rect ;return 0;}

用C++设计一个矩形类
void OffsetRect(SIZE size);void OffsetRect(POINT point);void NormalizeRect();\/\/ set this rectangle to intersection of two others BOOL IntersectRect(LPCRECT lpRect1, LPCRECT lpRect2);\/\/ set this rectangle to bounding union of two others BOOL UnionRect(LPCRECT lpRect1, LPCRECT lpR...

C++定义并实现一个矩形类
class Rectangle { public:Rectangle(){ x1=0;x2=0;y1=0;y2=0;} Rectangle(double a1,double b1,double a2,double b2){ x1=a1;y1=b1;x2=a2;y2=b2;} void input(){ cout<<"请输入矩形左下角和右上角顶点的坐标:"<<endl;cin>>x1>>y1>>x2>>y2;} void output(){ cout<<"...

C++设计一个矩形类,派生出长方形正方形,计算周长和面积
include <iostream.h>class juxing \/\/矩形类 { public:float a;\/\/矩形的长 float b;\/\/宽 public:juxing(float x,float y)\/\/初始化 { a=x;b=y;} public:float getS()\/\/面积 { return (float)(a*b);} float getC()\/\/周长 { return (float)((a+b)*2.0);} };class chang...

用c++编写一个矩形类
include <iostream>using namespace std;class Rectangle { private: double length; double height; double area; double circumference; public: Rectangle() {} Rectangle(double l, double h) { length = l; height=h; } Rectangle(const Rectangle &r) { leng...

采用c++定义并实现一个矩形(Rectangle)类
= color;}int GetArea() {return this->length * this->width;}bool IsSquare() {return this->length == this->width;}private:int length;int width;string color;};int main(){Rectangle rect;rect.SetLength(100);rect.SetWidth(100);rect.SetColor("红色");cout << "矩形:...

C++问题:设计一个名为Rectangle的矩形类,其属性为矩形的左下角和右...
第一种:include <iostream.h> include <math.h> class Rectangle { public:Rectangle(int left,int bottom,int right,int top);~Rectangle(){ } \/\/int GetLeft() {return itsLeft;} int GetBottom() {return itsBottom;} int GetRight() {return itsRight;} int GetTop() { return it...

相似回答