C面向对象程序设计答案第三章谭浩强清华大学出版社.docx

上传人:wj 文档编号:1215486 上传时间:2023-04-30 格式:DOCX 页数:10 大小:10.89KB
下载 相关 举报
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第1页
第1页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第2页
第2页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第3页
第3页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第4页
第4页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第5页
第5页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第6页
第6页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第7页
第7页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第8页
第8页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第9页
第9页 / 共10页
C面向对象程序设计答案第三章谭浩强清华大学出版社.docx_第10页
第10页 / 共10页
亲,该文档总共10页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C面向对象程序设计答案第三章谭浩强清华大学出版社.docx

《C面向对象程序设计答案第三章谭浩强清华大学出版社.docx》由会员分享,可在线阅读,更多相关《C面向对象程序设计答案第三章谭浩强清华大学出版社.docx(10页珍藏版)》请在冰点文库上搜索。

C面向对象程序设计答案第三章谭浩强清华大学出版社.docx

2:

#include

usingnamespacestd;

classDate

{public:

Date(int,int,int);

Date(int,int);

Date(int);

Date();

voiddisplay();

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty):

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

{}

Date:

:

Date(intm,intd):

month(m),day(d)

{year=2005;}

Date:

:

Date(intm):

month(m)

{day=1;

year=2005;

}

Date:

:

Date()

{month=1;

day=1;

year=2005;

}

voidDate:

:

display()

{cout<

intmain()

{

Dated1(10,13,2005);

Dated2(12,30);

Dated3(10);

Dated4;

d1.display();

d2.display();

d3.display();

d4.display();

return0;

}

3:

#include

usingnamespacestd;

classDate

{public:

Date(int=1,int=1,int=2005);

voiddisplay();

private:

intmonth;

intday;

intyear;

};

Date:

:

Date(intm,intd,inty):

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

{}

voidDate:

:

display()

{cout<

intmain()

{

Dated1(10,13,2005);

Dated2(12,30);

Dated3(10);

Dated4;

d1.display();

d2.display();

d3.display();

d4.display();

return0;

}

4:

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

voiddisplay();

private:

intnum;

floatscore;

};

voidStudent:

:

display()

{cout<

intmain()

{Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

Student*p=stud;

for(inti=0;i<=2;p=p+2,i++)

p->display();

return0;

}

5:

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

intnum;

floatscore;

};

voidmain()

{Studentstud[5]={

Student(101,78.5),Student(102,85.5),Student(103,98.5),

Student(104,100.0),Student(105,95.5)};

voidmax(Student*);

Student*p=&stud[0];

max(p);

}

voidmax(Student*arr)

{floatmax_score=arr[0].score;

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,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return0;

}

7:

解法一

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

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

voiddisplay(){cout<

//可改为:

voiddisplay()const{cout<

private:

intnum;

floatscore;

};

intmain()

{constStudentstud(101,78.5);

stud.display();

//stud.change(101,80.5);

stud.display();

return0;

}

解法二:

#include

usingnamespacestd;

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

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

voiddisplay()const{cout<

private:

mutableintnum;

mutablefloatscore;

};

intmain()

{constStudentstud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

return0;

}

解法三:

#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,78.5);

Student*p=&stud;

p->display();

p->change(101,80.5);

p->display();

return0;

}

8:

#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,78.5);

voidfun(Student&);

fun(stud);

return0;

}

voidfun(Student&stu)

{stu.display();

stu.change(101,80.5);

stu.display();

}

9:

#include

usingnamespacestd;

classProduct

{public:

Product(intn,intq,floatp):

num(n),quantity(q),price(p){};

voidtotal();

staticfloataverage();

staticvoiddisplay();

private:

intnum;

intquantity;

floatprice;

staticfloatdiscount;

staticfloatsum;

staticintn;

};

voidProduct:

:

total()

{floatrate=1.0;

if(quantity>10)rate=0.98*rate;

sum=sum+quantity*price*rate*(1-discount);

n=n+quantity;

}

voidProduct:

:

display()

{cout<

cout<

}

floatProduct:

:

average()

{return(sum/n);}

floatProduct:

:

discount=0.05;

floatProduct:

:

sum=0;

intProduct:

:

n=0;

intmain()

{

ProductProd[3]={

Product(101,5,23.5),Product(102,12,24.56),Product(103,100,21.5)

};

for(inti=0;i<3;i++)

Prod[i].total();

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);

t1.display(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<

cout<

Comparecmp2(45.78,93.6);

cout<

cout<

Comparecmp3('a','A');

cout<

cout<

return0;

}

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

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2