面向对象程序设计 C++山师 第七章习题答案.docx
《面向对象程序设计 C++山师 第七章习题答案.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计 C++山师 第七章习题答案.docx(15页珍藏版)》请在冰豆网上搜索。
面向对象程序设计C++山师第七章习题答案
第七章习题答案
一、选择填空
1、D2、A3、B4、C5、A6、C7、B8、D9、
二、判断下列描述的正确性,对者划√,错者划×。
1、√2、×3、×4、×5、√6、×7、√8、√9、√10、×
11、√12、√13、×14、√15、√16、×17、√18、√
三、分析下列程序的输出结果。
1、
(1)上述结构的DAG图如下所示。
(2)无二义性
(3)无二义性
2、
(1)无(2无(3)有(4)无(5)有
四、分析下列程序的输出结果
1、运行该程序输出如下结果。
(1,2)
5,6
(6,9)
2、该程序的输出结果如下所示
(1,2)
(6,9)
5,6
(6,9)
3、该程序的输出结果如下:
(13,22,30,40)
4、运行该程序输出结果如下所示。
D2:
:
display()
pri1=4,pri2=5
pri4=6
pri12=7
D2:
:
display()
pri1=12,pri2=9
pri4=7
pri12=8
5、该程序输出结果如下所示:
D2:
:
display()
pri1=1,pri2=4
pri4=6
pri12=7
D2:
:
display()
pri1=9,pri2=8
pri4=7
pri12=8
6、该程序输出结果如下所示:
baseclass
baseclass
baseclass
derive1class
derive2class
五、按下列要求编写程序。
1、程序内容如下所示。
#include
#include
classperson
{
intno;
charname[10];
public:
voidinput()
{
cout<<"编号:
";cin>>no;
cout<<"姓名:
";cin>>name;
}
voiddisp()
{
cout<<"编号:
"<cout<<"姓名:
"<}
};
classstudent:
publicperson
{
private:
chardepart[6];
intdegree;
public:
voidinput()
{
person:
:
input();
cout<<"班号:
";cin>>depart;
cout<<"成绩:
";cin>>degree;
}
voiddisp()
{
person:
:
disp();
cout<<"班号:
"<cout<<"成绩:
"<}
};
classteacher:
publicperson
{
private:
charprof[10];
chardepart[10];
public:
voidinput()
{
person:
:
input();
cout<<"职称:
";cin>>prof;
cout<<"部门:
";cin>>depart;
}
voiddisp()
{
person:
:
disp();
cout<<"职称:
"<cout<<"部门:
"<}
};
voidmain()
{
students1;
teachert1;
cout<<"输入一个学生数据:
\n";s1.input();
cout<<"输入一个教师数据:
\n";t1.input();
cout<<"显示一个学生数据:
\n";s1.disp();
cout<<"显示一个教师数据:
\n";t1.disp();
}
2、程序内容如下所示。
#include
#include
classstring
{
intlength;
char*contents;
public:
intget_length(){returnlength;}
char*get_contents(){returncontents;}
~string(){deletecontents;}
intset_contents(intin_length,char*in_contents);
intset_contents(char*in_contents);
voidprint(){cout<};
classedit_string:
publicstring
{
intcursor;
public:
intget_cursor_pos(){returncursor;}
voidmove_cursor(inthow_much){cursor=how_much;}
intadd_at_cursor(string*new_text);
intrepl_at_cursor(string*new_text);
voiddele_at_cursor(inthow_much);
};
intstring:
:
set_contents(intin_length,char*in_contents)
{
length=in_length;
if(!
contents)deletecontents;
contents=newchar[length+1];
strcpy(contents,in_contents);
returnlength;
}
intstring:
:
set_contents(char*in_contents)
{
length=strlen(in_contents);
if(!
contents)deletecontents;
contents=newchar[length+1];
strcpy(contents,in_contents);
returnlength;
}
intedit_string:
:
add_at_cursor(string*new_text)
{
intn,k,m;
char*cp,*pt;
n=new_text->get_length();
pt=new_text->get_contents();
cp=this->get_contents();
m=this->get_length();
char*news=newchar[m+n+1];
for(inti=0;inews[i]=cp[i];
k=i;
for(intj=0;jnews[i]=pt[j];
cursor=i;
for(j=k;jnews[i]=cp[j];
news[i]='\0';
set_contents(news);
deletenews;
returncursor;
}
intedit_string:
:
repl_at_cursor(string*new_text)
{
intn,m;
char*pt,*news;
n=new_text->get_length();
pt=new_text->get_contents();
m=this->get_length();
news=newchar[m>n+cursor?
m+1:
n+cursor+1];
news=this->get_contents();
for(inti=cursor,j=0;inews[i]=pt[j];
if(mcursor=i;
set_contents(news);
deletenews;
returncursor;
}
voidedit_string:
:
dele_at_cursor(inthow_much)
{
intm;
char*cp,*news;
cp=this->get_contents();
m=this->get_length();
for(inti=cursor;icp[i]=cp[i+how_much];
cp[i]='\0';
}
voidmain()
{
strings1;
edit_strings2;
char*cp;
s1.set_contents("Object_OrientedProgramming");
cp=s1.get_contents();
s2.set_contents(cp);
s2.print();
s2.move_cursor(15);
s1.set_contents("Windwos");
s2.add_at_cursor(&s1);
s2.print();
s2.move_cursor(6);
s2.dele_at_cursor(9);
s2.print();
s1.set_contents("TTT");
s2.repl_at_cursor(&s1);
s2.print();
}
3、程序内容如下所示。
#include
classvehicle
{
protected:
intwheels;
floatweight;
public:
vehicle(intwheels,floatweight);
intget_wheels();
floatget_weight();
floatwheel_load();
voidprint();
};
classcar:
vehicle
{
intpassenger_load;
public:
car(intwheels,floatweight,intpassengers=4);
intget_passengers();
voidprint();
};
classtruck:
vehicle
{
intpassenger_load;
floatpayload;
public:
truck(intwheels,floatweight,intpassengers=2,floatmax_load=240000.00);
intget_passengers();
floatefficiency();
voidprint();
};
vehicle:
:
vehicle(intwheels,floatweight)
{
vehicle:
:
wheels=wheels;
vehicle:
:
weight=weight;
}
intvehicle:
:
get_wheels()
{
returnwheels;
}
floatvehicle:
:
get_weight()
{
returnweight/wheels;
}
voidvehicle:
:
print()
{
cout<<"车轮:
"<"<cout<<"重量:
"<"<}
car:
:
car(intwheels,floatweight,intpassengers):
vehicle(wheels,weight)
{
passenger_load=passengers;
}
intcar:
:
get_passengers()
{
returnpassenger_load;
}
voidcar:
:
print()
{
cout<<"小车:
"<vehicle:
:
print();
cout<<"载人:
"<"<cout<}
truck:
:
truck(intwheels,floatweight,intpassengers,floatmax_load):
vehicle(wheels,weight)
{
passenger_load=passengers;
payload=max_load;
}
inttruck:
:
get_passengers()
{
returnpassenger_load;
}
floattruck:
:
efficiency()
{
returnpayload/(payload+weight);
}
voidtruck:
:
print()
{
cout<<"卡车"<vehicle:
:
print();
cout<<"载人:
"<"<cout<<"效率:
"<cout<}
voidmain()
{
carcar1(4,1000,5);
trucktru1(10,5000,3,340000);
car1.print();
tru1.print();
}
4、程序内容如下所示。
#include
#include
classemployee
{
protected:
intno;
charname[10];
floatsalary;
public:
employee()
{
cout<<"职工编号:
";
cin>>no;
cout<<"职工姓名:
";
cin>>name;
salary=0;
}
voidpay(){}
voiddisplay(){}
};
classtechnician:
publicemployee
{
private:
floathourlyrate;
intworkhours;
public:
technician(){hourlyrate=100;}
voidpay()
{
cout<";
cin>>workhours;
salary=hourlyrate*workhours;
}
voiddisplay()
{
cout<<"兼职技术人员:
"<"<<<")"<<"本月工资:
"<}
};
classsalesman:
virtualpublicemployee
{
protected:
floatcommrate;
floatsales;
public:
salesman(){commrate=0.04;}
voidpay()
{
cout<";
cin>>sales;
salary=sales*commrate;
}
voiddisplay()
{
cout<<"销售员:
"<"<"<}
};
classmanager:
virtualpublicemployee
{
protected:
floatmonthlypay;
public:
manager(){monthlypay=8000;}
voidpay(){salary=monthlypay;}
voiddisplay()
{
cout<<"经理:
"<"<"<}
};
classsalesmanager:
publicmanager,publicsalesman
{
public:
salesmanager()
{
monthlypay=5000;
commrate=0.005;
}
voidpay()
{
cout<";
cin>>sales;
salary=monthlypay+commrate*sales;
}
voiddisplay()
{
cout<<"销售经理:
"<"<"<}
};
voidmain()
{
managerm1;
techniciant1;
salesmans1;
salesmanagersm1;
m1.pay();
m1.display();
t1.pay();
t1.display();
s1.pay();
s1.display();
sm1.pay();
sm1.display();
}