C++面向对象 实验报告

实验一类与对象

一、             实验目的

1、 掌握类的声明和使用。

2、 掌握类的声明和对象的声明。

3、 复习具有不同访问属性的成员的访问方式。

4、 观察构造函数和析构函数的执行过程。

5、 学习类的组合使用方法。

6、 使用VC++的debug调试功能观察程序流程,跟踪观察类的构造函数、析构函数、成员函数的执行顺序。

二、             实验任务

1、 声明一个CPU类,包含等级(rank)、频率(frequency)、电压(voltage)等属性,有两个公有成员函数run、stop。其中,rank为枚举类型CPU_Rank,声明为enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7},frequency为单位是MHz的整型数,voltage为浮点型的电压值。观察构造函数和析构函数的调用顺序。

2、 声明一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,声明并实现这个类。

3、 (选作)设计一个用于人事管理的People(人员)类。考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”声明为一个“日期”类内嵌子对象。用成员函数实现对人员信息的录入和显示。要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、组合。

1、#include <iostream>

    using namespace std;

       enum CPU_Rank {p1=1,p2,p3,p4,p5,p6,p7};

       class CPU

       {      private:

              CPU_Rank  rank;

              int freauency;

              float voltage;

           public:

              CPU(CPU_Rank r, int f,float v)

                 { rank=r;

                freauency=f;

                voltage=v;

                cout<<" This is CPU construct program"<<endl;}

               ~CPU()

                     {cout<<" This is CPU destroy function"<<endl;}

              void run(){cout<< " The CPU is running"<<endl;}

              void stop(){cout<< " The CPU is Stopping"<<endl;}

       };

       int main()

       {

       enum CPU_Rank  rank;

       rank=p5;

       CPU cpu1(rank,1024,3.6);

       cpu1.run();

       cpu1.stop();

       return 0;

       }

运行结果:This is CPU construct program

          The CPU is running

          The CPU is Stopping

          This is CPU destroy function

2、#include <iostream>

    using namespace std;

       enum CPU_Rank {p1=1,p2,p3,p4,p5,p6,p7};

       class CPU

       {private:

           CPU_Rank rank;

              int freauency;

           float voltage;

       public:

           CPU(CPU_Rank r=p1,int f=0,float v=0)

              { rank=r;

             freauency=f;

             voltage=v;

             cout<<" This is CPU construct program"<<endl;}

           ~CPU()

              {cout<<" This is CPU destroy function"<<endl;}

           void run(){cout<<" The CPU is running"<<endl;}

        void stop(){cout<<" The CPU is Stopping"<<endl;}

       };

       class RAM

       {private:

              int  rank;

              int  size;

       public:

            RAM( int r=0, int s=0)

               { rank=r;

              size=s;}

            void run(){cout<<" The RAM is running"<<endl;}

            void stop(){cout<<" The RAM is Stopping"<<endl;}

       };

       class CDROM

       {      private:

              int  rank;

              int  size;

       public:

             CDROM ( int r=0, int s=0)

                { rank=r;

               size=s;}

          void run(){cout<<" The CDROM is running"<<endl;}

             void stop(){cout<<" The CDROM is Stopping"<<endl;}

       };

       class Computer

       {

       private:

        CPU cpu;

         RAM ram;

         CDROM  cdrom;

    public:

         Computer(CPU c,RAM r,CDROM cd)

         { cout<<" This is the Computer Construct"<<endl;

         cpu=c;

          ram=r;

         cdrom=cd;}

         void run()

         { cout<<" This is Computer is running"<<endl;

           cpu.run();

           ram.run();}

         void stop ()

         { cout<<" This is Computer is stop"<<endl;

           cpu.stop();

           ram.stop();}

       };

      

       int  main()

       { enum CPU_Rank  rank;

         rank=p5;

         CPU  cpu1(rank,1024,3.6f);

         RAM  ram(5,1024);

         CDROM cdrom(5,1024);

         Computer cp(cpu1,ram,cdrom);

         cp.run();

         cp.stop();

         return 0;

       }

实验二 C++程序结构

1、  实验目的

1.   观察程序运行中的变量的作用域、生存期和可见性。

