C++《面向对象程序设计》实验指导书Word下载.docx
- 文档编号:15395549
- 上传时间:2022-10-30
- 格式:DOCX
- 页数:13
- 大小:24.16KB
C++《面向对象程序设计》实验指导书Word下载.docx
《C++《面向对象程序设计》实验指导书Word下载.docx》由会员分享,可在线阅读,更多相关《C++《面向对象程序设计》实验指导书Word下载.docx(13页珍藏版)》请在冰豆网上搜索。
{int*p,i;
i=5;
p=&
i;
i=*p+10;
cout<
i="
i<
程序三:
voidmain(void)
{inti=10;
int&
r=i;
r++;
r="
r<
'
\n'
i=88;
程序四:
intf(inti)
{staticintk=1;
for(;
i>
0;
i--)
k+=i;
returnk;
}
voidmain()
{inti;
for(i=0;
5;
i++)
f(i)<
"
程序五:
voidfunc();
intn=1;
{staticinta;
intb=-9;
cout<
a:
a<
b:
b<
n:
<
n<
b+=4;
func();
n+=10;
voidfunc()
{
staticinta=2;
intb=5;
a+=2;
n+=12;
b+=5;
cout<
n<
实验二C++对C的扩充
1、了解在面向对象程序设计过程中C++对C功能的扩充与增强,并善于在编写程序的过程中应用这些新功能。
2、进一步熟悉编辑、编译、连接和运行C++程序的方法。
3、进一步熟悉C++程序的结构和编程方法。
intamount=123;
{intamount=456;
:
amount<
'
:
amount=789;
2、编写一个程序,用来求2个或3个正整数中的最大数。
①用不带默认参数的函数实现。
②用带默认参数的函数实现。
3、有5个字符串,要求对它们按由小到大顺序排列,用string方法。
4、定义一个求两个数中较小值的函数模板min(),要求在main()函数中进行调用求两个
浮点型数据和两个整型数据中较小的数。
实验三类和对象
(一)
1、掌握声明类的方法,类和类的成员的概念以及定义对象的方法。
2、掌握类的构造函数与析构函数的概念和使用方法。
3、初步掌握用类和对象编制基于对象的程序。
1、分析下面的程序,写出其运行时的输出结果。
iostream>
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;
voidDate∷display()
{cout<
month<
″/″<
day<
year<
intmain()
{Dated1(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
d1.display();
d2.display();
d3.display();
d4.display();
return0;
2、建立一个名为Student的类,该类有以下几个私有成员变量:
学生姓名、学号、性别、年龄。
还有以下两个成员变量:
一个用于初始化学生姓名、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。
编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。
3、类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定)。
classPerson
{private:
charname[10];
intage;
intsalary;
chartel[8];
public:
Person(char*xname,intxage,intxsalary,char*xtel);
voiddisp();
实验四类和对象
(二)
1、进一步加深对类和对象的理解。
2、掌握对类的对象数组、对象的指针及其使用方法。
3、掌握友元的概念和使用。
4、了解类模板的使用方法。
1、分析并比较下列程序运行的结果。
#include<
classsmallone
smallone(intsma)
{cout<
smconstr:
sma<
\n"
voidfn(intn)
{smallonesm(n);
infunctionfnwithn="
intmain()
{fn(10);
fn(20);
{staticsmallonesm(n);
}
2、建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
3、声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
(例3.14)
实验五运算符重载
1、进一步了解运算符重载的概念和使用方法。
2、掌握几种常用的运算符重载的方法。
1、定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。
将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。
根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和。
classComplex//定义Complex类
Complex(floatx=0,floaty=0){real=x;
imag=y;
}//构造函数
Complexcomplex_add(Complex&
c2);
//声明复数相加函数
voiddisplay(){cout<
real<
+'
imag<
i'
};
private:
floatreal;
//实部
floatimag;
//虚部
ComplexComplex:
complex_add(Complex&
c2)
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;
intmain()
{Complexcomplex1(3.34f,4.8f),complex2(12.8f,5.2f);
Complexcomplex;
//定义3个复数对象
complex=plex_add(complex2);
//进行两个复数的加运算
complex.display();
实验六继承和派生
1、了解继承在面向对象程序设计中的重要作用。
2、进一步理解继承与派生的概念。
3、掌握通过继承派生出一个新的类的方法。
4、了解虚基类的作用和用法。
1、运行程序,分析构造函数与析构函数的调用顺序。
classA
{public:
A(){cout<
A:
Constructor"
~A(){cout<
Destructor"
classB:
publicA
{public:
B(){cout<
B:
~B(){cout<
{Bb;
classA
{inta;
public:
A(intaa=0){a=aa;
~A(){cout<
”DestructorA!
”<
publicA
{intb;
B(intaa=0,intbb=0):
A(aa){b=bb;
~B(){cout<
”Destruct
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序设计 面向 对象 程序设计 实验 指导书