4. 设计并测试一个名为Rectangle的矩形类
4.设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。...
4. 设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角与右上角两个点的坐标,能计算矩形的面积。
展开
2个回答
展开全部
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle {
double left, top ;
double right, bottom;
public:
Rectangle(double l=0, double t=0, double r=0, double b=0);
~ Rectangle(){}; //析构函数,在此函数体为空
void Assign(double l,double t,double r,double b);
double getLeft(){ return left;} // 以下四个函数皆为内联成员函数
double getRight(){ return right;}
double getTop(){return top;}
double getBottom(){return bottom;}
void Show();
double Area();
double Perimeter();
};
// 构造函数,带缺省参数,缺省值为全0,在声明中指定
Rectangle::Rectangle(double l , double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Assign(double l, double t, double r, double b){//赋值
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Show(){//成员函数直接使用私有的数据成员
cout<<"left-top point is ("<<left<<","<<top<<")"<<'\n';
cout<<"right-bottom point is ("<<right<<","<<bottom<<")"<<'\n';
}
double Rectangle::Area(){
return fabs((right-left)*(bottom-top));
}
double Rectangle::Perimeter(){
return 2*(fabs(right-left)+fabs(bottom-top));
}
int main(){
Rectangle rect;
rect.Show();
rect.Assign(100,200,300,400);
rect.Show();
Rectangle rect1(0,0,200,200);
rect1.Show();
Rectangle rect2(rect1);
rect2.Show();
cout<<"面积"<<rect.Area()<<'\t'<<"周长"<<rect.Perimeter()<<endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
class Rectangle {
double left, top ;
double right, bottom;
public:
Rectangle(double l=0, double t=0, double r=0, double b=0);
~ Rectangle(){}; //析构函数,在此函数体为空
void Assign(double l,double t,double r,double b);
double getLeft(){ return left;} // 以下四个函数皆为内联成员函数
double getRight(){ return right;}
double getTop(){return top;}
double getBottom(){return bottom;}
void Show();
double Area();
double Perimeter();
};
// 构造函数,带缺省参数,缺省值为全0,在声明中指定
Rectangle::Rectangle(double l , double t, double r, double b) {
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Assign(double l, double t, double r, double b){//赋值
left = l; top = t;
right = r; bottom = b;
}
void Rectangle::Show(){//成员函数直接使用私有的数据成员
cout<<"left-top point is ("<<left<<","<<top<<")"<<'\n';
cout<<"right-bottom point is ("<<right<<","<<bottom<<")"<<'\n';
}
double Rectangle::Area(){
return fabs((right-left)*(bottom-top));
}
double Rectangle::Perimeter(){
return 2*(fabs(right-left)+fabs(bottom-top));
}
int main(){
Rectangle rect;
rect.Show();
rect.Assign(100,200,300,400);
rect.Show();
Rectangle rect1(0,0,200,200);
rect1.Show();
Rectangle rect2(rect1);
rect2.Show();
cout<<"面积"<<rect.Area()<<'\t'<<"周长"<<rect.Perimeter()<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
class Rectangle
{
int left, top, right, bottom;
public:
Rectangle(int l, int t, int r, int b):left(l),top(t),right(r),bottom(b){}
int area(){ return ((right-left)*(bottom-top));}
};
int main()
{
Rectangle r1(0, 0, 30, 40);
cout<<r1.area()<<endl;
return 0;
}
using namespace std;
class Rectangle
{
int left, top, right, bottom;
public:
Rectangle(int l, int t, int r, int b):left(l),top(t),right(r),bottom(b){}
int area(){ return ((right-left)*(bottom-top));}
};
int main()
{
Rectangle r1(0, 0, 30, 40);
cout<<r1.area()<<endl;
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询