3

C++类和对象(上)_萌新的日常的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_15787387/5929678
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C++类和对象(上)

精选 原创

<font color=red>1.将数据和方法放到定义一起</font>

<font color=Blue>c++类的成员函数 即方法 ,成员变量即数据</font>

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x);//成员函数
private:
	int b;//成员变量
};
int main()
{
	return 0;
}

<font color=red>2.把想给你看到的数据给你,不想给你看封装起来</font>
<font color=Blue>访问限定符: public (共有的) protected (保护) private (私有)
a属于public 类中 ,属于想给你看到的数据 ,而 b属于 private 类中,属于 不想给你看到的</font>

#include<iostream>
using namespace std;
class stack
{
public:
	int a;
private:
	int b;
};
int main()
{
	return 0;
}

2.关于c语言和c++对于struct用法

<font color=red>c语言中struct是用来定义结构体的</font>

struct listnode_C//c语言定义结构体
{
	int val;
	struct listnode_C* prev;
	struct listnode_C* next;
};

<font color=red>c++中,兼容c的struct定义结构体的用法,但是同时struct也可以用来定义类</font>

struct listnode_CPP//C++定义类,把listnode_CPP看作类名
{
	int val;
	listnode_CPP* prev;
	listnode_CPP* next;
	listnode_CPP* createnode(int val);//定义成员函数
};

3. c++中使用class和struct定义类的区别?

<font color=red>默认访问限定符不同</font>

struct listnode_CPP//使用struct 
{
	int val;
	listnode_CPP* prev;
	listnode_CPP* next;
};

<font color=Blue>使用struct 默认访问限定符为 public </font>

class listnode_CPP//使用class
{
	int val;
	listnode_CPP* prev;
	listnode_CPP* next;
};

<font color=Blue>使用class 默认访问限定符为private</font>

4. 类的实例化

<font color=Blue>用类类型创建对象的过程,成为类的实例化
类也可以看作是设计图,而类的实例化则使用图纸建造房子</font>

1.成员变量的声明和定义

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x);//成员函数
private:
	int a;//成员变量的声明
};
int mian()
{
  //类的实例化,通过stack这个类 创建s1这个对象
	stack s1;//相当于将成员函数定义
	return 0;
}

2.成员函数的定义和声明

1.在类外面定义

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x);//成员函数
	void pop();
private:
	int a;
};
void stack::pop()//在类外面定义
{
	//...
}
int mian()
{
	stack s1;
	s1.pop();
	return 0;
}

2. 在类里面定义

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x)
	{
		//.....
	}
	void pop();//成员函数的声明
private:
	int a;
};

int main()
{
	stack s1;
	s1.push(1);
	return 0;
}

5.类的大小计算

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x);
	void pop();//成员函数
	void destory();
	int a;
private:
	int b;//成员变量
};

int main()
{
	stack s1;
	cout << sizeof(s1) << endl;//结果为8
	return 0;
}

<font color=Blue>因为成员函数有3个,成员变量有2个,故只计算了成员变量的大小
</font>

1.只计算成员变量的原因

<font color=Blue>一个类实例化N个对象,每个对象的成员变量都可以存储不同的值,但是调用的函数却是同一个,如果每个对象都放成员函数,而这些成员函数确是一样的,</font><font color=red>就会浪费空间</font>

#include<iostream>
using namespace std;
class stack
{
public:
	void push(int x);
	void pop();//成员函数
	void destory();
	int a;
private:
	int b;//成员变量
};

int main()
{
	stack s1;
	s1.a = 5;
	s1.pop();
	stack s2;
	s2.a = 4;
	s2.pop();
	stack s3;
	s3.a = 3;
	s3.pop();
	return 0;
}

<font color=Blue>通过stack这个类,实例化出s1 、s2、s3 这三个对象
s1.a=5 s2.a =4 s3.a=3</font>
<font color=red>成员变量a每次被赋予都是不同的值
但每次pop这个成员函数的调用是相同的</font>

2. 如何计算一个类的实例化的对象的大小

<font color=Blue>计算成员变量之和,并且考虑内存对齐规则</font>
<font color=red>没有成员变量的大小为1</font>

#include<iostream>
using namespace std;
class A1//类中既有成员函数,又有成员变量
{
public:
	void f1()
	{
		//...
	}
private:
	int a;
	char b;
};
class A2//类中仅有成员函数
{
public:
	void f2()
	{
		//...
	}
};
class A3//类中什么也没有
{
	//...
};
int main()
{
	cout << sizeof(A1) << endl;//8
	cout << sizeof(A2) << endl;//1
	cout << sizeof(A3) << endl;//1
	return 0;
}

<font color=Blue>A1
VS默认对齐数为8
a为int,字节大小为4 , 4<8 即a的对齐数为 4
b为char,字节大小为1, 1<8 即b的对齐数为1
4为最大对齐数,整体结构需为最大对齐数的整数倍
整体结构为5 , 5>4 , 补齐后为 8
即结果为 8</font>

C++类和对象(上)_#include

为什么没有成员变量的大小为1(如A2 A3),而不是0?

<font color=Blue>开一个字节不是为了存数据,而表示对象存在</font>

#include<iostream>
using namespace std;
class A2//类中仅有成员函数
{
public:
	void f2()
	{
		//...
	}
};
int main()
{
	A2 a;
	A2 aa;
	A2 aaa;
	return 0;
}

<font color=Blue>当使用A2类实例化多个对象时,若类的大小为0,没有空间存在,则 &a,&aa,&aaa,就会报错,故选取一个字节是为了表示数据存在</font>

6. this指针

1. 隐藏的this指针

#include<iostream>
using namespace std;
class date
{
public:
	void init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	date d;
	d.init(2022, 12, 11);
	d.print();
	return 0;//2022-12-11

}

<font color=Blue>明明print中没有参数的存在,为何可以出现结果?</font>

C++类和对象(上)_成员函数_02

<font color=Blue>通过隐含的this指针,找到类中的私有 _year _month _day(在类中私有可以直接调用)
从而求出结果</font>

<font color=red>同样的init成员函数也包含隐藏的this指针</font>

C++类和对象(上)_成员变量_03
#include<iostream>
using namespace std;
class A
{
public:
	void printA()
	{
		cout << _a << endl;
	}
	void show()
	{
		cout << "show()" << endl;
	}
private:
	int _a;
};
int main()
{
	A* p = NULL;
	p->printA();//崩溃
	p->show();//正常运行
	return 0;

}

<font color=Blue>因为printA和show函数都在公共代码段中,所以p->printA()和p->show()不会去p所指向的对象中寻找,这里不会崩溃</font>

C++类和对象(上)_成员变量_04

<font color=red>p为NULL,this指针作为p的形参,所以this==NULL,NULL->a程序就会崩溃</font>

C++类和对象(上)_成员变量_05

<font color=red>虽然this==NULL,但是输出并没有用到this指针,没有访问NULL
</font>


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK