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

2019-08-29 18:53

cy1.setRadius(7.5); cy1.setPoint(5,5);

cout<<\ Point &pRef=cy1;

cout<<\ Circle &cRef=cy1;

cout<<\ return 0; }

19.写一个程序,定义抽象类型Shape,由他派生三个类:Circle(圆形),Rectangle(矩形),Trapezoid(梯形),用一个函数printArea分别输出三者的面积,3个图形的数据在定义对象是给定。

#include using namespace std;

class Shape {public:

virtual double area() const =0; };

class Circle:public Shape {public:

Circle(double r):radius(r){} virtual double area() const {return 3.14159*radius*radius;}; protected:

double radius; };

class Rectangle:public Shape {public:

Rectangle(double w,double h):width(w),height(h){} virtual double area() const {return width*height;} protected:

double width,height; };

class Trapezoid:public Shape {public:

Trapezoid(double w,double h,double

len):width(w),height(h),length(len){}

virtual double area() const {return 0.5*height*(width+length);} protected:

【第 16 页 共 48 页】

double width,height,length; };

void printArea(const Shape &s)

{cout<

int main() {

Circle circle(12.6); cout<<\

printArea(circle); Rectangle rectangle(4.5,8.4); cout<<\ printArea(rectangle);

Trapezoid trapezoid(4.5,8.4,8.0); cout<<\ printArea(trapezoid); return 0; }

20.定义一个人员类Cperson,包括数据成员:姓名、编号、性别和用于输入输出的成员函数。在此基础上派生出学生类CStudent(增加成绩)和老师类Cteacher(增加教龄),并实现对学生和教师信息的输入输出。

#include #include class CPerson {

public:

void SetData(char *name, char *id, bool isman = 1) { int n = strlen(name);

strncpy(pName, name, n); pName[n] = '\\0'; n = strlen(id);

strncpy(pID, id, n); pID[n] = '\\0'; bMan = isman; }

void Output() {

cout<<\姓名:\ cout<<\编号:\ char *str = bMan?\男\女\ cout<<\性别:\ }

private:

char pName[20];

【第 17 页 共 48 页】

char pID[20]; bool bMan; };

class CStudent: public CPerson {

public:

void InputScore(double score1, double score2, double score3) {

dbScore[0] = score1; dbScore[1] = score2; dbScore[2] = score3; }

void Print() {

Output(); for (int i=0; i<3; i++)

cout<<\成绩\:\ }

private:

double dbScore[3]; };

class Cteacher: public CPerson {

public:

void Inputage(double age) {

tage = age; }

void Print() {

Output();

cout<<\教龄:\ }

private:

double tage; };

void main() {

CStudent stu; Cteacher tea;

stu.SetData(\ stu.InputScore( 80, 76, 91 ); stu.Print();

tea.SetData(\ tea.Inputage(12); tea.Print();

【第 18 页 共 48 页】

}

二、第二类题目(20道,每题9分,请自行设计输出格式)

1.某商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,因此,商店需要记下目前库存货物的总量,要求把商店货物购进和卖出的情况模拟出来。

#include using namespace std; class Goods { public :

Goods ( int w) { weight = w; totalWeight += w ; } ; ~ Goods ( ) { totalWeight -= weight ; } ; int Weight ( ) { return weight ; } ;

static int TotalWeight ( ) { return totalWeight ; } ; private : int weight ; static int totalWeight ; } ;

int Goods :: totalWeight = 0 ; main ( ) { int w ;

cin >> w ; Goods *g1 = new Goods( w ) ; cin >> w ; Goods *g2 = new Goods( w ) ; cout << Goods::TotalWeight ( ) << endl ; delete g2 ;

cout << Goods::TotalWeight ( ) << endl ; }

2.设计一个Time类,包括三个私有数据成员:hour,minute,sec,用构造函数初始化,内设公用函数display(Date &d),设计一个Date类,包括三个私有数据成员:month,day,year,也用构适函数初始化;分别定义两个带参数的对象t1(12,30,55),d1(3,25,2010),通过友员成员函数的应用,输出d1和t1的值。 #include using namespace std; class Date; class Time {public:

Time(int,int,int);

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

Time::Time(int h,int m,int s)

【第 19 页 共 48 页】

{hour=h; minute=m; sec=s; }

class Date {public:

Date(int,int,int);

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

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

void Time::display(const Date &da)

{cout<

int main()

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

3. 设计一个Time类,包括三个私有数据成员:hour,minute,sec,用构造函数初始化, ,设计一个Date类,包括三个私有数据成员:month,day,year,也用构适函数初始化;设计一个普通函数display(?),将display分别设置为T ime类和Date类的友元函数,在主函数中分别定义两个带参数的对象t1(12,30,55),d1(3,25,2010), 调用desplay,输出年、月、日和时、分、秒。 #include using namespace std; class Date; class Time {public:

【第 20 页 共 48 页】


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

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

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

马上注册会员

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