system("PAUSE");
returnEXIT_SUCCESS;
}
(1)写出程序运行的结果
(2)将Coordinate类中带有两个参数的构造函数进行修改,在函数体内增加下述语句:
Cout<<"ConstructorisCallled.\n";
写出程序的运行结果,并解释输出结果。
(3)添加下述语句:
Coordinatep4;Coordinatep5
(2);调试时会出现什么错误?
为什么?
如何对已有的构造函数进行适当的修改?
错误:
没有想匹配的构造函数。
增加一个Coordinate(intx1){};构造函数和一个Coordinate();的无参构造函数。
(4)调试
(2)和(3)的程序,结合运行结果分析:
创建不同的对象时会调用不同的构造函数。
#include
#include
usingnamespacestd;
classCoordinate{
public:
Coordinate(intx1,inty1){
x=x1;
y=y1;
cout<<"ConstructorisCallled.\n";
}
Coordinate(Coordinate&p);
Coordinate(intx1){
x=x1;
cout<<"anewConstructor\n";
}
Coordinate(){
cout<<"novarConstructor\n";
}
~Coordinate(){//析构函数前加~无参数无返回值不能重载一个类只能有一个析构函数它执行与构造函数相反的操作通常用于撤销对象时的一些清理任务
cout<<"Destructoriscalleded\n";}
intgetx(){
returnx;
}
intgety(){
returny;
}
private:
intx,y;
};
Coordinate:
:
Coordinate(Coordinate&p){
//在类外定义构造函数
x=p.x;
y=p.y;
cout<<"Copy_initiallizationConstructoriscalled\n";
}
intmain(intargc,char*argv[])
{
Coordinatep1(3,4);
Coordinatep2(p1);
Coordinatep3=p2;
Coordinatep4;
Coordinatep5
(2);
cout<<"p3=("<system("PAUSE");
returnEXIT_SUCCESS;
}
设计一个4*4魔方程序,让魔方的各行值得和等于各列值的和,并且等于两对角线值的和。
例如,
312525
9211915
17131123
727291
各行,各列以及两对角线值的和都是64
#include
#include
usingnamespacestd;
classmagic{
public:
voidgetdate(intx,inty){
first=x;
step=y;
sum=x+x+15*y;
}
voidsetfristmagic(){
inttemp=0;
for(inti=0;i<4;i++)
for(intj=0;j<4;j++){
temp=i*4+j;
m[i][j]=first+temp*step;
}
}
voidgenneratemagic(){
for(inti=0;i<4;i++)
for(intj=0;j<4;j++){
if(i==j){
m[i][j]=sum-m[i][j];
}
elseif(i+j==3){
m[i][j]=sum-m[i][j];
}
}
}
voidprintmagic(){
intk=0;
for(inti=0;i<4;i++)
for(intj=0;j<4;j++){
cout<k++;
if(k%4==0)cout<}
}
private:
intm[4][4];
intstep;
intfirst;
intsum;
};
intmain(intargc,char*argv[])
{
intx,y;
cout<<"输入魔方初始值";
cin>>x;
cout<<"输入相邻元素之间的差值";
cin>>y;
magicmgc;
mgc.getdate(x,y);
mgc.setfristmagic();
mgc.genneratemagic();
cout<<"输出魔方"<mgc.printmagic();
system("PAUSE");
returnEXIT_SUCCESS;
}
3使用C++的string类,将5个字符串按逆转后的顺序显示出来。
例如,逆转前的字符串是:
GermanyJapanAmericaBritainFrance
逆转后的顺序输出字符串是:
FranceBritainAmericaJapanGermany
程序:
#include
#include
usingnamespacestd;
intmain(intargc,char*argv[])
{
stringstr[5];
cout<<"请分别输入5个字符串:
"<cout<<"1:
";
cin>>str[0];
cout<";
cin>>str[1];
cout<";
cin>>str[2];
cout<";
cin>>str[3];
cout<";
cin>>str[4];
cout<"<cout<cout<<"逆转后的顺序为:
"<cout<system("PAUSE");
returnEXIT_SUCCESS;
}
第5章
1编写一个学生和教师的数据输入和显示程序。
学生数据有编号、姓名、性别、年龄、系别、和成绩,教师数据有编号、姓名、性别、年龄、职称和部门。
要求将编号、姓名、性别、年龄的输入和显示设计成一个类person,并作为学生类student和教师类teacher的基类。
#include
#include
usingnamespacestd;
classperson
{
public:
intnum;
stringname;
stringsex;
intage;
voidget()
{
cout<<"请输入编号:
";
cin>>num;
cout<<"请输入姓名:
";
cin>>name;
cout<<"请输入年龄:
";
cin>>age;
cout<<"请输入性别:
";
cin>>sex;
}
voidshow()
{
cout<<"编号"<<"\t"<<"姓名"<<"\t"<<"性别"<<"\t"<<"年龄"<<"\t"<cout<}
};
classstudent:
publicperson
{
public:
stringdept;
doublescore;
voidgets()
{
cout<<"请输入系别:
";
cin>>dept;
cout<<"请输入成绩:
";
cin>>score;
}
voidshows()
{
cout<<"系别"<<"\t"<<"成绩"<<"\t"<cout<}
};
classteacher:
publicperson
{
public:
stringzhi;
stringbu;
voidgett()
{
cout<<"请输入职称:
";
cin>>zhi;
cout<<"请输入部门:
";
cin>>bu;
}
voidshowt()
{
cout<<"职称"<<"\t"<<"部门"<<"\t"<cout<}
};
voidmain()
{
classstudents1;
cout<<"请输入学生信息:
"<s1.get();
s1.gets();
s1.show();
s1.shows();
classteachert1;
cout<<"请输入教师信息:
"<t1.get();
t1.gett();
t1.show();
t1.showt();
}
2按要求阅读、编辑、编译、调试和运行一下程序。
(1)编辑、编译、调试和运行程序,并写出程序的运行结果。
#include
#include
usingnamespacestd;
classMyArray
{
protected:
int*alist;
intlength;
public:
MyArray(intleng)
{
if(leng<=0)
{
cout<<"errorlength";
exit
(1);
}
alist=newint[leng];
length=leng;
if(alist==NULL)
{
cout<<"assignfailure";
exit
(1);
}
cout<<"MyArray类对象已创建."<}
~MyArray()
{
delete[]alist;
cout<<"MyArray类对象被撤销."<}
voidInput()
{
cout<<"请从键盘输入"<";
inti;
int*p=alist;
for(i=0;i>*p;
}
voidDisplay(stringstr)
{
inti;
int*p=alist;
cout<";
for(i=0;icout<}
};
intmain()
{
MyArraya(5);
a.Input();
a.Display("显示已输入的");
return0;
(2)声明一个类SortArray继承类MyArray,在该类中定义一个函数,具有将输入的证书从大到小进行排序的功能。
#include
#include
usingnamespacestd;
classMyArray
{
protected:
int*alist;
intlength;
public:
MyArray(intleng)
{
if(leng<=0)
{
cout<<"errorlength";
exit
(1);
}
alist=newint[leng];
length=leng;
if(alist==NULL)
{
cout<<"assignfailure";
exit
(1);
}
cout<<"MyArray类对象已创建."<}
~MyArray()
{
delete[]alist;
cout<<"MyArray类对象被撤销."<}
voidInput()
{
cout<<"请从键盘输入"<";
inti;
int*p=alist;
for(i=0;i>*p;
}
voidDisplay(stringstr)
{
inti;
int*p=alist;
cout<";
for(i=0;icout<}
};
classSortArray:
publicMyArray
{
public:
SortArray(intleng):
MyArray(leng)
{
cout<<"SortArray类对象已创建."<}
voidSort()
{
inti,a;
int*p=alist;
for(i=1;i<=length;i++,p++)
{
if(*p>*(p+1)){a=*(p+1);*(p+1)=*p;*p=a;}
}
}
~SortArray()
{
cout<<"SortArray类对象被撤销."<}
};
intmain()
{
SortArrays(5);
s.Input();
s.Display("排序之前");
s.Sort();
s.Display("排序之后");
return0;
}
第6章
实验内容和步骤
1分析并调试下列程序,写出程序的输出结果,并分析输出结果。
#include
usingnamespacestd;
classStock
{
public:
voidprint()
{
cout<<"Stockclass.\n";
}
};
classDer1_Stock:
publicStock
{
public:
voidprint()
{
cout<<"Der1_Stockclass.\n";
}
};
classDer2_Stock:
publicStock
{
public:
voidprint()
{
cout<<"Der2_Stockclass.\n";
}
};
intmain()
{
Stocks1;
Stock*ptr;
Der1_Stockd1;
Der2_Stockd2;
ptr=&s1;
ptr->print();
ptr=&d1;
ptr->print();
ptr=&d2;
ptr