[C++]cpp小笔记3 --- C++ Structures
2013-12-10 14:41
190 查看
注:语法:在头文件中的变量,只有static const类型的变量才能被初始化!!
、
在头文件中:
use pointer to access structures
1. Struct的声明。
、
在头文件中:
public: struct Person { char name[20]; int height; int weight; }; struct Food { string name; int weight; } orange, apple; struct Point { int x; int y; } point; struct Circle { Point point; int radius; }; struct { string name; } test;注意最后一个test因为没有定义struct type name,所以这个struct无法在后面在定义新的类型。
2. Struct的使用
Person me = {"x", 187, 190}; Person Parents[] = { {"y", 166, 100}, {"z", 170, 150} }; cout << "Person: " << me.name <<" is " << me.height << " tall and " << me.weight <<" weight"<< endl; int size = sizeof(Parents) / sizeof(Person); for(int i = 0; i < size; i ++) { cout << "Person: " << Parents[i].name <<" is " << Parents[i].height << " tall and " << Parents[i].weight <<" weight"<< endl; } apple = {"apple", 12}; orange = {"orange", 13}; cout << "Food: " << orange.name <<" is " << orange.weight <<" weight"<< endl; Food banana={"banana", 14}; Food strawberry; strawberry.name="strawberry"; strawberry.weight = 18;
Circle circle= {{10,20}, 20}; cout << "The point of a circle is "<<circle.point.x << " and " <<circle.point.y << " ,and radius is" << circle.radius<<endl;
use pointer to access structures
Point * point; point->x = 10; point->y = 20;
相关文章推荐
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记三
- Data Structures with C++ Using STL Chapter 2对象设计技术---笔记
- Data Structures with C++ Using STL Chapter 1数据结构入门---笔记
- [C++]cpp小笔记3 --- C++数组, 数组指针
- [C++]cpp小笔记3 --- C++基础类型的大小和转换
- Data Structures with C++ Using STL Chapter 4向量容器---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 5指针和动态内存---笔记
- Data Structures and Algorithm Analysis in c++ 第一章笔记和部分习题
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- [C++]cpp小笔记3 --- C++ String and char
- 【C++】笔记一:Microsoft Visual Studio 2010 软件的安装与建立第一个cpp文件
- Data Structures with C++ Using STL Chapter 3算法概述---笔记
- [cpp]C++笔记
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记五
- Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记二
- Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四
- 【C++】【学习笔记】【001】CPP语言与OO思想介绍
- The C++ Programming Language 第七章 笔记