zoukankan      html  css  js  c++  java
  • c++ 类与对象

    实验二类与对象

    一、实验目的

    1、学习类与对象的定义,掌握类与对象的使用方法。

    2、学习数据成员与成员函数的访问方式,理解构造函数和析构函数的定义与执行过程,学会构造函数的重载方法。

    3、掌握数组与指针的定义与使用方法,理解数组与指针的存储分配与表示。

    4、掌握用指针和引用向函数传递参数。

    5、掌握静态数据成员和静态成员函数的使用。

    6、理解友元与友元函数的作用与使用方法。

    二、实验内容

    1、下面是一个计算器类的定义,请完成该类成员函数的实现。

    class Counter

    {

       public:

         Counter(int number);

         void increment();   //给原值加1

         void decrement();  //给原值减1

         int getValue();    //取得计数器值

         int print();       //显示计数

       private:

         int value;

    };

     

     

     

     

    #include <iostream>

    using namespace std;

    class Counter

    {

       public:

           Counter(int number)

           {

               value=number;

           }

           void increment()

           {

               value+=1;

             

           }

                  //给原值加1

           void decrement()

           {

               value=value-1;

              

           }     //给原值减1

           int getValue()

           {

               return value;

           }    //取得计数器值

           int print()

           {

               cout<<value;

               return value;

           }       //显示计数

       private:

           int value;

    };

    int main()

    {

       

        Counter A(6);

        cout<<"the munber is:";

        A.print();

        cout<<endl;

        A.decrement();

        cout<<"value-1="<<A.print()<<endl;

        A.increment();

        cout<<"value+1="<<A.print()<<endl;

        return 0;

    }

     

    2、根据注释语句的提示,实现类Date的成员函数。

    class Date

    {

       public:

         void printDate();//显示日期

         void setDay(int d);//设置日的值

         void setMonth(int m);//设置月的值

         void setYear(int y);//设置年的值

       private:

         int day,month,year;

     

    };

    int main()

    {

      Date testDay;

      testDay.setDay(5);

      testDay.setMonth(10);

      testDay.setYear(2014);

      testDay.printDate();

      return 0;

    }

     

    3、建立类cylinder,cylinder的构造函数被传递了两个double值,分别表示圆柱体的半径和高度。用类cylinder计算圆柱体的体积,并存储在一个double变量中。在类cylinder中包含一个成员函数vol(),用来显示每个cylinder对象的体积。

    #include <iostream>

    using namespace std;

    class cylinder

    {

    private:

      double r,high,v;

    public:

      cylinder(double a,double b)

      {

          r=a,high=b;

      }

      double rol()

      {

     

          v=4*3.14*r*r*high;

          return v;

      }

    };

    int main()

    {

      cylinder A(4.0,5.0);

      double volume;

      volume=A.rol();

      cout<<"the volume of the cylinder is:"<<volume<<endl;

      return 0;

    }

    4、构建一个类book,其中含有两个私有数据成员qu和price,建立一个有5个元素的数组对象,将qu初始化为1~5,将price初始化为qu的10倍。显示每个对象的qu*price值。

     

     

    #include <iostream>

    using namespace std;

    class book

    {

    private:

        int qu,price;

    public:

        book(int q)

        {

            qu=q;

            price=10*qu;

        }

        void print()

        {

            cout<<"qu*price=:"<<qu*price<<endl;

        }

    };

    int main()

    {

        book b[5]={1,2,3,4,5};

        for(int i=0;i<5;i++)

        b[i].print();

        return 0;

    }

     

    5、修改上题,通过对象指针访问对象数组,使程序以相反的顺序显示对象数组的qu*price值。

     

     

     

     

     

    #include <iostream>

    using namespace std;

    class book

    {

    private:

        int qu,price;

    public:

        book(int q)

        {

            qu=q;

            price=10*qu;

        }

        void print()

        {

            cout<<"qu*price=:"<<qu*price<<endl;

        }

    };

    int main()

    {

        book b[5]={1,2,3,4,5};

        book *p;

        p=b;

        p=p+5;

        for(int i=0;i<5;i++)

        {p--;

        p->print();

        }

        return 0;

    }

     

    6、构建一个类Stock,含字符数组stockcode[]及整型数据成员quan、双精度型数据成员price。构造函数含3个参数:字符数组na[]及q、p。当定义Stock的类对象时,将对象的第一个字符串参数赋给数据成员stockcode,第2个和第3个参数分别赋给quan和price。未设置第2个和第3个参数时,quan的值为1000,price的值为8.98。成员函数print()使用this指针,显示对象内容。

     

     

     

     

     

    #include <iostream>

    #include<string>

    using namespace std;

    class stock

    {

    private:

        char stockcode[100];

        int quan;

        double price;

    public:

        stock(char na[],int q=100,double p=8.98)

        {

            strcpy(stockcode,na);

            quan=q;

            price=p;

        }

        void print()

        {

        cout<<"stockcode="<<this->stockcode<<endl<<"quan="<<this->quan<<endl<<"price="<<this->price<<endl;

        }

    };

        int main()

        {

            stock a("hello");

            a.print();

            stock b("hello",2,5.6);

            b.print();

            return 0;

        }

     

     

     

     

     

     

     

     

     

    7、参考课本例子,建立一个源程序文件,在此文件中建立一个新的类,将新建的类命名为Rect。

    class Rect

    {

    public:

       int Area_int();

       double Area_double();

       Rect(double length,double width);

       Rect(int length,int width);

       virtual ~Rect();

    private:

       int nLength;

       int nWidth;

       double dLength;

       double dWidth;

    };

    【要求】

    (1)向Rect类中添加数据成员及成员函数,并完善成员函数的功能。如设计一个Area_int()函数,计算边长为整型的长方形的面积;设计一个Area_double()函数,计算边长为double型的长方形的面积。

    (2)重载构造函数。一种构造函数用整型变量记录长方形的长和宽,另一种构造函数用double型记录。

    (3)体现对象的构造和析构过程。例如,在构造函数中用cout<<”I am the constructor!”<<endl;在析构函数中输出cout<<”I am the destructor”<<endl。

    (4)在main()函数中定义两个Rect类的对象,一个对象用实例实现(就像定义普通的变量一样),另一个对象用指针实现(利用关键字new,给指针分配内存空间)。并用不同的参数,以调用不同的构造函数体现构造函数的重载。

     

     

     

     

    #include <iostream>

    #include<string>

    using namespace std;

    class Rect

    {

    public:

       int Area_int()

       {

           int Area;

           Area=nLength*nWidth;

           cout<<"the area is:"<<Area<<endl;

           return Area;

       }

       double Area_double()

       {

           double Area;

           Area=nLength*nWidth;

           cout<<"the area is:"<<Area<<endl;

           return Area;

       }

       Rect(double length,double width)

       {

           dLength=length;

           dWidth=width;

           cout<<"I am constructor!"<<endl;

       }

       Rect(int length,int width)

       {

           nLength=length;

           nWidth=width;

           cout<<"I am constructor!"<<endl;

       }

       ~Rect()

       {

           cout<<"I am the destructor"<<endl;

       }

    private:

       int nLength;

       int nWidth;

       double dLength;

       double dWidth;

    };

    int main()

    {

        Rect a1(3,5);

        Rect *a2=new Rect(2.3,6.3);

        a1.Area_int();

        a2->Area_double();

        return 0;

    }

     

    8、声明一个Student,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。在main()函数中,输入某班学生的成绩,并调用上述函数求出全班学生的成绩之和和平均分。

     

     

     

     

    #include <iostream>

    using namespace std;

    class Student

    {

    private:

        float score;

        static float total_score;

        static float count;

    public:

        void account()

        {

            cout<<"please putin the score:"<<endl;

            cin>>score;

            total_score+=score;

            count++;

        }

        static void sum()

        {

                 cout<<"the sum is:"<<total_score<<endl;

        }

        static void average()

        {

             cout<<"the average is:"<<total_score/count<<endl;

        }

    };

     

    float Student::total_score=0.0;

    float Student::count=0;

    int main()

    {

        Student s[3];

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

        {

            s[i].account();

        }

        Student::sum();

        Student::average();

       

                 return 0;

    }

     

     

     

    9、设计一个用来表示直角坐标系的Location类,在主程序中创建类Location的两个对象A和B,要求A的坐标点在第3象限,B的坐标点在第2象限,分别采用成员函数和友元函数计算给定两个坐标点之间的距离,要求按如下格式输出结果:

    A(x1,y1),B(x2,y2)

    Distance=d

    其中:x1、y1、x2、y2为指定的坐标值,d为两个坐标点之间的距离。

     

     

     

     

     

    #include<iostream>

    #include<cmath>

    using namespace std;

    class location

    {

    private:

        float x;

        float y;

        float distance;

    public:

        location(float a,float b)

        {

            x=a;

            y=b;

        }

        friend void dis(location &m,location &n)

        {

            float dis;

                 dis=sqrt((m.x-n.x)*(m.x-n.x)+(m.y-n.y)*(m.y-n.y));

                cout<<dis;

    }

     

     

        float dis(location c)

        {

            distance=sqrt((x-c.x)*(x-c.x)+(y-c.y)*(y-c.y));

            return distance;

        }

    };

        int main()

        {

            location A(-3,-3);

            location B(-3,3);

            float d;

            d=A.dis(B);

            cout<<"A(-3,-3),B(-3,3)"<<endl<<"distance="<<d<<endl;

           

            cout<<"A(-3,-3),B(-3,3)"<<endl<<"distance i=";

            dis(A,B);

            cout<<endl;

            return 0;

    }

     

     

    10、使用C++的string类,将5个字符串按逆转后的顺序显示出来。例如,逆转前的5个字符串是:Germany、Japan、American、British、France

    按逆转后的顺序输出字符串为:

    France、British、American、Japan、Germany

     

     

     

     

     

     

     

    #include <iostream>

    #include<string>

    using namespace std;

    int main()

    {

        string s[5]={"german","japan","american","british","france"};

        for(int i=4;i>=0;i--)

            cout<<s[i]<<endl;

        return 0;

    }

    11、设计一个矩阵类Matrix,有分配空间和对矩阵赋值的功能,将这个矩阵类的对象作为参数传送到函数Mul(Matrix a,Matrix b),用Mul(Matrix a,Matrix b)函数实现两个Matrix对象相乘的运算。

     

     

     

    #include<iostream>

    using namespace std;

    class Matrix

    {

    private:

        int **x;

    public:

        int m,n;

        Matrix()

        {

            cout<<"please input the number of matrix's row:";

            cin>>m;

            cout<<"please input the number of matrix's column:";

            cin>>n;

            cout<<endl<<"please input ethe number:"<<endl;

            x=new int*[m];

            for(int i=0;i<m;i++)

            {

                 x[i]=new int[n];

            }

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

            {

                 for(int r=0;r<n;r++)

                 {   cin>>x[i][r];}

            }

        }

     

     

            void Mul(Matrix a,Matrix b)

            {

                 int t=0;

                 cout<<"the product of twomatrices is:"<<endl;

                 for(int i=0;i<m;i++)

                 {

                     for(int j=0;j<m;j++)

                    

                        

                             t+=a.x[i][j]*b.x[j][i];

                                                    cout<<t<<" ";

    t=0;

    cout<<endl;

    }

                    

                 }

    };

           

            int main()

            {

                 Matrix a1,a2;

                

                 a1.Mul(a1,a2);

                 return 0;

            }

     

    朝闻道
  • 相关阅读:
    上周热点回顾(7.29-8.4)团队
    云计算之路:AWS, Azure, Aliyun, UCloud提供的Windows操作系统团队
    上周热点回顾(7.22-7.28)团队
    我的MYSQL学习心得(推荐)
    深度学习笔记之使用Faster-Rcnn进行目标检测 (实践篇)
    深度学习笔记之使用Faster-Rcnn进行目标检测 (原理篇)
    深度学习笔记之基于R-CNN的物体检测
    深度学习笔记之目标检测算法系列(包括RCNN、Fast RCNN、Faster RCNN和SSD)
    深度学习笔记之神经网络、激活函数、目标函数和深度的初步认识
    深度学习笔记之CNN(卷积神经网络)基础
  • 原文地址:https://www.cnblogs.com/wander-clouds/p/8443769.html
Copyright © 2011-2022 走看看