C++上机实验报告类和对象Ⅱ.docx

上传人:b****2 文档编号:2590621 上传时间:2023-05-04 格式:DOCX 页数:12 大小:44.76KB
下载 相关 举报
C++上机实验报告类和对象Ⅱ.docx_第1页
第1页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第2页
第2页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第3页
第3页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第4页
第4页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第5页
第5页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第6页
第6页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第7页
第7页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第8页
第8页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第9页
第9页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第10页
第10页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第11页
第11页 / 共12页
C++上机实验报告类和对象Ⅱ.docx_第12页
第12页 / 共12页
亲,该文档总共12页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C++上机实验报告类和对象Ⅱ.docx

《C++上机实验报告类和对象Ⅱ.docx》由会员分享,可在线阅读,更多相关《C++上机实验报告类和对象Ⅱ.docx(12页珍藏版)》请在冰点文库上搜索。

C++上机实验报告类和对象Ⅱ.docx

C++上机实验报告类和对象Ⅱ

C++上机实验报告

 

实验名称:

类和对象

专业班级:

姓名:

学号:

实验日期:

1.实验目的

2.实验内容

3.程序代码

4.调试结果

5.实验心得

1.实验目的

(1)进一步加深对类和对象的理解;

(2)掌握类的构造函数和析构函数的概念和使用方法;

(3)掌握对象数组,对象的指针及其使用方法;

(4)掌握友元的概念和使用;

(5)了解类模板的使用方法。

2.实验内容

(1)有以下程序:

#include

classStudent

{public:

Student(intn,floats):

num(n),score(s){}

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

voiddisplay(){cout<

private:

intnum;

floatscore;

};

voidmain()

{Studentstud(101,78.5);

stud.display();

stud.change(101,80.5);

stud.display();

}

Ⅰ.阅读此程序,分析其执行过程,然后上机运行,对比输出结果;

Ⅱ.修改上面的程序,增加一个fun函数,改写main函数。

在main函数中调用fun函数,在fun函数中调用change和display函数。

在fun函数中使用对象的引用(Student&)作为形参。

(2)商店销售某一商品,商店每天公布统一的折扣(discount)。

同时允许销售人员在销售时灵活掌握售价(price),在此基础上,对一次购10件以上者还可以享受9.8折优惠。

现已知当天3个销货员销售情况为

销货员号(num)销货件数(quantity)销货单价(price)

101523.5

1021224.56

10310021.5

请编些程序,计算出当日此商品的总销售款sum以及每件商品的平均售价。

要求用静态数据成员和静态成员函数。

(3)有以下程序:

#include

usingnamespacestd;

classDate;//对Date类的提前引用声明

classTime//定义Time类

{public:

Time(int,int,int);

voiddisplay(Date&);//display是成员函数,形参是Date类对象的引用

private:

inthour;

intminute;

intsec;

};

classDate//声明Date类

{public:

Date(int,int,int);

friendvoidTime:

:

display(Date&);//声明Time中的display函数为友元成员函数

private:

intmonth;

intday;

intyear;

};

Time:

:

Time(inth,intm,ints)//类Time的构造函数

{hour=h;

minute=m;

sec=s;

}

voidTime:

:

display(Date&d)//display的作用是输出年,月,日和时,分,秒

{cout<

cout<

"<

"<

}

Date:

:

Date(intm,intd,inty)//类Date的构造函数

{month=m;

day=d;

year=y;

}

intmain()

{Timet1(10,13,56);//定义Time类对象t1

Dated1(12,25,2004);//定义Date类对象d1

t1.display(d1);//调用t1中的display函数,实参是Date类对象d1

return0;

}

将程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将display声明为友元函数。

在主函数中调用display函数,display函数分别引用Time和Date两个类的对象的私有数据输出年,月,日和时,分,秒。

修改后上机调试和运行。

(4)有以下使用类模板程序:

#include

usingnamespacestd;

template//声明类模板,虚拟类型名为numtype

classCompare//类模板名为Compare

{public:

Compare(numtypea,numtypeb)//定义构造函数

{x=a;y=b;}

numtypemax()//类型函数暂定为numtype

{return(x>y)?

x:

y;}

numtypemin()

{return(x

x:

y;}

private:

numtypex,y;

};

intmain()

{Comparecmp1(3,7);//定义对象cmp1,用于两个整数比较

cout<

cout<

Comparecmp2(45.78,93.6);//定义对象cmp2,用于两个浮点数的比较

cout<

cout<

Comparecmp3('a','A');//定义对象cmp3,用于两个字符的比较

cout<

cout<

return0;

}

Ⅰ.运行此程序;

Ⅱ.将它改写为在类模板外定义各成员函数。

3.程序代码

(1)

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

}

(2)

#include

usingnamespacestd;

classProduct

{public:

Product(intm,intq,floatp):

num(m),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;

}

 

(3)

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

}

(4)

#include

usingnamespacestd;

templste

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;

}

4.调试结果

(1)

(2)

(3)

(4)

5.实验心得

以前上机实验的时候,对某一个程序进行调试,显示出错,那么我就仅只是单纯将打出来的程序与课本上的源程序进行比较,然后凭借眼力找出错误。

虽然这样的方法可行,却也实在浪费了很多时间。

这一次,是我真正尝试着通过调试后界面给出的error和warn的提示找出错误,也第一次尝试去理解系统给出的语言。

虽然对其他人来说,可能不算什么,但是我觉得这算是我自己的一个提高。

然后,也通过上机编码程序,更加地了解了类模板函数,对类的构造函数和析构函数在形式和结构上也能很好的分辨。

至于友元函数,我还需要多分析一些程序,进而了解友元函数访问权限。

总的来说,我觉得这次试验真的是收获挺大的,也可能是几次试验下来觉得比较成功的一个了。

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

当前位置:首页 > 解决方案 > 学习计划

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

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