面向对象程序设计visualc++与基于ACIS的几何造型第七章答案Word文档下载推荐.docx
《面向对象程序设计visualc++与基于ACIS的几何造型第七章答案Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计visualc++与基于ACIS的几何造型第七章答案Word文档下载推荐.docx(31页珍藏版)》请在冰点文库上搜索。
Destructorcalled.a=1,b=2
7、运行该程序输出结果如下所示。
Destructorcalled.
Constructor1called.
Constructor2called.
x=0,y=0
x=5,y=0
x=2,y=3
8、运行该程序输出结果如下所示。
Constructorcalled.0
Constructorcalled.5
Destructorcalled.5
5
9、运行该程序输出结果如下所示。
10、运行该程序输出结果如下所示。
DefaultConstructorcalled.
real=6.8,imag=0
real=5.6,imag=7.9
0+0I
6.8+0I
5.6+7.9I
real=1.2,imag=3.4
real=5,imag=0
1.2+3.4I
5+0I
11、答:
⑴该程序中用string.h中所包含的函数有3种,它们是strcpy()、strcat()和strlen().
⑵该程序中使用了函数重载。
它们是下述两个构造函数重载:
String()和String(constchar*str)。
⑶类中成员函数Setc()的功能是用来设置字符数组中某位置的一个指定字符。
⑷类中成员函数Getc()的功能是用来从某个字符数组中获取指定位置的字符。
⑸类中成员函数Append()的功能是在一个字符数组中追加一个指定的字符串,即将指定的字符串添加到已知串的后边。
⑹不行。
⑺该程序中有两处使用了new运算符。
⑻运行该程序输出结果如下所示。
empty.
astring.
9
i
thisastring.
四、改正以下程序中的错误。
1、该程序中point类的构造函数定义不正确,在main()中队数据成员的访问不正确,修改如下:
#include<
iostream.h>
classpoint
{
intx1,x2;
public:
point(intx,inty){point:
:
x=x;
point:
y=y;
}
voiddisp()
{
cout<
<
x1<
endl;
x2<
//……
};
voidmain()
pointdata(5,5);
data.disp();
2、在main()函数中的p.x+=5和p.y+=6两个语句是错误的,因为保护数据仅能被类的成员函数访问。
五、按下列要求编写程序。
1、程序内容如下所示。
classTest
Test(){}
Test(inti,intj=20)
t1=i;
t2=j;
t+=j-i;
staticintfun(Test&
T);
friendintadd(Test&
private:
intt1,t2;
staticintt;
intTest:
fun(Test&
T)
t+=T.t;
returnt;
intadd(Test&
intn=T.t1+T.t2;
returnn;
t=5;
Testa1,a2(10),a3(15,25);
cout<
add(a2)<
Test:
fun(a2)<
2、程序内容如下所示。
string.h>
classProduct
char*name;
intprice;
intquantity;
Product(char*n,intp,intq)
name=newchar[strlen(n)+1];
strcpy(name,n);
price=p;
quantity=q;
~Product()
if(name)
delete[]name;
name=0;
voidbuy(intmoney)
intn,r;
n=money/price;
if(n>
quantity)
"
数量不够"
else
quantity-=n;
r=money%price;
产品:
name<
单价:
price<
元顾客"
;
money<
元,买了"
n<
台,剩余"
r<
元"
voidget()const
元剩余"
quantity<
台"
Productp1("
电视机"
2000,15);
p1.buy(7000);
p1.get();
p1.buy(4500);
3、程序内容如下所示。
stdlib.h>
classCDate
private:
intm_nDay;
intm_nMonth;
intm_nYear;
public:
CDate();
CDate(intday,intmonth,intyear);
voidDisplay();
voidAddDay();
voidSetDate(intday,intmonth,intyear);
~CDate();
boolIsLeapYear();
//判断该年是否为闰年
CDate:
CDate()
CDate(intday,intmonth,intyear)
m_nDay=day;
m_nMonth=month;
m_nYear=year;
voidCDate:
Display()
charday[5];
charmonth[5];
charyear[5];
_itoa(m_nDay,day,10);
_itoa(m_nMonth,month,10);
_itoa(m_nYear,year,10);
day<
/"
month<
year<
AddDay()
m_nDay++;
if(IsLeapYear())
if((m_nMonth==2)&
&
(m_nDay==30))
m_nMonth++;
m_nDay=1;
return;
}
else
(m_nDay==29))
m_nMonth++;
if(m_nDay>
31)
if(m_nMonth==12)
m_nYear++;
m_nMonth=1;
SetDate(intday,intmonth,intyear)
~CDate()
boolCDate:
IsLeapYear()
boolbLeap;
if(m_nYear%4!
=0)
bLeap=false;
elseif(m_nYear%100!
bLeap=true;
elseif(m_nYear%400!
bLeap=false;
bLeap=true;
returnbLeap;
CDated;
d.SetDate(2001,2,28);
当前日期=>
:
d.Display();
d.AddDay();
当前日期加1=>
4、程序内容如下所示。
classTc
doubleunlead,lead,total;
intunprice,price;
Tc(){unprice=17;
price=16;
voidgetdata()
无铅汽油总量;
cin>
>
unlead;
有铅汽油总量;
lead;
total=unprice*unlead+price*lead;
voiddisp()
总收入:
total<
TcA;
A.getdata();
A.disp();
5、程序内容如下所示。
classCFactorial
intvalue;
intfact;
CFactorial(intval);
voidCalculateFactorial();
voidDisplay();
CFactorial:
CFactorial(intval)
value=val;
fact=1;
voidCFactorial:
CalculateFactorial()
inti=value;
while(i>
1)
fact*=i--;
value<
!
="
fact<
CFactorialA(5);
A.CalculateFactorial();
A.Display();
6、程序内容如下所示。
iomanip.h>
classrectangle
floatledge,sedge;
rectangle(){};
rectangle(floata,floatb)
ledge=a;
sedge=b;
floatarea()
returnledge*sedge;
voidaddarea(rectangler1,rectangler2)
总面积:
r1.ledge*r1.sedge+r2.ledge*r2.sedge<
rectangleA(3.5,2.5),B(4.2,3.8),C;
C.addarea(A,B);
7、程序内容如下所示。
voidshowlength()
周长:
(ledge+sedge)*2<
rectangletlength(rectangler2)
rectangletemp;
temp.ledge=ledge+r2.ledge;
temp.sedge=sedge+r2.sedge;
returntemp;
rectangleA(3.5,2.5),B(4.2,3.8);
A"
A.showlength();
B"
B.showlength();
rectangleC=A.tlength(B);
C"
C.showlength();
8、程序内容如下所示。
classLine
doublex1,x2,y1,y2;
Line(){};
Line(doublea,doubleb,doublec,doubled)
x1=a;
y1=b;
x2=c;
y2=d;
线段端点"
"
y1<
)-("
y2<
)"
friendtwoline(Linel1,Linel2)
doubler1=l2.y1*(l2.x2-l1.x1)-l1.y1*(l2.x2-l2.x1)+(l1.x1-l2.x1)*(l2.y2-l2.y1);
doubler2=(l1.y2-l1.y1)*(l2.x2-l2.x1)-(l1.x2-l1.x1)*(l2.y2-l2.y1);
doubler=r1/r2;
doublet=((l1.x1-l2.x1)+r*(l1.x2-l1.x1))/(l2.x2-l2.x1);
if(r>
0&
1&
t>
t<
elseif(r>
=1)
elseif((r>
=1||r<
=0)&
t>
LineA(2,2,18,18),B(1,12,19,5);
twoline(A,B);
9、本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(no),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。
cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。
程序内容如下所示。
classstudent
intno;
charname[10];
classcdegree
intmath;
intenglish;
intphy;
}degree;
voidgetdata()
学号:
no;
姓名:
name;
数学分数:
degree.math;
英语分数:
degree.english;
物理分数:
degree.phy;
no<
degree.math<
degree.english<
degree.phy<
studentstud;
stud.getdata();
stud.disp();
10、程序内容如下所示。
classStudent
intenglish,computer,total;
voidgetscore();
voiddisplay();
voidsort(Student*);
~Student();
voidStudent:
getscore()
输入英语成绩"
english;
输入计算机成绩"
computer;
total=english+computer;
sort(Student*p)
inttmp,i,j;
for(j=0;
j<
2;
j++)
for(i=0;
i<
i++)
if(total<
p->
total)
tmp=total;
total=p->
total;
p->
total=tmp;
tmp=english;
english=p->
english=tmp;
tmp=computer;
computer=p->
computer=tmp;
display()
英语="
english<
计算机="
computer<
总分="
Student*A[3];
for(intj=0;
3;
A[j]=newStudent;
学生"
j+1<
A[j]->
getscore();
inti;
for(j=0;
for(i=0;
A[i]->
sort(A[i+1]);
endl<
排序结果如下:
for(i=0;
display();
11、程序内容如下所示。
structlist//定义栈
intdata;
list*next;
classStack//定义一个栈操作类
list*ptr;
Stack(){ptr=NULL;
voidpush(inti);
intpop();
voidStack:
push(intx)//入栈成员函数
list*newnode=newlist;
newnode->
data=x;
next=ptr;
ptr=newnode;
intStack:
pop()//出栈成员函数
list*top;
intvalue;
value=ptr->
data;
top=ptr;
ptr=ptr->
next;
deletetop;
returnvalue;
StackA;
intarr[]={5,2,8,1,4,3,9,7,6};
入栈顺序:
for(inti=0;
9;
arr[i]<
"
A.push(arr[i]);
出栈顺序:
A.pop()<