1、实验5类和对象题目实验5 类和对象程序填空1. 题目描述:仔细阅读下列求两个点之间距离的程序,程序的输出结果是50,根据程序的输出结果在划线处填入正确语句。代码:#include#includeusing namespace std;class pointpublic: point(float a,float b) x=a; y=b; float Distance(point &p) float dx=p.x-x; float dy=p.y-y; return (float)sqrt(dx*dx+dy*dy); private: float x,y;int main() point p1(2,
2、3),p2(32,43); coutp1.Distance(p2)endl; return 0;2. 题目描述:设计一个矩阵类CRectangle,该类中的私有成员变量存放Rectangle的长和宽,并设置它们的默认值为1,通过成员函数set()来设定长和宽的值,并确保长宽都在(0,50)范围之内,求其周长Perimeter并显示输出。以下是完成此项工作的程序,请将未完成的部分填入,使之完整。代码:#includeusing namespace std;class CRectanglepublic: void Set(float a,float b) if(a0)&(a0)&(b50) wid
3、th=b; else width=1; float perimeter() return 2*(length+width); private: float length; float width;int main() CRectangle R; float l,w;/定义矩形的长和宽做为输入变量; / cout请输入矩形的长和宽:lw; R.Set(1,w); /设置矩形的长和宽 cout矩形的周长为:R.perimeter()endl; return 0;3. 题目描述:设计一个类CRectangle,要求如下所述。(1)定义两个点坐标x1,y1,x2,y2,两点所确定的一条直线构成了矩形的
4、对角线。(2)初始化矩形的两个点时,判断给定的两个点是否能够构成一个矩形,如果不能构成矩形,则矩形对角线的两点初始化为(0,0)和(1,1)。如果可以构成,则用形参初始化对象的数据成员。根据以上描述完成下列程序。代码:#include#includeusing namespace std;class CRectanglepublic: CRectangle(float Rx1=0,float Ry1=0, float Rx2=1,float Ry2=1); bool IsSquare( ); void PrintRectangle( );private: /确定直线的两点的坐标 float x
5、1,y1,x2,y2;CRectangle:CRectangle(float Rx1 ,float Ry1, float Rx2,float Ry2) if (Rx1=Rx2|Ry1=Ry2) /两点的横坐标或纵坐标的值相等,则不能构成矩形 x1=y1=0; x2=y2=1; cout不能构成矩形! endl; else x1=Rx1,x2=Rx2,y1=Ry1,y2=Ry2 /初始化数据成员x1,y1,x2,y2 cout可以构成矩形! endl; int main() CRectangle R1(1,3,5,6); CRectangle R2(1,3,1,6); return 0;4. 题
6、目描述:下列程序中声明了类girl,其中函数“display”是类girl的友元函数,请在(1)、(2)和(3)处各填入正确的内容,使程序能正常运行。代码:#includeusing namespace std;class girlprivate: char name; int age;public: girl(char n, int d) /构造函数 name= n; age=d; Friend void display(girl &x); /声明友元函数;void display(girl &x) /类外定义 友元函数 coutGirls name is :x. name, age is
7、:x.ageendl; /girl类的友元函数能访问girl类对象的私有成员int main( ) girl e(z,18); display(e); /调用友元函数 return 0;5. 题目描述:,请完善下面程序,使程序的运行结果如下:This is a constructor ! This is a constructor ! The value of ch is a The value of ch is b This is a destructor of b This is a destructor of a 代码:#includeusing namespace std;class
8、MyClass char ch;public: MyClass( ) coutThis is a constructor! endl; ch=a; MyClass(char character ) coutThis is a constructor! endl; ch=character; void Print( ) coutThe value of ch is chendl; MyClass( ) coutThis is a destructor ofchendl; ;int main( ) MyClass first, second(b); first.Print( ); second.P
9、rint( ); return 0;程序设计6.题目标题:计算两点间的距离 题目描述:仿照本次实验预习的程序填空题1,将以上Distance函数定义为类piont的友元函数,实现程序的功能。并在主函数中增加输入两点作为友元函数的实参。其主函数如下:输入描述:输入四个数,用空格隔开。输出描述:两个点的距离。样例输入:1 3 5 6样例输出:5#include#includeusing namespace std;class pointpublic: point(float a,float b) x=a; y=b; friend float Distance( point &p1, point &
10、p2);private: float x,y;float Distance( point &p1, point &p2) float dx=p1.x-p2.x; float dy=p1.y-p2.y; return (float)sqrt(dx*dx+dy*dy);int main() float p1_x,p1_y,p2_x,p2_y; /输入四个点 cinp1_xp1_yp2_xp2_y; point p1(p1_x,p1_y),p2(p2_x,p2_y); coutDistance(p1,p2)endl; return 0;7.题目标题:日期类CDateInfo的设计。题目描述:根据以下
11、主函数的功能来设计日期类CDateInfo,使其能正确运行。类CDateInfo中应该具有描述年、月、日的三个数据成员和相应的成员函数。#includeusing namespace std;class CDateibfo int day,month,year;public: CDateibfo(); CDateibfo(int yy,int mm,int dd); void setdate(int yy,int mm,int dd); void getdate();CDateibfo:CDateibfo() day=10; month=10; year=2011;CDateibfo:CDat
12、eibfo(int yy,int mm,int dd) year=yy; month=mm; day=dd;void CDateibfo:setdate(int yy,int mm,int dd) year=yy; month=mm; day=dd;void CDateibfo:getdate() coutyear-month-dayymd; date1.setdate(y,m,d); date1.getdate(); date2.getdate(); return 0;输入描述: 三个整数,分别用来设置对象data1的年、月、日输出描述:两行:第1行为对象data1的年月日;第2行为data
13、2的年月日。样例输入:2011 12 5样例输出:2012-12-52011-10-108.题目标题:学生类Student的设计题目描述:根据以下主函数的功能来设计日期类Student,使其能正确运行。类Student中应该具有描述学生姓名、性别、年龄的三个数据成员和相应的成员函数。输入描述:3行,第一行为一个长度不超过10的字符串表示姓名;第二行为0和1中的一个整数;第三行为一个整数,表示年龄。输出描述: 按主函数要求输出。#include#includeusing namespace std;class Studentprivate: char name20; int sex; unsigned old;public: void SetName(char *chOne); void SetGender(int isex); void SetAge(unsigned iold); vo
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1