第4周課後實踐·閱讀程式-建構函式和解構函式(3)

不被看好的青春叫成長發表於2015-03-27
*  
 * Copyright (c) 2015, 煙臺大學計算機學院  
 * All rights reserved.  
 * 檔名稱:test.cpp  
 * 作    者:劉暢   
 * 完成日期:2015年 3 月 27 日  
 * 版 本 號:v1.0  
 * 
 * 問題描述: 閱讀程式。
 * 輸入描述:NULL;
 * 程式輸出:按要求輸出。


程式碼如下:

#include <iostream>
using namespace std;
class toy
{
public:
    toy(int q,int p)
    {
        quan=q;
        price=p;
    }
    int get_quan( )
    {
        return quan;
    }
    int get_price( )
    {
        return price;
    }
private:
    int quan,price;
};
int main()
{
    toy op[3][2]=
    {
        toy(10,20),toy(30,48),
        toy(50,68),toy(70,80),
        toy(90,16),toy(11,120)
    };
    int i;
    for(i=0; i<3; i++)
    {
        cout<<op[i][0].get_quan()<<",";
        cout<<op[i][0].get_price()<<"\n";
        cout<<op[i][1].get_quan()<<",";
        cout<<op[i][1].get_price()<<"\n";
    }
    cout<<"\n";
    return 0;
}


執行結果:

 

相關文章