2.   学习类的静态成员的使用。

3.   学习多文件结构在C++程序中的使用。

2、  实验任务

1.   运行下面程序,2.  观察变量x、y的值。

//lab2_1.cpp

3.   实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,4.       保存其服5.    务器名6. 称:整型静态数据成员ClientNum,7.    记录已定义的客户数量;定义静态函数ChangeServerName()改变服8.    务器名9. 称。在头文件client.h中定义类,10.       在文件client.cpp中实现,11.       在文件test.cpp中测试这个类,12.      观察相应的成员变量取值的变化情况。

实验二 C++程序结构

1、 实验目的

1.   观察程序运行中的变量的作用域、生存期和可见性。

2.   学习类的静态成员的使用。

3.   学习多文件结构在C++程序中的使用。

2、 实验任务

1.   运行下面程序,2.       观察变量x、y的值。

3.   实现客户机(CLIENT)类。定义字符型静态数据成员ServerName,4.      保存其服务器名称:整型静态数据成员ClientNum,记录已定义的客户数量;定义静态函数ChangeServerName()改变服务器名称。在头文件client.h中定义类,在文件client.cpp中实现,在文件test.cpp中测试这个类,观察相应的成员变量取值的变化情况。

三、实验结果

    1.

2.  服务器名称为:N

   客户机数量为:2

   服务器名称为:A

实验源代码:

1.#include <iostream>

using namespace std;

void fn1();

int x = 1, y = 2;

void main()

{

       cout << "Begin..." << endl;

    cout << "x = " << x << endl;

    cout << "y = " << y << endl;

       cout << "Evaluate x and y in main()..." << endl;

       int x = 10, y = 20;

    cout << "x = " << x << endl;

    cout << "y = " << y << endl;

       cout << "Step into fn1()..." << endl;

    fn1();

    cout << "Back in main" << endl;

    cout << "x = " << x << endl;

    cout << "y = " << y << endl;

    return 0;

}

void fn1()

{

    int y = 200;

    cout << "x = " << x << endl;

    cout << "y = " << y << endl;

}

2. //client.h

#include <iostream>

#include <string>

using namespace std;

class CLIENT

{

private:

    static char ServerName;

    static int ClientNum;

public:

     CLIENT();

    ~CLIENT();

        static int GetClientNum();

     static void ChangServerName(char name);

        static char GetServerName();

};

//client.cpp

#include "client.h"

CLIENT::CLIENT()

{

       ClientNum++ ;

}

CLIENT::~CLIENT()

{

       ClientNum-- ;

}

int CLIENT::GetClientNum()

{

       return ClientNum;

}

void CLIENT::ChangServerName(char name)

{

       ServerName=name;

}

char CLIENT::GetServerName()

{

       return ServerName;

}

//test.cpp

#include <iostream.h>

#include "client.h"

int CLIENT::ClientNum = 0;

char CLIENT::ServerName= 'N';

int main()

{

       CLIENT a;

       CLIENT b;

       cout << "服务器名称为:" << CLIENT::GetServerName() << endl;

       CLIENT::ChangServerName('A');

       cout << "客户机数量为:" << CLIENT::GetClientNum() << endl;

       cout << "服务器名称为:" << CLIENT::GetServerName() << endl;

}

实验三 数组与指针

一、实验目的

1、学习使用数组。

2、学习字符串数据的组织和处理。

3、掌握指针的使用方法。

4、练习通过debug观察指针的内容及其所指的对象的内容。

5、练习通过动态内存分配实现动态数组,并体会指针在其中的作用。

二、实验任务

1、测试3X3矩阵转置函数的程序,程序如下:

#include <iostream>

using namespace std;

void move (int matrix[3][3])

{

       int i, j, k;

       for(i=0; i<3; i++)

              for (j=0; j<i; j++)

              {

                     k = matrix[i][j];

                     matrix[i][j] = matrix[j][i];

                     matrix[j][i] = k;

              }

}

int main()

