结构体区别
1.定义结构体与C语言一致
2.定义变量可省略关键字struct
3.C++结构体中允许函数存在(C++在没有写构造函数和权限限定的时候,用法和C语言一致)
(1)函数可以直接在结构体中实现,也可在结构体中声明,在结构体外实现,
(2)结构体中函数可以直接访问结构体中数据
(3)学会调用:
对象(结构体变量).成员
对象指针->成员
(*对象指针).成员
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
struct MM
{
//属性,特性 数据成员
char name[20];
int age;
void print()//行为方法 成员函数
{
cout << name << "\t" << age << endl;
}
void printData();//结构体中声明
int& putage()
{
return age;
}
};
void MM::printData()//在结构体外实现
{
cout << name << "\t" << age << endl;
}
//结构体中的变量必须要通过结构体变量(结构体指针)访问
//C++结构体中的函数可以直接访问属性
int main()
{
MM a = {"tatata",21};//等效于struct MM a;
a.