第一范文网 - 专业文章范例文档资料分享平台

西安石油大学大二(上)C++面向对象程序设计课件各章习题

来源:用户分享 时间:2025/5/17 4:33:53 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

{ Car myCar;

SportsCar mySportsCar; int c = 5; float d = 5.0; myCar.f(c); mySportsCar.f(c); mySportsCar.f(d); mySportsCar.d();

}

程序阅读和设计题:

9.17 阅读程序,给出运行结果: (1)

#include \class A {

private: int a,b; public:

A(int i,int j) { a=i; b=j;

}

void move(int x,int y) { a+=x; b+=y; }

void show() { cout<

};

class B: private A {

private:

29

int x,y;

public:

B(int i,int j,int k,int l):A(i,j) { x=k; y=l;

}

void show() { cout<

void fun() { move(2,4);

}

void f1() { A::show(); }

};

void main() { A a1(10,10); a1.show(); B b1(5,5,6,6); b1.fun(); b1.show(); b1.f1(); }

2)

#include class Point { int x,y; public: Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<\

}

~Point() {

30

cout<<\

}

};

class Circle { Point center; //圆心位置

int radius; //半径

public: Circle(int cx,int cy, int r):center(cx,cy),radius(r) { cout<<\

}

~Circle() {cout<<\

};

void main() {

Circle c(3,4,5); }

3)

#include #include class Point { int x,y; public: Point(int x1=0, int y1=0) :x(x1), y(y1) { cout<<\

}

~Point() { cout<<\

}

};

class Text {

char text[100]; //文字内容 public:

Text(char * str) { strcpy(text,str); cout<<\ }

~Text() {cout<<\

};

31

class CircleWithText : public Point,public Text { public: };

void main() { }

CircleWithText cm(3,4,\

CircleWithText(int cx,int cy, char *msg): Point(cx,cy),Text(msg) { }

~CircleWithText() {cout<<\

cout<<\

(4)

#include class Date { public:

void SetDate(int y,int m,int d){ Year=y; Month=m; Day=d; } void PrintDate(){ cout<

Date(int y,int m,int d){SetDate(y,m,d);} protected:

int Year,Month,Day; };

class Time { public:

void SetTime(int h,int m,int s){ Houre=h; Minutes=m; Seconds=s;} void PrintTime()

{ cout<

Time(int h,int m,int s){SetTime(h,m,s);} protected:

int Houre, Minutes, Seconds; };

class Date_Time: public Date, public Time { public:

32

西安石油大学大二(上)C++面向对象程序设计课件各章习题.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c0a2hz4wq135dq8n1sb5y_8.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top