c - - 面向对象程序设计 - 课后答案 - 中国铁道出版社 - 陈维兴 - 林小茶 2

2026/4/25 17:00:15

counter b(10); for(int k=1;k

return 0;

}

习题:[3_12]根据注释语句的提示,实现类Date的成员函数 #include class Date { public:

void printDate(); //显示日期

void setDay(int d);//设置日期值 void setMonth(int m);//设置月的值 void setYear(int y);//设置年的值 private:

int day,month,year; };

void main()

{ Date testDay;

testDay.setDay(5); testDay.setMonth(10); testDay.setYear(2003); testDay.printDate(); }

void Date::printDate()

{ cout<<\ cout<

void Date::setDay(int d) { day=d; }

void Date::setMonth(int m) { month=m; }

void Date::setYear(int y) { year=y; }

习题:[3_13]下面定义了一个类date,根据主程序的提示,实现重载构造函数date() #include #include class date { public:

date(int d,int m,int y); date::date(); void show();

private:

int day,month,year; };

void date::show()

{ cout<

main()

{ date idate(28,10,1949);//构造函数的参数为3个整数 idate.show();

date indate; //构造函数没有参数,数据通过键盘直接输入 indate.show(); return 0; }

//解:重载构造函数的实现如下: date::date(int d,int m,int y) { day=d; month=m; year=y; }

date::date()

{ cout<<\ cin>>day; cin>>month; cin>>year;

}//注意:敲数据时要如:8 回车 9回车 2005回车

习题:[3_14]建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol(),用来显示每个cylinder对象的体积。 #include class cylinder { public:

cylinder(double a,double b); void vol(); private:

double r,h; double volume;

};

cylinder::cylinder(double a,double b) { r=a; h=b;

volume=3.141592*r*r*h;

}

void cylinder::vol()

{ cout<<\

void main()

{

cylinder x(2.2,8.09); x.vol(); }

习题:[3_15]建立一个Stock类,含有股票代码和股票现价两个数据成员。用new自动为Stock类的对象分配内存,并将股票代码“600001”,现价8.89存入内存的相应域中。 #include #include class Stock { public:

void set(char *c,float pr); void print(); private:

char Stockcode[7]; float price;

};

void Stock::set(char *c,float pr) { strcpy(Stockcode,c); price=pr; }

void Stock::print()

{ cout<

main()

{ Stock *p;

p=new Stock; //为对象分配空间 if(!p) //判断分配是否成功 { cout<<\ return 1; }

p->set(\为对象赋值 p->print(); //显示对象 delete p; return 0; }

习题:[3_16]声明一个栈类,利用栈操作实现将输入字符串反向输出的功能 #include //#include //#include #include const int SIZE=10; class stack

{ public:

stack() //构造函数 { tos=0; }

void push(char ch);//将数据ch压入栈 char pop(); //将栈顶数据弹出栈

char stck[SIZE]; //数组,用于存放栈中数据SIZE上面赋值为10 int tos; //栈顶位置(数组下标) };

//stack::stack() //构造函数,初始化栈 //{ tos=0; }

void stack::push(char ch)//压入栈 {

if(tos==SIZE) {

cout<<\栈是满的 return; }

stck[tos]=ch; tos++;

}

char stack::pop()//弹出栈

{

if(tos==0)

{ cout<<\栈是空的 return 0; }

tos--;

return stck[tos]; }

void main() { int i;

char str[20];

char re_str[20];

cout<<\ cin>>str; stack ss;

for(i=0;i

for(i=0;i

cout<<\ cout<


c - - 面向对象程序设计 - 课后答案 - 中国铁道出版社 - 陈维兴 - 林.doc 将本文的Word文档下载到电脑
搜索更多关于: c - - 面向对象程序设计 - 课后答案 - 中国铁道出 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

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