《面向对象程序的设计》答案Word下载.docx
- 文档编号:18704186
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:28
- 大小:33.15KB
《面向对象程序的设计》答案Word下载.docx
《《面向对象程序的设计》答案Word下载.docx》由会员分享,可在线阅读,更多相关《《面向对象程序的设计》答案Word下载.docx(28页珍藏版)》请在冰豆网上搜索。
i=88;
//i=11,r=11
i=88,r=88
程序四:
intf(inti)
{staticintk=1;
for(;
i>
0;
i--)
k+=i;
returnk;
}
voidmain()
{inti;
for(i=0;
5;
i++)
f(i)<
"
//1251121
程序五:
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<
//a:
0b:
-9n:
1
4b:
10n:
13
-5n:
6b:
35
实验二C++对C的扩充
1、了解在面向对象程序设计过程中C++对C功能的扩充与增强,并善于在编写程序的过程中应用这些新功能。
2、进一步熟悉编辑、编译、连接和运行C++程序的方法。
3、进一步熟悉C++程序的结构和编程方法。
intamount=123;
{intamount=456;
:
amount<
'
:
amount=789;
//123,456,789,456
2、编写一个程序,用来求2个或3个正整数中的最大数。
①用不带默认参数的函数实现。
include<
iostream>
usingnamespacestd;
intmax(inta,intb,intc)//求3个整数中的最大者
{if(b>
a)a=b;
if(c>
a)a=c;
returna;
intmax(inta,intb)//求两个整数中的最大者
{if(a>
b)returna;
elsereturnb;
intmain()
{inta=7,b=-4,c=9;
max(a,b,c)<
//输出3个整数中的最大者
max(a,b)<
//输出两个整数中的最大者
return0;
②用带默认参数的函数实现。
intmain()
{intmax(inta,intb,intc=0);
inta,b,c;
cin>
>
a>
b>
c;
max(a,b,c)="
max(a,b)="
intmax(inta,intb,intc)
{if(b>
if(c>
3、有5个字符串,要求对它们按由小到大顺序排列,用string方法。
string>
{inti;
stringstr[5]={"
BASIC"
C"
FORTRAN"
C++"
PASCAL"
};
voidsort(string[]);
sort(str);
thesortedstrings:
for(i=0;
i++)
str[i]<
voidsort(strings[])
{inti,j;
stringt;
for(j=0;
j<
j++)
5-j;
if(s[i]>
s[i+1])
{t=s[i];
s[i]=s[i+1];
s[i+1]=t;
}
4、定义一个求两个数中较小值的函数模板min(),要求在main()函数中进行调用求两个
浮点型数据和两个整型数据中较小的数。
#include"
iostream"
string"
template<
typenameT>
Tmin(Ta,Tb)
{
returna<
b?
b;
inta=1,b=9;
floatc=1.23471,d=32.431564;
Theminof"
and"
is"
min(a,b)<
endl
c<
d<
min(c,d)<
实验三类和对象
(一)
1、掌握声明类的方法,类和类的成员的概念以及定义对象的方法。
2、掌握类的构造函数与析构函数的概念和使用方法。
3、初步掌握用类和对象编制基于对象的程序。
1、分析下面的程序,写出其运行时的输出结果。
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<
{Dated1(10,13,2005);
Dated2(12,30);
Dated3(10);
Dated4;
d1.display();
d2.display();
d3.display();
d4.display();
return0;
//10/13/2005
12/30/2005
10/1/2005
1/1/2005
2、建立一个名为Student的类,该类有以下几个私有成员变量:
学生、学号、性别、年龄。
还有以下两个成员变量:
一个用于初始化学生、学号、性别和年龄的构造函数,一个用于输出学生信息的函数。
编写一个主函数,声明一个学生对象,然后调用成员函数在屏幕输出学生信息。
classstudent
public:
student();
voiddisplay();
private:
stringsName,sNum;
charchSex;
intiAge;
student:
student(stringna,stringnum,chars,inta):
sName(na),sNum(num),
chSex(s),iAge(a){}
voidstudent:
display()
cout<
----------THEINFORMATIONOFSTUDENT----------\n"
name:
sName<
endl<
number:
sNum<
sex:
chSex<
age:
iAge<
students("
WangFang"
0811045263"
w'
20);
s.display();
3、类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(容自定)。
classPerson
{private:
charname[10];
intage;
intsalary;
chartel[8];
public:
Person(char*xname,intxage,intxsalary,char*xtel);
voiddisp();
解:
string.h>
Person:
person(char*Xname,intXage,intXsalary,char*Xtel)
{strcpy(name,xname);
age=xage;
salary=xsalary;
strcpy(tel,xtel);
voidPerson:
disp()
{cout<
“:
”<
name<
“年龄”:
age<
“工资”:
salary<
endl:
“”:
tel<
{Personobj(“三”,25,850,“45678912”);
obj.disp()
实验四类和对象
(二)
1、进一步加深对类和对象的理解。
2、掌握对类的对象数组、对象的指针及其使用方法。
3、掌握友元的概念和使用。
4、了解类模板的使用方法。
1、分析并比较下列程序运行的结果。
#include<
classsmallone
smallone(intsma)
smconstr:
sma<
\n"
voidfn(intn)
{smallonesm(n);
infunctionfnwithn="
intmain()
{fn(10);
fn(20);
//smconstr:
10
infunctionfnwithn=10
20
infunctionfnwithn=20
{staticsmallonesm(n);
10
2、建立一个对象数组,放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
intnum;
floatscore;
};
{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);
reyurn0;
voidmax(Student*arr)
{floatmax_score=arr[0].score;
intk=0;
for(inti=1;
if(arr[i].score>
max_score){max_score=arr[i].score;
k=i;
arr[k].num<
max_score<
3、声明一个类模板,利用它分别实现两个整数、浮点数和字符的比较,求出大数和小数。
classnumtype>
classCompare
Compare(numtypea,numtypeb)
{x=a;
y=b;
numtypemax()
{return(x>
y)?
x:
y;
numtypemin()
{return(x<
numtypex,y;
{Compare<
int>
cmp1(3,7);
cmp1.max()<
istheMaximumoftwointedernumbers."
cmp1.min()<
istheMinimumoftwointedernumbers."
endl<
Compare<
float>
cmp2(45.78,93.6);
cmp2.max()<
istheMaximumoftwofloatnumbers."
cmp2.min()<
istheMinimumoftwofloatnumbers."
char>
cmp3('
a'
A'
);
cmp3.max()<
istheMaximumoftwocharacters."
cmp3.min()<
istheMinimumoftwocharacters."
实验五运算符重载
1、进一步了解运算符重载的概念和使用方法。
2、掌握几种常用的运算符重载的方法。
1、定义一个复数类Complex,重载运算法“+”,使之能用于复数的加法运算。
将运算符重载为普通函数(非成员、非友元)、成员函数、友元函数。
根据要求修改通过函数来实现复数相加的示例,分别编写程序,求两个复数之和。
classComplex//定义Complex类
Complex(floatx=0,floaty=0){real=x;
imag=y;
}//构造函数
Complexcomplex_add(Complex&
c2);
//声明复数相加函数
voiddisplay(){cout<
real<
+'
imag<
i'
floatreal;
//实部
floatimag;
//虚部
ComplexComplex:
complex_add(Complex&
c2)
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
returnc;
{Complexcomplex1(3.34f,4.8f),complex2(12.8f,5.2f);
Complexcomplex;
//定义3个复数对象
complex=plex_add(complex2);
//进行两个复数的加运算
complex.display();
//16.14+10i
//普通函数(非成员、非友元)
classComplex
Complex(){real=0;
imag=0;
Complex(doubler,doublei){real=r;
imag=i;
doubleget_real();
doubleget_imag();
doublereal;
doubleimag;
doubleComplex:
get_real()
{returnreal;
get_imag()
{returnimag;
voidComplex:
("
i)"
Complexoperator+(Complex&
c1,Complex&
returnComplex(c1.get_real()+c2.get_real(),c1.get_imag()+c2.get_imag());
{Complexc1(3,4),c2(5,-10),c3;
c3=c1+c2;
c3="
c3.display();
//运算符重载为成员函数
Complexoperator+(Complex&
operator+(Complex&
{Complexc;
c.real=real+c2.real;
c.imag=imag+c2.imag;
c1="
c1.display();
c2="
c2.display();
c1+c2="
c3.display();
//将运算符重载为友元函数
Complex(doubler){real=r;
friendComplexoperator+(Complex&
Complexoperator+(Complex&
{returnComplex(c1.real+c2.real,c1.imag+c2.i
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向对象程序的设计 面向 对象 程序 设计 答案