由struct类过度到类
C中的struct类与C++中struct的区别
我们在C语言中都学过struct类型 他是一种符合类型 里面可以定义各种基本类型的变量 但是不知道你是否留意 struct类里面不能定义函数
例子1:
#include <stdio.h> struct Student { int a; int b; void show() { //非法的 } }; int main() { return 0; }
#include <iostream> using namespace std; struct Student { int a; int b; void show() { //合法的 } }; int main() { return 0; }
通过上面的这个例子我们可以看出,
struct类型在C中不可以定义函数,但是在C++中可以定义函数
在看一下第二个例子
例子2
#include <stdio.h> struct Student { int a; int b; }; int main() { struct Student s1;//合法的 Student s1;//非法的 return 0; }
#include <iostream> using namespace std; struct Student { int a; int b; void show() { //合法的 } }; int main() { struct Student s1;//合法的 Student s1;//合法的 return 0; }
通过例子2我们可以看出,在C中如果不用typedef关键字,我们定义结构体变量只能用struct Student,使用Student是不合法的,而在C++中这俩种方式都是合法的
在C中结构体名称不可以定义变量,但是C++中结构体名称可以定义变量
结合这俩点我们不难看出 C++对结构体类型进行了某种程度的升级
“某种程度”-->函数的升级,以及定义“变量”的升级,都更加方便了
这种升级就是把struct升级成了C++语言中的类
这就是我们今天的主题,在这里就引出了
类
可以把类看成就C中struct类型的plus版
C++面向对象的三大特点
1.封装
2.继承
3.多态
C++中认为万事万物皆为对象,对象有它的属性和行为
属性-->成员变量
行为-->成员函数
封装
格式:class 类名 { 访问权限:属性、行为};
不要忘了分号!!!
意义:讲属性和行为放在一起,并加以权限控制
C语言太自由了主函数中向修改结构体类型里面变量的值就修改
C++把它们封装了起来,加了访问权限的控制
访问权限:公有的、受保护的和私有的
1.public
2.protected
3.private
C++中类下默认访问权限是private
struct类的默认访问权限是public,因为要兼容C中的struct类
#include<iostream> using namespace std; struct Student { //struct类里面没有说明什么权限 默认权限是public //class类里面没有说明什么权限 默认权值是private string _name; string _num; void Set_name(string name) { _name = name; } void Set_num(string num) { _num = num; } void show() { cout << _name << endl << _num << endl; } }; int main() { Student s1;//合法的 s1._name = "123";//合法的默认是public return 0; }
#include<iostream> using namespace std; class Student { //struct类里面没有说明什么权限 默认权限是private //class类里面没有说明什么权限 默认权值是class string _name; string _num; void Set_name(string name) { _name = name; } void Set_num(string num) { _num = num; } void show() { cout << _name << endl << _num << endl; } }; int main() { Student s1;//合法的 s1._name = "123";//非法的 默认位private 访问不到 return 0; }
类的作用域
public-->共有的作用域为类内函数、子类和对象
protected-->受保护的作用域为类内函数、子类
private-->私有的作用域类内函数
this关键字
先看个例子
#include<iostream> using namespace std; class Student { //struct类里面没有说明什么权限 默认权限是private //class类里面没有说明什么权限 默认权值是class private: string _name; string _num; public: void Set_name(string name) { _name = name; } void Set_num(string num) { _num = num; } void show() { cout << _name << endl << _num << endl; } }; int main() { Student s1;//合法的 Student s2; s1.Set_name("syc");//语句1 s2.Set_name("lh");//语句2 return 0; }
语句1要执行的是把字符串syc赋值给s1的_name
语句2要执行的是把字符串lh赋值给s2的_name
这里的s1的和s2的就是this关键字
this关键字是指向当前对象的指针,谁调用这个函数,this指针就指向谁
本质:this指针的本质是在实参传值给形参的过程中,形参多了一个指针常量this用来接收调用这个函数对象的地址
#include<iostream> using namespace std; class Student { private: string _name; string _num; public: void Set_name(Student* const this1,string name) {//指针常量 指向方向不变 this1->_name = name; } void Set_num(Student* const this2,string num) { this2->_num = num; } void show() { cout << _name << endl << _num << endl; } }; int main() { Student s1;//合法的 Student s2; s1.Set_name(&s1,"syc"); s2.Set_name(&s2,"lh"); return 0; }
特点:其实this指针的特性在上一个模块(this指针的引出)已经讲解的差不多了,这里简要说明:
1.this指针的类型:类类型* const 2.只能在“成员函数”的内部使用 3.this指针本质上其实是一个成员函数的形参,是对象调用成员函数时,将对象地址作为实参传递给this形参。所4.以对象中不存储this指针。 5.this指针是成员函数第一个隐含的指针形参,一般情况由编译器通过ecx寄存器自动传递,不需要用户传递
此处为摘抄。
类的对齐和结构体的对齐很相似
注意:计算类的大小不包括成员函数,只计算成员变量