C++设计一个矩形类(Rectangle),可以求矩形的周长和面积?急急急!!!求高手帮忙?
设计一个矩形类(Rectangle),可以求矩形的周长和面积。要求:a、矩形的长和宽由键盘上输入;b、在主函数中有一个矩形对象rect1,求rect1的周长和面积,然后再...
设计一个矩形类(Rectangle),可以求矩形的周长和面积。要求:a、矩形的长和宽由键盘上输入;b、在主函数中有一个矩形对象rect1,求rect1的周长和面积,然后再定义一个对象rect2,用rect1去初始化rect2,最后也输出rect2的周长和面积
展开
2个回答
展开全部
#include<stdio.h>
class Rectangle{
private:
int m_nWidth;
int m_nHeight;
public:
Rectangle(){;}
~Rectangle(){;}
void SetWidth( const int width ){ m_nWidth = width; }
void SetHeight( const int height){ m_nHeight = height; }
int GetGirth(){ return 2 * ( m_nHeight + m_nWidth ); }
int GetArea(){ return m_nHeight * m_nWidth; }
};
void main(){
int count= 0;
Rectangle rc;
printf("请输入矩形的宽:\n");
scanf("%d",&count);
rc.SetWidth( count );
printf("请输入矩形的长:\n");
scanf("%d",&count);
rc.SetHeight( count );
printf("周长为:%d\n", rc.GetGirth());
printf("面积为:%d\n", rc.GetArea());
}
class Rectangle{
private:
int m_nWidth;
int m_nHeight;
public:
Rectangle(){;}
~Rectangle(){;}
void SetWidth( const int width ){ m_nWidth = width; }
void SetHeight( const int height){ m_nHeight = height; }
int GetGirth(){ return 2 * ( m_nHeight + m_nWidth ); }
int GetArea(){ return m_nHeight * m_nWidth; }
};
void main(){
int count= 0;
Rectangle rc;
printf("请输入矩形的宽:\n");
scanf("%d",&count);
rc.SetWidth( count );
printf("请输入矩形的长:\n");
scanf("%d",&count);
rc.SetHeight( count );
printf("周长为:%d\n", rc.GetGirth());
printf("面积为:%d\n", rc.GetArea());
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
class Rectangle
{
private:
int height;
int width;
public:
int getC()
{
return (height+width)*2;
}
int getS()
{
return height*width;
}
Rectangle& operator=(const Rectangle& r)
{
height=r.height;
width=r.width;
return *this;
}
Rectangle(int a,int b)
{
height=a;
width=b;
}
};
int main()
{
int a,b;
cin>>a>>b;
Rectangle rect1(a,b);
cout<<"rect 1 C:"<<rect1.getC()<<" rect1 S:"<<rect1.getS()<<endl;
Rectangle rect2=rect1;
cout<<"rect 2 C:"<<rect2.getC()<<" rect2 S:"<<rect2.getS()<<endl;
return 0;
}
using namespace std;
class Rectangle
{
private:
int height;
int width;
public:
int getC()
{
return (height+width)*2;
}
int getS()
{
return height*width;
}
Rectangle& operator=(const Rectangle& r)
{
height=r.height;
width=r.width;
return *this;
}
Rectangle(int a,int b)
{
height=a;
width=b;
}
};
int main()
{
int a,b;
cin>>a>>b;
Rectangle rect1(a,b);
cout<<"rect 1 C:"<<rect1.getC()<<" rect1 S:"<<rect1.getS()<<endl;
Rectangle rect2=rect1;
cout<<"rect 2 C:"<<rect2.getC()<<" rect2 S:"<<rect2.getS()<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询