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;

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM