c面向对象程序设计课后习题答案谭浩强版.docx

上传人:b****2 文档编号:1573646 上传时间:2022-10-23 格式:DOCX 页数:18 大小:17.17KB
下载 相关 举报
c面向对象程序设计课后习题答案谭浩强版.docx_第1页
第1页 / 共18页
c面向对象程序设计课后习题答案谭浩强版.docx_第2页
第2页 / 共18页
c面向对象程序设计课后习题答案谭浩强版.docx_第3页
第3页 / 共18页
c面向对象程序设计课后习题答案谭浩强版.docx_第4页
第4页 / 共18页
c面向对象程序设计课后习题答案谭浩强版.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

c面向对象程序设计课后习题答案谭浩强版.docx

《c面向对象程序设计课后习题答案谭浩强版.docx》由会员分享,可在线阅读,更多相关《c面向对象程序设计课后习题答案谭浩强版.docx(18页珍藏版)》请在冰豆网上搜索。

c面向对象程序设计课后习题答案谭浩强版.docx

c面向对象程序设计课后习题答案谭浩强版

第一章

5:

#include

usingnamespacestd;

intmain()

{

cout<<"This"<<"is";

cout<<"a"<<"C++";

cout<<"program."<

return0;

}

6:

#include

usingnamespacestd;

intmain()

{

inta,b,c;

a=10;

b=23;

c=a+b;

cout<<"a+b=";

cout<

cout<

return0;

}

7:

#include

usingnamespacestd;

intmain()

{

inta,b,c;

intf(intx,inty,intz);

cin>>a>>b>>c;

c=f(a,b,c);

cout<

return0;

}

intf(intx,inty,intz)

{

intm;

if(x

elsem=y;

if(z

return(m);

}

8:

#include

usingnamespacestd;

intmain()

{

inta,b,c;

cin>>a>>b;

c=a+b;

cout<<"a+b="<

return0;

}

9:

#include

usingnamespacestd;

intmain()

{intadd(intx,inty);

inta,b,c;

cin>>a>>b;

c=add(a,b);

cout<<"a+b="<

return0;

}

intadd(intx,inty)

{intc;

c=x+y;

return(c);

}

10:

#include

usingnamespacestd;

intmain()

{voidsort(intx,inty,intz);

intx,y,z;

cin>>x>>y>>z;

sort(x,y,z);

return0;

}

voidsort(intx,inty,intz)

{

inttemp;

if(x>y){temp=x;x=y;y=temp;}2-4-12-4-22-5-12-5-2Boxbox1Boxbox1core;

intk=0;

for(inti=1;i<5;i++)

if(arr[i].score>max_score){max_score=arr[i].score;k=i;}

cout<

}

6:

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){num=n;score=s;}

voiddisplay(){cout<

private:

intnum;

floatscore;

};

intmain()

{Studentstud(101,;

();

(101,;

();

return0;

}

7:

解法一

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

voidchange(intn,floats){num=n;score=s;}

voiddisplay(){cout<

otal();

Product:

:

display();

return0;

}

10:

#include

usingnamespacestd;

classDate;

classTime

{public:

Time(int,int,int);

friendvoiddisplay(constDate&,constTime&);

private:

inthour;

intminute;

intsec;

};

Time:

:

Time(inth,intm,ints)

{hour=h;

minute=m;

sec=s;

}

classDate

{public:

Date(int,int,int);

friendvoiddisplay(constDate&,constTime&);

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty)

{month=m;

day=d;

year=y;

}

voiddisplay(constDate&d,constTime&t)

{

cout<<<<"/"<<<<"/"<<<

cout<<<<":

"<<<<":

"<<<

}

intmain()

{

Timet1(10,13,56);

Dated1(12,25,2004);

display(d1,t1);

return0;

}

11:

#include

usingnamespacestd;

classTime;

classDate

{public:

Date(int,int,int);

friendTime;

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty):

month(m),day(d),year(y){}

classTime

{public:

Time(int,int,int);

voiddisplay(constDate&);

private:

inthour;

intminute;

intsec;

};

Time:

:

Time(inth,intm,ints):

hour(h),minute(m),sec(s){}

voidTime:

:

display(constDate&d)

{

cout<<<<"/"<<<<"/"<<<

cout<

"<

"<

}

intmain()

{

Timet1(10,13,56);

Dated1(12,25,2004);

(d1);

return0;

}

12:

#include

usingnamespacestd;

template

classCompare

{public:

Compare(numtypea,numtypeb);

numtypemax();

numtypemin();

private:

numtypex,y;

};

template

Compare:

:

Compare(numtypea,numtypeb)

{x=a;y=b;}

template

numtypeCompare:

:

max()

{return(x>y)?

x:

y;}

template

numtypeCompare:

:

min()

{return(x

x:

y;}

intmain()

{Comparecmp1(3,7);

cout<<()<<"istheMaximumoftwointegernumbers."<

cout<<()<<"istheMinimumoftwointegernumbers."<

Comparecmp2,;

cout<<()<<"istheMaximumoftwofloatnumbers."<

cout<<()<<"istheMinimumoftwofloatnumbers."<

Comparecmp3('a','A');

cout<<()<<"istheMaximumoftwocharacters."<

cout<<()<<"istheMinimumoftwocharacters."<

return0;

}

第四章

1:

#include

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

doubleget_real();

doubleget_imag();

voiddisplay();

private:

doublereal;

doubleimag;

};

doubleComplex:

:

get_real()

{returnreal;}

doubleComplex:

:

get_imag()

{returnimag;}

voidComplex:

:

display()

{cout<<"("<

Complexoperator+(Complex&c1,Complex&c2)

{

returnComplex()+(),()+());

}

intmain()

{Complexc1(3,4),c2(5,-10),c3;

c3=c1+c2;

cout<<"c3=";

();

return0;

}

2:

#include

usingnamespacestd;

classComplex

{public:

Complex(){real=0;imag=0;}

Complex(doubler,doublei){real=r;imag=i;}

Complexoperator+(Complex&c2);

Complexoperator-(Complex&c2);

Complexoperator*(Complex&c2);

Comp

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 节日庆典

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1