c++程序设计 设计一个矩形类(rect), 5
设计一个矩形类(rect),具有长,宽的属性,类还具有求解并显示矩形的周长和面积的功能,以及求两个矩形面积和的功能(实现该功能时,要求用对象做参数和返回值)...
设计一个矩形类(rect),具有长,宽的属性,类还具有求解并显示矩形的周长和面积的功能,以及求两个矩形面积和的功能(实现该功能时,要求用对象做参数和返回值)
展开
4个回答
展开全部
#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
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
展开全部
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();
}
}
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();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
}
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;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询