C++ study notes twelve-structure union enumeration class

Structure The
structure is actually a special kind. It is derived from C++ and C language, but it is different from classes in the following two points:
1. The keyword is struct, the keyword of the class is class.
2. The default attribute of the class is public, and the default attribute of the class is private.
Others are the same as those in C++ The method of use is the same. Here is an example to illustrate the use of structure

#include <iostream>
using namespace std;
struct test {
    
    
private:
	int i;
	int j;
public:
	test(int i1, int j1) :i(i1), j(j1) {
    
    }
	int getx(){
    
    return i;}
	int gety() {
    
     return j; }
};
int main()
{
    
    
	test rui(3, 2);//构造函数初始化
	cout << rui.getx() << endl;//输出成员x的值
}

Consortium

联合体利用关键字 union声明,最主要的特点是储存空间的共用,其中成员也包括private public 以及protected属性,但是由于共用一个储存空间的特点所以。任何数据不可能同时有效,具体储存如下图所示

image description
The above is a schematic diagram of the consortium memory.
You can see that there is a shared memory, so when there is the following code

union mark{
    
    
int i;
int j;
};
i=1;
j=2

When the value of j is equal to 2, the value of i will be overwritten because they share a memory.

Enumeration class
similarity with enumerated types specifically enumerated type, but with the enumerated type is different, there is strong scope enumeration class, while it is necessary to add enumeration value defining the domain, not all the same name, The meaning will be clearer.
Examples are as follows

enum {
    
    
		Grade,
		Pass,
		Percentage
	}mode;

And the enumeration class can set the underlying value, the specific format is as follows:
enum calss enumeration type name: underlying type {enumeration value table}; the default is int, other types can also be specified.
Let's write an example of the integration of three categories to create an exam information category. There are three statistical methods int char bool. According to the three types, different forms of scores are output. The specific code is as follows

#include <iostream>
# include<string>
using namespace std;
class examinfo {
    
    
private:
//课程名字
	string name;
//枚举类mode	
	enum {
    
    
		Grade,
		Pass,
		Percentage
	}mode;
//无名联合体,就是没有名字的类,可以直接用
	union {
    
    
		char grade;
		bool pass;
		int percent;
	};
public:
//不同类型的构造函数,根据不同的模式选择不同的初始化对象
	examinfo(string name, char grade1) :name(name), mode(Grade), grade(grade1) {
    
    }
	examinfo(string name, int precent) :name(name), mode(Percentage), percent(precent) {
    
    }
	examinfo(string name, bool pass) :name(name), mode(Pass), pass(pass) {
    
    }
	void show();
	
};
void examinfo::show() {
    
    
//输出课程名
	cout << "name  : " << name << endl ;
//选择模式
	switch (mode)
	{
    
    
	case examinfo::Grade:
		cout << "grade is :" << grade << endl;
		break;
	case examinfo::Pass:
		cout << "pass is " << pass << endl;
		break;
	case examinfo::Percentage:
		cout << "percent is " << percent << endl;
		break;
	default:
		break;
	}
	cout << endl;
}
int main()
{
    
    
	examinfo english("english", 109);
	english.show();
	examinfo chinese("chinese", 'A');
	chinese.show();
	examinfo math("math", false);
	math.show();

	
}

The results of running the code are as follows. The
operation result
above is the basic concept of structure union and enumeration class

Guess you like

Origin blog.csdn.net/qq_41803340/article/details/112603831
Recommended