Related Posts Plugin for WordPress, Blogger... 簡單易懂的低調手札: [memo] C++ 動態結構

2011年8月21日 星期日

[memo] C++ 動態結構

#include <iostream>

struct inflatable
{
    char name[20];
    float volume;
    double price;
};


//宣告一個 "空結構" & "結構成員"

int main() {
    using namespace std;
    inflatable *ps = new inflatable;


//建立動態結構 ps
    
    cout << "Enter name of inflatable item: " ;
    cin.get(ps->name,20);


//輸入 char 指向結構成員 name 並限制 char 大小

    cout << "Enter your price: $ ";
    cin >>ps->price;


//輸入 double 指向結構成員 price

    cout << "Enter volume in cubic feet: ";


    /*cin >> ps->volume;   //效果與下面是一樣的*/


   cin >> (*ps).volume; 


// (*ps) 考慮運算子優先所以用括號。


// *ps 是結構;(*ps).volume 是結構的成員


// 上述兩種均是輸入 float 指向結構成員 volume


    cout << "Name: " << (*ps).name << endl;
    cout << "Volume: " << ps->volume << " cubic feet\n" ;
    cout << "price: $ " << ps->price << endl;
    delete ps;

    system("pause");
    return 0;
}


 



沒有留言:

張貼留言