C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  117 Posts :: 2 Stories :: 61 Comments :: 0 Trackbacks

常用链接

留言簿(8)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

第一个程序,struct的基本使用

#include "stdafx.h"
#include 
<iostream>
using namespace std;
struct inflatable
{
    
char name[20];
    
float volume;
    
double price;
}
;  //注意,这个声明后面有个分号!

int main(int argc, char* argv[])
{
    inflatable quest
=
    
{
        
"Clorious Gloria",//name value
        1.88,//volume value
        29.99//price value
    }
;

    inflatable pal
=
    
{
        
"Audacious Arthur",
        
3.12,
        
32.99
    }
;

    cout
<<"Expand your quest list with "<<quest.name;
    cout
<<" and "<<pal.name<<"!\n";

    cout
<<"You can hava both for $";
    cout
<< quest.price+pal.price<<"!\n";
    
return 0;
}


第二个程序:使用new创建一个未命名的结构,并演示两种访问结构成员的指针表示法。

#include "stdafx.h"
#include 
<iostream>
using namespace std;
struct inflatable
{
    
char name[20];
    
float volume;
    
double price;
}
;
int main(int argc, char* argv[])
{
    inflatable 
* ps=new inflatable; //allot memory for structure
    cout<<"Enter name of inflatable item: ";
    cin.
get(ps->name,20);    //method 1 for member access
    
    cout
<<"Enter volume in cubic feet: ";
    cin
>>(*ps).volume;    //method 2 for member access

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

    cout
<<"Name: "<<(*ps).name<<endl;//method2
    cout<<"Volume:"<<ps->volume<<" cubic feet\n";//method 1
    cout<<"Price: $"<<ps->price<<endl;
    delete ps;
//free memory used by structure
    return 0;
}
posted on 2010-02-05 22:53 烟皑 阅读(288) 评论(0)  编辑 收藏 引用 所属分类: C++ primer plus学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理