#include <iostream>
using namespace std;
struct A
{
void add(int a,int b)
{
cout << a <<"+" <<b <<"="<<a+b <<endl;
}
};
int main()
{
A s;
s.add(3,4);
return 0;
}
void add(int a,int b)
{
cout << a <<"+" <<b <<"=" << a+b <<endl;
}
#include <iostream>
using namespace std;
class Stu
{
//C++中的结构体可以添加访问权限
int age = 18;
public:
//结构体中只写函数声明
int get_age(); //公有函数,返回结构体中私有成员age的值
void set_age(int a);
string name;
struct A
{
int tall;
}a;
};
//结构体外实现函数的定义
int Stu::get_age()
{
return age;
}
void Stu::set_age(int a)
{
age = a;
}
int main()
{
//C++中定义结构体变量时,可以不加struct
Stu s1;
//cout << s1.age << endl;
cout << s1.get_age() << endl;
s1.set_age(30);
cout << s1.get_age() << endl;
cout << s1.name << endl;
//Stu::A a1;
s1.a.tall=180;
return 0;
}
结构体大小:
16? 12? 12? ?6? ?40? ?104? ?12? ?5? ?32