C++中的结构体、联合、枚举

一、C++中struct

1、C++和C语言的区别
(1)C++中的空结构体求sizeof大小为1,C中的空结构体大小为0

#include <iostream>
using namespace std;

//C++中的空结构体的大小为1
struct A{	
};
struct B{
	char c;	
	void show(){
		cout << "show" << endl;	
	}
};
int main(){
	cout << sizeof(struct A) << endl;
	cout << sizeof(struct B) << endl;
	B b;
	return 0;	
}

(2)C++中结构体类型名 struct 可以省略, C中结构体类型名struct不可以省略
(3)C++中结构体中可以直接声明定义函数(成员函数),C中不可以定义函数

#include <stdio.h>
//在C语言结构体中可以声明函数指针,但是不可以直接声明函数
struct A{
};
struct B{
	char c;	
	void (*f)();//函数指针
	/*
	void show(){	
	}
	*/
};
int main(){
	printf("%d\n",sizeof(struct A));	
	struct B b;
	return 0;
}

C++结构体中的成员函数可以直接访问结构体中的成员,但是静态函数不可以:

#include <iostream>
using namespace std;

struct Stu{
	int no;
	char name[24];
	int age;
	//结构体中的成员函数可以直接访问结构体中的成员
	void show(){
		cout << no << ":" << name << ":" << age << endl;
	}
	//结构体中的静态函数 不可以访问结构体中的成员
	static void print(){
		//cout << no << ":" << name << ":" << age << endl;	
	}
};
int main(){
	Stu s = {119,"张三",33};
	Stu t = {120,"李四",34};
	//s.no = 100;
	s.show();
	t.show();
	s.print();
	return 0;	
}

多文件实现名字空间中结构体中的成员函数
.h中:

namespace NS{
	struct Stu{
		int no;
		char name[24];
		int age;
		void show();
	};
	void print(Stu s);
}

二、C++中的union

1、联合只是变量在内存中的布局方式,本质是成员共享同一块内存;
2、共用体 多个变量共用同一块内存;
3、C++支持匿名联合;
4、若联合为全局作用域,则必须声明static

union{
 	int m_i;
	char m_c[4];
};

相当于定义了两个变量 m_i和m_c,这两个变量共享同一块内存,即&m_i == &m_c
可以直接访问这m_i和m_c

#include <iostream>
using namespace std;
//联合  共用体  成员共用同一块内存
union X{
	char c;
	int m;
};
int main(){
	X x;
	x.m = 97;
	cout << x.c << endl;
	//匿名联合
	union{
		int m_i;
		char m_c[4];
	};
	cout << &m_i << endl;
	cout << &m_c << endl;
	m_i = 0x61626364;
	cout << m_c[0] << m_c[1] << m_c[2] << m_c[3] << endl;
	return 0;	
}

三、C++中的enum

C++是一种强数据类型的编程语言:
enum和int是两种不同的数据类型
不能直接用 int 类型值 直接 赋值 给 枚举类型变量
但枚举值本质上还是一个int类型,所以可以把枚举值 或者 枚举变量赋值给int变量
例如:

enum Dir{UP,DOWN,LEFT,RIGHT};
Dir d = DOWN;
d = 1;//将int类型的数赋值给enum 会报错
int x = DOWN;
int y = d;

完整代码如下:

#include <iostream>
using namespace std;

enum Dir{UP=100,DOWN,LEFT,RIGHT};
int main(){
	enum Dir d = LEFT;
	cout << d << endl;
	//d = 2;//不能把int赋值给枚举类型变量
	int x = d;
	cout << x << endl;
	x = LEFT;	
	return 0;	
}

四、C++中的bool

1、C语言中在stdbool.h 声明了bool类型,但是C++中bool是一种基本数据类型;
2、bool类型 非"零"即真
为零的如下:
(1)false
(2) NULL
(3) 0
(4) 0.0
(5) ‘\0’
3、cout << boolalpha << true << endl; //能够以 true 和 false 的形式输出 布尔变量的值

#include <iostream>
using namespace std;

int main(){
	bool b = true;
	cout << b << endl;
	b = false;
	cout << b << endl;

	b = 1;
	cout << b << endl;
	b = 0;
	cout << b << endl;
	b = 0.000000000001;
	cout << b << endl;
	b = NULL;
	cout << b << endl;

	bool bf = '\0';
	cout << bf << endl;
	
	cout << true << endl;
	cout << boolalpha << true << endl;
	cout << false << endl;
	return 0;	
}

五、C++中运算符的别名

在C++中一下运算符相互等价:

	&&    and
	||    or
	!     not
	{     <%
	}     %>
	[     <:
	]     :>
	&     bitand
	|     bitor
	^     xor

程序举例:

#include <iostream>
using namespace std;

int main()
<%
	cout << "Hello world!" << endl;
	int arr[10] = {1,2,3};
	cout << arr<:0:> << endl;
	int n = 2010;
	bool b = n%4==0 and n%100!=0 or n%400==0;
	cout << b << endl;
	int a = 10;
	int c = 21;
	cout << (a xor c) << endl;
	return 0;
%>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值