C++面向对象程序设计上机考试题库[1](5)

2019-08-29 18:53

Time(int,int,int);

friend void display(const Date &,const Time &); private: int hour; int minute; int sec; };

Time::Time(int h,int m,int s) {hour=h; minute=m; sec=s; }

class Date {public:

Date(int,int,int);

friend void display(const Date &,const Time &); private: int month; int day; int year; };

Date::Date(int m,int d,int y) {month=m; day=d; year=y; }

void display(const Date &d,const Time &t) {

cout<

int main() {

Time t1(10,30,55); Date d1(3,25,2010); display(d1,t1); return 0; }

4.可以定义点类(Point),再定义一个类(Distance)描述两点之间的距离,其数据成员为两个点类对象,两点之间距离的计算可设计由构造函数来实现。

【第 21 页 共 48 页】

#include #include using namespace std; class Point { public :

Point ( double xi , double yi ) { X = xi ; Y = yi ; } double GetX( ) { return X ; } double GetY( ) { return Y ; } private : double X , Y ; } ;

class Distance {

public :

Distance(Point p,Point q);

double Getdist() {return dist;} private:

Point a,b; double dist; };

Distance::Distance(Point q1,Point q2):a(q1),b(q2) { double dx=double(a.GetX()-b.GetX()); double dy=double(a.GetY()-b.GetY()); dist=sqrt(dx*dx+dy*dy); }

int main ( )

{ Point p1 ( 3.0 , 5.0 ) , p2 ( 4.0 , 6.0 ) ; Distance dis(p1,p2);

cout << \return 0;}

5.定义点类(Point),再定义一个函数(Distance)描述两点之间的距离,其数据成员为两个点类对象,将两点之间距离函数声明为Point类的友元函数。 #include #include using namespace std; class Point { public :

Point ( double xi , double yi ) { X = xi ; Y = yi ; } double GetX( ) { return X ; } double GetY( ) { return Y ; }

friend double Distance ( Point & a , Point & b ) ; private : double X , Y ; } ;

【第 22 页 共 48 页】

double Distance ( Point & a , Point & b ) { double dx = a.X - b.X ; double dy = a.Y - b.Y ;

return sqrt( dx * dx + dy * dy ) ; }

int main ( )

{ Point p1 ( 3.0 , 5.0 ) , p2 ( 4.0 , 6.0 ) ; double d = Distance ( p1 , p2 ) ;

cout << \return 0;}

6.实现重载函数Double(x),返回值为输人参数的两倍;参数分别为整型、浮点型、双精度型,返回值类型与参数一样。(用类模板实现) #include using namespace std; template class Double {public:

Double(numtype a) {x=a;}

numtype bei() {return 2*x;} private:

numtype x; };

int main()

{Doubledou1(3); cout<dou2(12.36); cout<

Doubledou3(25.33333); cout<

7.有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0开始算。要求输出分和秒的值。初值自拟。 #include using namespace std; class Time {public:

Time(){minute=0;sec=0;}

Time(int m,int s):minute(m),sec(s){} Time operator++();

void display(){cout<

【第 23 页 共 48 页】

int sec; };

Time Time::operator++() {if(++sec>=60) {sec-=60; ++minute;} return *this; }

int main()

{Time time1(34,0);

for (int i=0;i<61;i++) {++time1;

time1.display();} return 0; }

8.声明一个教师(Teacher)类和一个学生(Student)类,用多重继承的方式声明一个研究生(Graduate)派生类。教师类中包括数据成员name(姓名),age(年龄),title(职称)。学生类中包括数据成员name(姓名),age(年龄),score(成绩)。在定义派生类对象时给出初始化的数据(自已定),然后输出这些数据。初值自拟。

#include #include using namespace std;

class Teacher {public: Teacher(string nam,int a,string t) {name=nam; age=a; title=t;}

void display() {cout<<\ cout<<\

cout<<\ }

protected: string name; int age;

string title; };

class Student {public:

Student(string nam,char s,float sco) {name1=nam; sex=s;

score=sco;} void display1()

【第 24 页 共 48 页】

{cout<<\ cout<<\ cout<<\ }

protected: string name1; char sex;

float score; };

class Graduate:public Teacher,public Student {public:

Graduate(string nam,int a,char s,string t,float sco,float w): Teacher(nam,a,t),Student(nam,s,sco),wage(w) {} void show( ) {cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ cout<<\ }

private:

float wage; };

int main( )

{Graduate grad1(\ grad1.show( ); return 0; }

9.在上题的基础上,在Teacher类和Student类之上增加一个共同的基类Person,如下图所示。作为人员的一些基本数据都放在Person中,在Teacher类和Student类中再增加一些必要的数据(Student类中增加score,Teacher类中增加职称title,Graduate类中增加工资wages)。初值自拟。

Person【第 25 页 共 48 页】


C++面向对象程序设计上机考试题库[1](5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:3+4基础综合(2015年试卷)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: