C++程序设计与应用基础第四章继承与派生习题答案

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第四章继承与派生
1、填空题
1)在派生类中使用基类的成员,可以显示地使用___成员名限定符__来使用基类成员。

2)垂直访问时,保护成员的访问控制权限与____公有_____成员相同
3)指向基类的对象的指针变量也可以指向___派生类____的对象。

4)若类Y是类X的私有派生类,类Z是类Y的派生类,则类Z__不能___访问类X的保护成员与公有成员。

5)在划线处填上适当的语句,完成下列程序。

#include <math.h>
class Point
{
public:
point(double a,double b,double c){___X=a;Y=b;Z=c;___}
double Getx(){return X;}
double Gety(){return Y;}
double Getz(){return Z;}
private:
______double X;______
protected:
_____double Y,Z;_____
};
class Line :_____public______Point
{
public:
Line(double a ,double b,double c,double d)_____:point(a,b,c)__{_ k=d;_ }
void show()
{
cout<<Getx()<<endl;
cout<<Gety()<<““<<.<<<endl;
cout<<Getz()<<““<<Z<<endl;
cout<<K<<endl;
}
private:
double k;
};
void main()
{
Line obj(1.2,3.4,5.6,7.8);
obj.show();
}
6)设置虚基类的目的是__解决二义性问题___,可通过___关键字virtual__标识虚基类。

7)利用继承能够实现___软件复用______。

这种实现缩短了程序的开发时间,促使开发人员复用已经测试和调试好的高质量软件。

2、编程题
1)编写一个程序,其中有一个简单的串类String,包含设置字符串、返回字符串长度及内容等功能。

另有一个具有编辑功能的串类EditString,它的基类是String,在其中设置一个光标,使其能支持在光标处的插入、替换和删除等编辑功能。

答案:
#include <iostream.h>
#include <string.h>
class String
{
public:
~String() {delete[] str;}
int SetStr(int l,char *s);
int SetStr(char *S);
int GetLen(){return len;}
char *GetStr(){return str;}
void Print(){cout<<str<<endl;}
private:
int len;
char *str;
};
class EditString:public String
{
public:
void MoveCursor(int off){cursor=off;}
int Add(String *s);
int Replace(String *s);
void Delete(int pos);
int GetCursor(){return cursor;}
private:
int cursor;
};
int String::SetStr(int l,char *s)
{
len=1;
if(!str) delete str;
str=new char[len+1];
strcpy(str,s);
return len;
}
int String::SetStr(char *s)
{
len=strlen(s);
if(!str) delete str;
str=new char[len+1];
strcpy(str,s);
return len;
}
int EditString::Add(String *s)
{
int n,k,m;
char *cp,*pt;
n=s->GetLen();
pt=s->GetStr();
cp=this->GetStr();
m=this->GetLen();
char *news=new char[n+m+1];
for(int i=0;i<cursor;i++)
news[i]=cp[i];
k=i;
for(int j=0;j<n;i++,j++)
news[i]=pt[j];
cursor=i;
for(j=k;j<m;j++,i++)
news[i]=cp[j];
news[i]='\0';
SetStr(news);
delete news;
return cursor;
}
int EditString::Replace(String *s)
{
int n,m;
char *pt,*news;
n=s->GetLen();
pt=s->GetStr();
m=this->GetLen();
news=new char[m>n+cursor?m+1:n+cursor+1];
news=this->GetStr();
for(int i=cursor,j=0;i<n+cursor;j++,i++)
news[i]=pt[j];
if(m<n+cursor) news[i]='\0';
cursor=i;
SetStr(news);
delete news;
return cursor;
}
void EditString::Delete(int pos)
{
int m;
char *cp,*news;
cp=this->GetStr();
m=this->GetLen();
for(int i=cursor;i<m;i++)
cp[i]=cp[i+pos];
cp[i]='\0';
}
void main()
{
String s1;
EditString s2;
char *cp;
s1.SetStr("Object-oriented programming!");
cp=s1.GetStr();
s2.SetStr(cp);
s2.Print();
s2.MoveCursor(16);
s1.SetStr("Windows");
s2.Add(&s1);
s2.Print();
s2.MoveCursor(7);
s2.Delete(9);
s2.Print();
s1.SetStr("TTT");
s2.Replace(&s1);
s2.Print();
}
2)设计一个程序,有一个汽车类vehicle,它具有一个需要传递参数的构造函数,类中的数据成员包括:车轮个数wheels和车重weight作为保护成员:小车类car是它的私有派生类,其中包含载人数passengers;卡车类truck是vehicle的私有派生类,其中包含载人数passengers 和载重量payload。

每个类都有相关数据的输出方法。

答案:
#include <iostream.h>
class vehicle
{
public:
vehicle(int wh,double we){wheels=wh;weight=we;}
int GetWheels(){return wheels;}
double GetWeight(){return weight;}
void Print(){cout<<"has"<<wheels<<"wheels,weights"<<weight<<"tons";}
protected:
int wheels;
double weight;
};
class car: public vehicle
{
public:
car(int wh,double we,int pa=4):vehicle(wh,we),passengers(pa){};
int GetPassengers(){return passengers;}
void print()
{
cout<<"The Car";
vehicle::Print();
cout<<" and "<<passengers<<"passengars."<<endl;
}
private:
int passengers;
};
class truck: private vehicle
{
public:
truck(int wh,int we,int pa=2,double load=24):vehicle(wh,we),passengers(pa),payload(load){} int Getpassengers(){return passengers;}
double GetPayload(){return payload;}
void Print()
{
cout<<"The truck";
vehicle::Print();
cout<<","<<passengers<<"passengers and"<<payload<<"load."<<endl;
}
private:
int passengers;
double payload;
};
void main()
{
car obj1(4,2);
truck obj2(6,5);
obj1.Print();
obj2.Print();
}
3)大学里有这样几类人员:学生、教师、职员和在职读书的教师。

给出这几类人员的类描述。

答案:
#include <iostream.h>
#include <string.h>
class Person
{
public:
Person(const char* n)
{
name=new char[strlen(n)+1];
strcpy(name,n);
}
void Print() const{cout<<"Name:"<<name<<endl;}
protected:
char* name;
};
class Student: virtual public Person
{
public:
Student(const char* n, const char* m): Person(n)
{
major=new char[strlen(m)+1];
strcpy(major,m);
}
void Print() const
{
Person::Print();
cout<<"Major:"<<major<<endl;
}
protected:
char* major;
};
class Staff: virtual public Person
{
public:
Staff(const char* n,const char* d):Person(n)
{
dept=new char[strlen(d)+1];
strcpy(dept,d);
}
void Print() const
{
Person::Print();
cout<<"Department:"<<dept<<endl;
}
protected:
char* dept;
};
class Teacher:public Staff
{
public:
Teacher(const char* n,const char* d,const char* l):Person(n),Staff(n,d)
{
lesson=new char[strlen(l)+1];
strcpy(lesson,l);
}
void Print() const
{
Staff::Print();
cout<<"Lesson:"<<lesson<<endl;
}
protected:
char* lesson;
};
class StudentTeacher:public Student,public Teacher
{
public:
StudentTeacher(const char* n,const char* m,const char* d,const char* l):Person(n),Student(n,m),Teacher(n,d,l){}
void Print() const
{
Student::Print();
cout<<"Department:"<<dept<<endl;
cout<<"Lesson:"<<lesson<<endl;
}
};
void main()
{
Student stu("Mike","software Engineering");
Staff sta("Jason","Management");
Teacher t("Tim","Computer","C++");
StudentTeacher st("Sam","Computer Application","Computer","C++");
stu.Print();
sta.Print();
t.Print();
st.Print();
}。

相关文档
最新文档