- 1、本文档共5页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
C答案2
习题参考答案:
第2章类与对象
一、填空题
1.delete 2.组合类 3.所在类类名 4.当前类的引用类型5.new Example[10]
6.类的成员 7.this . 8.对象 初始化 对象 9.默认 10.拷贝构造函数
二、选择题
1.C 2.D 3.C 4.A 5.A 6.D 7.D 8. C 9. C10.C
三、程序填空题
1.Age(x) Dog d return Age;
2.10 5 5 5
6 3 3 3
3.x=10y=20
X=10y=20
4.x=1y=2
x=3y=4
x=5y=6
5.a=100
b=200
6.m=100
7. It is ok!
8.
四、编程题
1. //声明一个tree类,有成员ages,成员函数grow(int years)对ages加上years,age()显示tree对象的ages值。
#include iostream
using namespace std;
class tree
{
private:
int ages;
public :
tree():ages(0){ }
void grow(int years)
{
ages=ages+years;
}
void age()
{
coutagesendl;
}
};
void main()
{
tree t;
t.grow (5);
t.age ();
}
2./*定义一个类Area求圆的面积,该类有两个私有数据成员radius和area,两个公有成员函数:get_radius()(用来输入radius的值,并计算area的值)和disp_area()(用来输出圆的面积)。 实现:class Area{?? private:??????? int radius;??????? float area;?? public:??????? void get_radius()??????? {???????????? cout请输入半径:;???????????? cinradius;???????????? area=3.14*radius*radius;???????? }???????? void disp_area()???????? {???????????? cout圆的面积为:;???????????? coutarea;????????? }};
#include iostream
using namespace std;
class tree
{
private:
int ages;
public :
tree():ages(0){}
void grow(int years);
void age();
};
void tree::grow(int years)
{
ages=ages+years;
}
void tree::age()
{
coutagesendl;
}
void main()
{
tree t;
t.grow (5);
t.age ();
}
4.//1.h
#include iostream
using namespace std;
class Tdate{
public:
void Set(int,int,int) ;
int IsLeapYear() ; //判断是否闰年
void Print() ;
private:
int month ;
int day;
int year;
};
//1.cpp
#include “1.h”
void Tdate::Set(int m, int d, int y)
{
month=m;
day=d;
year=y;
}
void Tdate::Print()
{
coutmonth”/”day”/”yearendl;
}
int Tdate::IsLeapYear()
{
return(year%4==0 year%100!=0) || (year %400==0);
}
//2.cpp
#include “1.h”
void main()
{
Tdate s;
Tdate t;
s.Set(2,15,1998);
t.Set(3,15,1997);
s.Print();
t.Print();
}
5. #include iostream
using namespace std;
class Point
{
private:
int x,y;
public:
Point(){}
Point(int m,
文档评论(0)