{

       int i, j;

       int data[3][3];

       cout << "输入矩阵的元素" << endl;

       for(i=0; i<3; i++)

              for (j=0; j<3; j++)

              {

                     cout << "第" << i+1 << "行第" << j+1

                            <<"个元素为:";

                     cin >> data[i][j];

              }

              cout << "输入的矩阵的为:" << endl;

              for(i=0; i<3; i++)

              {

                     for (j=0; j<3; j++)

                            cout << data[i][j] << " ";

                     cout << endl;

              }

              move(data);

              cout << "转置后的矩阵的为:" << endl;

              for(i=0; i<3; i++)

              {

                     for (j=0; j<3; j++)

                            cout << data[i][j] << " ";

                     cout << endl;

              }

}    

2、使用动态内存分配生成动态数组来重新设计一个3X3矩阵转置函数,使用指针实现函数的功能。

3、编程实现两字符串的连接。要求使用字符数组保存字符串,不要使用系统函数。

4、使用String类定义字符串对象,重新实现上一小题。

(选作)5、定义一个Employee类,其中包括姓名、街道地址、城市和邮编等属性,以及change_name( )和display()等函数。display()显示姓名、街道地址、城市和邮编等属性,change_name()改变对象的姓名属性。实现并测试这个类。(提示:对字符数组的赋值可以使用字符串拷贝函数strcpy(char *,char*,name))。

(选作)6、定义包含个5元素的对象数组,每个元素都是Employee类型的对象。

实验四 继承与派生

一、实验目的

1、学习定义和使用类的继承关系,定义派生类。

2、熟悉不同继承方式下对基类成员的访问控制。

3、学习利用虚基类解决二义性问题。

二、实验任务

1、定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。

2、定义一个基类BaseClass,有整型成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。

3、定义一个车(vehicle )基类,具有MaxSpeed、Weight等成员变量,Run,Stop等成员函数,由此派生出自行车(bicycle )类、汽车(motorcar)类,自行车类具有高度(Height)等属性,汽车类有座位数(SeatNum)等属性。从bicycle和motorcar类派生出摩托车(motorcycle )类,在继承过程中,注意把vehicle设置为虚基类。如果不把设置为虚基类,会有什么问题?编程试试看。

实验结果:

1、error C2248: 'age' : cannot access private member declared in class 'Animal'

  改正方案:将Animal类中私有成员age换成公共成员。

2、构造基类对象!

构造派生类对象!

析构派生类对象!

析构基类对象!

3、Now it is running!

   Now it has stopped!

   //如果vehicle不是虚基类,有错误:

     error C2385: 'motorcycle::Run' is ambiguous

     ……

源程序:

 1、#include <iostream>

using namespace std;

class Animal

{

private:

   int age;

public:

    Animal(){};

   ~Animal(){};

};

class Dog : private Animal

{

public:

   Dog(){};

   ~Dog(){};

   void SetAge(int n){ age = n;}

};

int main()

{

   Dog a;

   a.SetAge(10);

   return 0;

}

2、#include <iostream>

using namespace std;

class BaseClass

{

public:

    BaseClass() { cout << "构造基类对象!" << endl;}

   ~BaseClass() { cout << "析构基类对象!" << endl;}

};

class DerivedClass : public BaseClass

{

public:

    DerivedClass() {cout << "构造派生类对象!" << endl;}

   ~DerivedClass() {cout << "析构派生类对象!" << endl;}

};

int main()

{

    DerivedClass d;

}

3、nclude <iostream>

using namespace std;

class vehicle

{

private:

   int MaxSpeed;

   int Weight;

public:

    vehicle(){MaxSpeed=0; Weight=0;};

   ~vehicle(){};

   void Run() {cout << "Now it is running!" << endl; }

   void Stop() {cout << "Now it has stopped!" << endl; }

};

class bicycle : virtual public vehicle

{

private:

   int Height;

public:

   bicycle(){};

   ~bicycle(){};

};

class motorcar : virtual public vehicle

{

private:

   int SeatNum;

public:

   motorcar(){};

   ~motorcar(){};

};

class motorcycle : public bicycle , public motorcar

{

public:

   motorcycle (){};

   ~motorcycle (){};

};

int main()

{

   motorcycle a;

   a.Run();

   a.Stop();

}

相关推荐