Rectangle(Point p,Point q); void show(); };
Rectangle::Rectangle(Point p,Point q):rightbottom(q),lefttop(p){} /********** End **********/
void Point::show()//显示坐标 {
cout<<\}
void Rectangle::show()//显示4个顶点坐标和矩形面积 {
Point leftbottom,righttop;
double length=rightbottom.getX()-lefttop.getX(); double width=lefttop.getY()-rightbottom.getY();
leftbottom.setX(lefttop.getX());leftbottom.setY(rightbottom.getY()); righttop.setX(rightbottom.getX());righttop.setY(lefttop.getY()); cout<<\lefttop.show();
cout<<\leftbottom.show();
cout<<\righttop.show();
cout<<\rightbottom.show();
double area=length*width;
cout<<\ }
void main() {
Point p1(1,6),p2(5,2); Rectangle R(p1,p2); R.show(); }
试题三
第一题(该题被扣了两分,实在不知道哪错了,大神若知道,请告诉本人) /*------------------------------------------------------- 【程序设计】
---------------------------------------------------------
题目:设计一个点类Point,包含两个坐标数据成员x,y,
带默认形参的构造函数(默认形参值为0),和成员函数
double distance(Point p)用于求两点之间的距离。
运行结果见图:样张.JPG
-------------------------------------------------------*/ #include
/**********Program**********/ class Point{ double m,n,a,b; public: Point(); Point(double X,double Y); double distance(Point p); void setXY(double X,double Y); };
Point::Point(){ m=0; n=0; }
Point::Point(double X,double Y){ m=X;n=Y; }
void Point::setXY(double X,double Y){ m=X;n=Y; }
double Point::distance(Point p){ a=x;b=y; return sqrt((a-m)*(a-m)+(b-n)*(b-n)); }
/********** End **********/ void main(void) {
double x=3, y=4; Point p0,p1(x,y);
cout<<\两点间的距离=\ }
第二题
/*------------------------------------------------------- 【程序设计】
---------------------------------------------------------
题目:定义shape类,有三个表示长度的整型数据成员,分别为l、w、h。
编一个基于对象的程序,写一个成员函数is,可判定其形状:
当l、w、h均不为0时,判定形状为柱体,返回值为1;
当l、w、h仅有一个为0时,判定形状为长方形,返回值为2; 当l、w、h仅有一个不为0时,判定形状为线段,返回值为3; 当l、w、h均为0时,判定形状为点,返回值为4;
输出结果见图:样张.JPG
-------------------------------------------------------*/ #include
shape(int, int, int); int is(); };
/**********Program**********/ shape::shape(int L, int W, int H){ l=L;w=W;h=H; }
int shape::is(){ if(l!=0&&w!=0&&h!=0) return 1; else if((l==0&&w!=0&&h!=0)||(l!=0&&w==0&&h!=0)||(l!=0&&w!=0&&h==0))return 2; else if((l==0&&w==0&&h!=0)||(l==0&&w!=0&&h==0)||(l!=0&&w==0&&h==0))return 3; else if(l==0&&w==0&&h==0)return 4; }
/********** End **********/ void display(shape s) {
switch(s.is()) {
case 1:cout<<\这是柱体\ case 2:cout<<\这是长方形\ case 3:cout<<\这是线段\
case 4:cout<<\这是点\ } }
void main(void) {
shape my1(1, 2, 3); display(my1); shape my2(1, 0, 2); display(my2); shape my3(0, 0, 1); display(my3); shape my4(0, 0, 0); display(my4); }
第三题
/*------------------------------------------------------- 【程序设计】
---------------------------------------------------------
题目:
定义一个日期类Date,包含年、月、日三个数据成员(int),定义带有3个参数的构造函数, 以及一个求日期是当年的第几天的成员函数和输出日期的成员函数, 日期的显示格式为年/月/日。编写主函数进行测试。
(每年各月天数分别为31,28,31,30,31,30,31,31,30,31,30,31,闰年2月为29天, 闰年的条件year%4==0&&year0!=0)||year@0==0)) 输出结果见样张.JPG
-------------------------------------------------------*/ #include
/**********Program**********/ class Date{ intyear,month,day;
public: Date(int y=2000,int m=1,int d=1); intgetDay();
void SetD(int,int,int); void show(); };
Date::Date(inty,intm,int d){ year=y;month=m;day=d; }
void Date::SetD(inty,intm,int d){year=y;month=m;day=d;}
int Date::getDay(){ int n=0,nnn;int mon[12];
mon[0]= 31; mon[2]=31 ; mon[3]=30 ; mon[4]= 31; mon[5]=30 ; mon[6]=31 ; mon[7]= 31; mon[8]= 30; mon[9]= 31; mon[10]= 30; mon[11]= 31; if((year%4==0&&year0!=0)||year@0==0) mon[1]=29; else mon[1]=28; for(inti=1;i /********** End **********/ void Date::show() { cout< int main() { Date d1(2009,2,1),d2; //d1为2009年2月1日 d1.show(); cout< d2.SetD(2012,3,1); //d2为2012年3月1日 d2.show(); cout< 第四题 /*------------------------------------------------------- 【程序设计】 --------------------------------------------------------- 题目: 定义一个点类Point?包括x坐标和y坐标(int)。定义一个圆类Circle,有数据成员半径 和表示圆心坐标的点类对象。圆类成员函数包括构造函数、求面积的函数和输出显示圆心 坐标及圆半径的函数。 输出结果见样张.JPG -------------------------------------------------------*/ #include /**********Program**********/