设计并测试一个矩形类(Rectangle)
设计并测试一个矩形类(Rectangle),属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积。测试包括用成员函数和普通函数。...
设计并测试一个矩形类(Rectangle),属性为矩形的左下与右上角的坐标,矩形水平放置。操作为计算矩形周长与面积。测试包括用成员函数和普通函数。
展开
展开全部
#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;
}
参考资料: 已通过编译,main函数只做测试之用,所以比较灵活,可以自己写
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream.h>
#include<math.h>
class Rectangle
{
private:
int x1,y1;
int x2,y2;
public:
Rectangle (){}
Rectangle (int a,int b,int c,int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
int Area();
int Cir();
};
int Rectangle::Area()
{
return (abs(x2-x1))*(abs(y2-y1));
}
int Rectangle::Cir()
{
return (abs(x2-x1)+abs(y2-y1))*2;
}
void main()
{
Rectangle rec1(10,10,1,1);
cout<<rec1.Area()<<endl<<rec1.Cir()<<endl;
}
随便写的。。
#include<math.h>
class Rectangle
{
private:
int x1,y1;
int x2,y2;
public:
Rectangle (){}
Rectangle (int a,int b,int c,int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
int Area();
int Cir();
};
int Rectangle::Area()
{
return (abs(x2-x1))*(abs(y2-y1));
}
int Rectangle::Cir()
{
return (abs(x2-x1)+abs(y2-y1))*2;
}
void main()
{
Rectangle rec1(10,10,1,1);
cout<<rec1.Area()<<endl<<rec1.Cir()<<endl;
}
随便写的。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询