logo资料库

C++习题答案.pdf

第1页 / 共51页
第2页 / 共51页
第3页 / 共51页
第4页 / 共51页
第5页 / 共51页
第6页 / 共51页
第7页 / 共51页
第8页 / 共51页
资料共51页,剩余部分请下载后查看
VC++程序设计 主讲教师:贾澎涛 《Visual C++面向对象编程教程》 第三章习题参考答案 3-35 以下程序有什么错误?如有请将它改正。 源程序错误的原因是:在类外访问私有数据成员,可以将 x,y 声明为公有数据成员,但破坏了 数据的私有性,提倡以下两种更改方式: 改正后 1:使用构造函数为私有数据成员赋值 #include class Point { public: int x,y; Point(int a,int b) { x=a; y=b; } void Display() {cout<<"x="< class Point { public: }; int main() { void Setxy(int a,int b) { } void Display() { } cout<<"x="<
VC++程序设计 主讲教师:贾澎涛 char m_strName[20]; long m_ID; strcpy(m_strName,strName); m_ID=ID; return x.m_ID; point1.Setxy(100,200); point1.Display(); return 0; Person(char* strName,long ID) { } static long GetID(Person x) { } } 3-36 写出下列程序运行后的输出结果。 The time is:14:52:0 3-37 以下程序有什么错误?并予以改正 1)错误原因是:静态成员函数 GetID 访问了一个非静态数据成员 m_ID,在主函数中用类名 访问静态成员函数 Person::GetID()时,不能确定访问的非静态成员 m_ID 属于哪一个对象。 因此改为: #include #include class Person { private: public: }; void main() { } 2)错误原因为当基类有构造函数且构造函数有参数且没有缺省值时,派生类必须定义构造函 数负责基类的构造,改为: #include class Point { protected: public: Person person1("liujun",1101640524); cout<<"ID="<
VC++程序设计 主讲教师:贾澎涛 int radius; radius=r; return x; } int getX() { } int getY() { } return y; return radius; Circle(int a=0,int b=0, int r=0 ) { } int getRadius() { } }; class Circle:public Point { protected: public: }; main() { } 3)错误是当 C 类的对象 c 访问 GetData()函数时,不知是访问从 A 继承来的还是从 B 继承来 的,产生二义性:改为: #include class A { protected: int a; public: void SetDate(int x) { } int GetDate() { Circle c(100,150,200); cout<<"x="<
} VC++程序设计 主讲教师:贾澎涛 b=y; return b; void SetDate(int x,int y) { } a=x; b=y; void SetDate(int y) { } int GetDate() { } }; class B { protected: int b; public: }; class C:public A,public B { public: }; main() { } 3-38 写出下列程序的运行结果 1)A::show A::show 2)A::destructor 3)144 6.25 12.25 4)a=60,b=90 3-39 一个名为 CPerson 的类有如下属性:姓名、身份证号、性别和年龄,请用 C++语言定义 这个类,并为上述属性定义相应的方法。 #include #include class CPerson { C c; c.SetDate(30,70); cout<<"a="<
VC++程序设计 主讲教师:贾澎涛 char Name[10]; char ID[20]; char Sex[4]; int Age; CPerson(char *na,char *id,char *se,int ag); void Show(); strcpy(Name,na); strcpy(ID,id); strcpy(Sex,se); Age=ag; cout<<"姓名:"< class date{ public: date() { } date(int yr,int mo,int da); CPerson person("王三","610102198506041952","男",21); person.Show(); int year; int month; int day; bool flag; year=0; month=0; day=0; 5
VC++程序设计 主讲教师:贾澎涛 year=yr; month=mo; day=da; flag=false; if(mo>=1&&mo<=12&&da>=1&&da<=31) { } else { } flag=true; void setdate(); int getyear(); int getmonth(); int getday(); void addday(); void show(); }; date::date(int yr,int mo,int da) { } void date::setdate() { } void date::show() { } cout<<"请输入年分"<>year; cout<<"请输入月份(1-12)"<>month; while(month<1||month>12) { } cout<<"请输入日期(1-31)"<>day; while(day<1||day>31) { } flag=false; cout<<"输入有误,请重新输入月份(1-12)"<>month; cout<<"输入有误,请重新输入日期(1-31)"<>day; cout<
VC++程序设计 主讲教师:贾澎涛 return day; return year; return month; day++; if(month==2) //判断是否是二月 { bool leapyear; int date::getyear() { } int date::getmonth() { } int date::getday() { } void date::addday() { leapyear=((year%4==0&&year%100!=0)||(year%400==0));//定义闰年 if(leapyear) else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12) if(day>31)//若不是二月月大时,Day=1,Mon 增加一个月 else { if(day>28)//若不是闰年的二月当 Day 大于 28 时,Day=1,Mon 增加一个月 { day=1; } } { if(day>29)//若是闰年的二月当 Day 大于 29 时,Day=1,Mon 增加一个月 { day=1; } } month++; month++; } { { day=1; month++; } } else 7
{ VC++程序设计 主讲教师:贾澎涛 month=1; } { } { day=1; } month++; date d1(1999,5,30); d1.show(); d1.setdate(); d1.show(); cout<<"日期增加一天后为:"; d1.addday(); d1.show(); if(day>30)//若不是二月月小时,Day=1,Mon 增加一个月 if(month>12)//若月大于 12 则 Mon=1,Year 增加一年 year++; } void main() { } 3-41 建立一个名为 Student 的类,该类有以下几个私有成员变量:学生姓名、学号、性别和 年龄。还有以下两个成员函数:一个用于初始化学生姓名、学号、性别和年龄的构造函数, 一个用于输出学生信息的函数。编写一个主函数,声明一个学生对象,然后调用成员函数在 屏幕上输出学生信息。 #include #include class Student { public: private: }; Student::Student(char *name,char *num,char *sex,int age) { Student(char *name,char *num,char *sex,int age); ~Student(); void show(); char *Name; char *Num; char *Sex; int Age; Name=new char[strlen(name)+1]; //注意字符串的赋值 8
分享到:
收藏