C++daima.docx
《C++daima.docx》由会员分享,可在线阅读,更多相关《C++daima.docx(21页珍藏版)》请在冰豆网上搜索。
C++daima
C++面向对象程序设计
实验报告
学生姓名尚云飞
学号0640842098
院(系)、专业信息工程系、电子信息工程
指导教师赵乘麟
2007年12月26—2007年1月3
实验一类和对象
一、实验目的
1、了解和并掌握类和对象的概念2、理解类和对象的关系3、学会定义类和类的对象
二、实验环境
硬件:
联想488E商用机软件:
操作系统windowsxp编译环境VC++6.0
三、实验程序
1.检查下面的程序,找出其中的错误,并改正。
然后上机调试,使之能正常运行。
运行时从键盘输入时、分、秒的值,检查输出是否正确。
调试后的程序:
#include
usingnamespacestd;
classTime
{public:
voidset_time();
voidshow_time();
inthour;
intminute;
intsec;
};
intmain()
{
Timet;
t.set_time();
t.show_time();
return0;}
voidTime:
:
set_time()
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime:
:
show_time()
{cout<"<"< }
2.改写上程序,要求:
(1)将数据成员改为私有的;
(2)将输入和输出功能改为由成员函数实现;
(3)在类体内定义成员函数。
程序:
#include
usingnamespacestd;
classTime
{public:
voidset_time();
voidshow_time();
private:
inthour;
intminute;
intsec;
};
intmain()
{
Timet;
t.set_time();
t.show_time();
return0;
}
voidTime:
:
set_time(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime:
:
show_time(void)
{cout<"<"< }
3程序:
#include
usingnamespacestd;
classTime
{public:
voidset_time();
voidshow_time();
private:
inthour;
intminute;
intsec;
};
intmain()
{
Timet;
t.set_time();
t.show_time();
return0;
}
voidTime:
:
set_time(void)
{cin>>hour;
cin>>minute;
cin>>sec;
}
voidTime:
:
show_time(void)
{if(sec>=60){minute+=sec/60;sec=sec%60;}
if(minute>=60){hour+=minute/60;minute=minute%60;}
if(hour>=24)hour=hour%24;
cout<"<"<}
四、实验结果
程序1、2、3结果分别如下:
1
五、实验结论:
对于类和对象,类是对象的抽象,而对象是类的具体实现。
类是抽象的,不占用内存,而对象是具体的,占用存储空间。
类是C++语言的基础,因此深刻的理解和掌握类的定义方法是学好这门语言的关键。
同时经常上机操作是非常有必要的,一方面可以熟悉VC++6.0,另一方面加深对类概念的理解。
实验二关于类和对象的进一部讨论
一、实验目的
1更深入地理解类和对象
2掌握构造函数和析构函数定义方法和作用
3理解和掌握对象指针、this指针、常对象成员、静态成员
4明白友元函数,并且能真确地运用。
二、实验环境
硬件:
联想488E商用机软件:
操作系统windowsxp编译环境VC++6.0
三、实验程序
a)分析下面的程序,写出其运行时的输出的结果。
程序:
#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;
}
2如果将上题中程序的第5行改为用默认参数,即
Date(int=1,int=I,int=2005);分析程序有问题。
上机编译,分析出错信息,修改程序使之能通过编译。
要求保留上面一行给出的构造函数,同时能输出与第2题的程序相同的输出结果。
调试后的程序:
#include
usingnamespacestd;
classDate
{public:
Date(int=1,int=1,int=2005);
voiddisplay();
private:
intmonth;
intday;
int year ;
}; };
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;
}
3建立一个对象数组,内放5个学生的数据(学号、成绩),用指针向数组首元素,输出第1,3.,5个学生的数据。
#include
usingnamespacestd;
classStudent
{public:
Student(intn,floats):
num(n),score(s){}
voiddisplay();
private:
intnum;
floatscore;
};
voidStudent:
:
display()
{cout<<"thenumeberis"<cout<<"thescoreis"<}
intmain()
{Studentstud[5]={
Student(1001,80),
Student(1002,90),
Student(1003,100),
Student(1004,50),
Student(1005,70)};
Student*p;
p=&stud[0];
p->display();
(p+2)->display();
(p+4)->display();
return0;
}
4、建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生成绩最高者,并输出学号!
程序:
#include
usingnamespacestd;
classStudent
{public:
Student(intn,floats):
number(n),score(s){}
intnumber;
floatscore;
};
voidmax(Student*y)
{floatt;
intk;
for(inti=0;i<4;i++)
for(intj=0;j<4-i;j++)
if((y+i)->score>(y+i+1)->score)
{
t=(y+i)->score;
(y+i)->score=(y+i+1)->score;
(y+i+1)->score=t;
k=(y+i)->number;;
(y+i)->number=(y+i+1)->number;
(y+i+1)->number=k;
}
cout<<"成绩最高者"<<(y+4)->score<<"学号"<<(y+4)->number<}
intmain()
{
Studentstud[5]={
Student(1001,75),
Student(1002,88.5),
Student(1003,95.5),
Student(1004,82),
Student(1005,95)};
max(stud);
return0;
}
四、实验结果
程序1、2、3、4结果分别如下:
五、实验结论:
对于构造函数的使用,定义了带默认参数的构造函数,就不能再定义构造函数的重载了。
同时还需要注意构造函数和析构函数的调用顺序,先构造的后析构,后构造的先析构,当然应该注意特殊情况。
对于数组和指针的运用也是关键。
实验三运算符重载
一、实验目的
1.理解并掌握运算符重载的定义。
2、理解运算符的实质是函数的重载并能利用运算符的重载来解决问题。