c++怎么给一个类里的结构体中的变量赋值

初学c++,我写一个类,里面有个结构体,结构体里有几个变量,我想给结构体里变量赋值,怎么写呢,比如说:
头文件中我定义
class person{
string name;
string gender;
srtuct brith{
int year;
int month;
int day;
};
void set(int,int,int);
};
我在cpp里怎么给这个结构体brith里的年月日赋值呢?

这个应该算嵌套类的使用了,首先你这个结构申明是私有(private)的吗?如果是的话,只能用构造函数逐一赋值:
class person{
string name;
string gender;
struct brith
{ int year; int month; int day; };
brith b;//创建实例
public:
void set(int,int,int);
person (){name="xiaoming";gender="male";b.year=1995;b.month=9;b.day=23;}
};
int main()
{
person p;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-10-07
struct brith b = {2014, 1, 1};
person p = {"小明",“男”,b,};追问

那我该怎么去写这个.h的cpp呢,我想在其中写一个cpp,就是上面那个set()
这个我怎么在set给这个结构体赋值,也就是说怎么写这个set的函数?
void person::set(int, int, int)
{
此处给那个stuct brith写赋值?
}
我主要是不会怎么加前缀
是加“::”?还是加“.”?

第2个回答  2014-10-07
要有结构体的对象或指针才行
第3个回答  2014-10-07
构造函数

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网