马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在类中有一个枚举类型变量,如何让此变量有默认值
如果创建一个类,不给此变量值,默认是负多少多少,我想让它默认是0,请问如何解决
以下是类的代码
#pragma once
#include <iostream>
#include <string>
using namespace std;
enum ESex
{
ESex_Null,
ESex_FMale,
ESex_Male,
};
class CStudent
{
public:
string name;
int num;
ESex sex ;
int age;
};
以下是主程序代码#include <iostream>
#include <string>
#include "Student.h"
using namespace std;
int main()
{
CStudent stu_1;
stu_1.sex = ESex_Male;
cout << stu_1.sex << endl;
CStudent stu_2 = {"zhangsan", 1002, ESex_FMale, 23};
cout << stu_2.sex << endl;
CStudent stu_3;
cout << stu_3.sex << endl;
return 0;
}
class CStudent
{
public:
string name;
int num;
ESex sex;
int age;
CStudent()
{
sex=ESex_Null;
}
CStudent(string n,
int nm,
ESex s,
int a)
{
name=n;
num=nm;
sex=s;
age=a;
}
};
int main()
{
CStudent stu_1;
stu_1.sex = ESex_Male;
cout << stu_1.sex << endl;
CStudent stu_2 ("zhangsan", 1002, ESex_FMale, 23);
cout << stu_2.sex << endl;
CStudent stu_3;
cout << stu_3.sex << endl;
return 0;
}
|