C++面向对象实验报告

发布时间 : 星期一 文章C++面向对象实验报告更新完毕开始阅读

Student(string n,string s,int a,string no,string d) :Person(n,s,a) { num=no; depa=d;

}

void display()

{Person::display(); cout<<\学号:\

cout<<\系别:\

}

protected:

string num; string depa;

};

class Graduate:public Student

{public: Graduate(string n,string s,int a,string no,string d,string tn)

{

:Person(n,s,a),Student(n,s,a,no,d) tname=tn;

}

void display()

{Student::display(); cout<<\导师:\}

private:

string tname; };

int main()

{Teacher teac(\韩艳\女\副教授\面向对象程序设计\ Student stud(\李阳\男\计算机系\ Graduate grad(\王飞\男\计算机系\马平\ cout<<\教师的有关数据如下:\ }

teac.display();

cout<<\学生的有关数据如下:\stud.display();

cout<<\研究生的有关数据如下:\grad.display(); return 0;

实验5 多态性与虚函数

一、实验目的和要求

了解静态联编和动态联编的概念。掌握动态联编的条件。

二、实验内容和原理

事先编写好程序,上机调试和运行程序,分析结果。 (1)实验指导书P97 2 (2)实验指导书P100 5

三、实验环境

联想计算机,Windows XP操作系统,Visual C++ 6.0

四、算法描述及实验步骤

(1)编写源程序。

(2)检查程序有无错误(包括语法错误和逻辑错误),有则改之。 (3)编译和连接,仔细分析编译信息,如有错误应找出原因并改正之。 (4)运行程序,分析结果。

(5)将调试好的程序保存在自己的用户目录中,文件名自定。

五、调试过程

在第2题中的类‘Teacher’中没有定义‘lessons’导致错误。

六、实验结果

(1)

(2)

七、总结

(1) 了解了重载和覆盖的区别。

(2) 认识到动态联编和静态联编的区别以及动态联编的条件。

附录:

代码如下:

(1)//sy5_1.cpp #include #include using namespace std;

class Base {public:

void f(int x){cout<<\ };

class Derived:public Base {public:

virtual void g(void){cout<<\};

int main() {Derived d;

Base *pb=&d; pb->f(42); pb->f(3.14f) ; pb->g(); return 0; }

(2)//sy5_2.cpp #include using namespace std; #include class Teacher {public:

Teacher(string n,int h); virtual double salary()=0; virtual void print()=0; protected:

string name; int lessons; };

Teacher::Teacher(string n,int h) {name=n; lessons=h;

}

class Professor:public Teacher

{public:

Professor(string n,int h):Teacher(n,h){}

double salary(){return 5000+50*lessons;} void print();

void f(float x){cout<<\ virtual void g(void){cout<<\

};

void Professor::print()

{ cout<

class Associateprofessor:public Teacher

{public:

Associateprofessor(string n,int h):Teacher(n,h){} double salary(){return 3000+30*lessons;} };

void Associateprofessor::print()

{ cout<

class Lecturer:public Teacher { public:

Lecturer(string n,int h):Teacher(n,h){} double salary(){return 2000+20*lessons;} void print(); void print();

};

void Lecturer::print()

{ cout<

int main()

{

Teacher *t[3];

t[0]=new Professor(\

t[1]=new Associateprofessor(\ }

t[3]=new Lecturer(%ue\for(int i=0;i<3;i++) { t[i]->print(); delete t[i]; }

return 0;

实验6 运算符重载

一、实验目的和要求

熟悉运算符重载的定义和使用方法

二、实验内容和原理

事先编写好程序,上机调试和运行程序,分析结果。 (1)实验指导书P104 1

联系合同范文客服:xxxxx#qq.com(#替换为@)