虫儿从零学c++ 2:类的基础相关知识点复习1

本文通过实际代码演示了C++中类的定义、构造函数、析构函数、拷贝构造函数、运算符重载等核心概念,并展示了如何实现友元函数及静态成员变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上篇学习日志的文章不知道咋恢复,无所谓啦,今天忙了一下午,对着网课的资料敲了一下午的代码
大致内容参考的是下面相关的内容
有点粗糙

1.参考的网课大纲

2.3 函数重载
函数重载
2.4 函数参数的缺省值与追踪返回类型的函数
函数参数的缺省值与追踪返回类型的函数
2.5 类的定义
类的定义
2.6 类成员的访问权限与友元
类成员的访问权限与友元
3.1 构造函数析构函数
构造函数析构函数
3.2 赋值运算符重载
赋值运算符重载
3.3 流运算符重载
流运算符重载
3.4 函数运算符重载
函数运算符重载
下面是自己下午写的函数代码

关于友元

#include<iostream>
using namespace std;
class A
{
public:
	A(int id);
	~A();
	friend void print(A obj);


private:
	int id;

};

A::A(int id)
{
	this->id = id;
}


A::~A()
{
	this->id = 0;
}
void print(A obj)
{
	cout << obj.id<< endl;
}
int main()
{
	A a1(16), a2(17);
	print(a1);
	print(a2);
	return 0;
}

关于class 构造函数 析构函数 拷贝构造 默认构造 运算符重载 静态成员

简单的写了下相关代码 ,有点粗糙,参考的是网课相关资料 照葫芦画瓢敲了一遍

#include<iostream>
using namespace std;
class Student
{
public:
	int ID;
	int AGE;
	bool SEX;
	static int count;
	static int howmany()
	{
		return count;
	}
	Student(int id, int age, bool sex) :ID(id), AGE(age), SEX(sex) {
		cout << hex << this << "号对象的Student(int id, int age, bool sex) :ID(id), AGE(age), SEX(sex)被调用" << endl;
		count++;
		
	}//初始化形参列表构造
	Student(const Student& stu);//拷贝构造函数
	Student(int year, int num, int age, bool sex):ID(year+num), AGE(age), SEX(sex) {
		count++;
		cout << hex << this << "号对象的Student(int year, int num, int age, bool sex):ID(year+num), AGE(age), SEX(sex)被调用" << endl;
	};//初始化形参列表构造 重载
	Student();//无参造函数
	Student GetTmpStu();//取得临时对象
	Student& operator = (const Student& right)//赋值运算符重载
	{
		if (this != &right)
		{
			cout << "不是同一对象" << endl;
			this->ID = right.ID;
			this->AGE = right.AGE;
			this->SEX = right.SEX;
		}
		else
		{
			cout << "是同一对象" << endl;

		}
		cout << hex << this << "号对象的Student& operator = (const Student& right)被调用" << endl;
		return *this;
	}
	Student operator()(Student& s1, Student& s2);//函数运算符重载
	void Init(int id, int age, bool sex);
	void Print()
	{
		cout << "该学生是" <<dec<<this->ID << "号" << "年纪是" << this->AGE << "性别是";
		if (this->SEX)
			cout << "男" << endl;
		else
			cout << "女" << endl;
		cout << hex << this << "号对象的:Print()被调用" << endl;
	}

	//关于函数的输入输出流运算符重载
	friend istream& operator>>(istream &in, Student& dst);
	friend ostream& operator<<(ostream &out, const Student& src);
	~Student();
};
int Student::count = 0;
Student::Student(const Student& stu)
{
	count++;
	this->ID = stu.ID;
	this->AGE = stu.AGE;
	this->SEX = stu.SEX;
	cout << hex << this << "号对象的:Student(const Student& stu)被调用" << endl;
}



Student::Student()
{
	count++;
	cout << hex << this << "号对象的:Student()被调用" << endl;
	//有风险 函数内部的值未赋予初值 会报警告
}

Student Student::GetTmpStu()
{
	Student tmp;
	tmp.Print();
	cout << hex << this << "号对象的:GetTmpStu()被调用" << endl;
	return tmp;
}

Student Student::operator()(Student& s1, Student& s2)
{
	Student tmp = this->GetTmpStu();
	tmp.AGE = s1.AGE + s2.AGE;
	tmp.ID = s1.ID + s2.ID;
	tmp.SEX = s1.SEX && s2.SEX;
	cout << hex << this << "Student::operator()(Student& s1, Student& s2)被调用" << endl;
	return  tmp;
}

void Student::Init(int id, int age, bool sex)
{
	this->ID = id;
	this->AGE = age;
	this->SEX = sex;
	cout << hex << this << "号对象的:Init(int id, int age, bool sex)被调用" << endl;
}

Student::~Student()
{
	count--;
	cout<<"内存地址:" << hex << this << "  " <<dec <<this->ID <<"  "<<
		"号"<<"年纪是"<<this->AGE<<"学生对象被销毁" << endl;
	cout << "count" << count << endl;
}
int main()
{
	cout << "执行Student s1" << endl;
	Student s1;
	cout << "执行Student s2(1, 27, true);" << endl;
	Student s2(1, 27, true);
	cout << "执行Student s3;" << endl;
	Student s3;
	cout << "执行s3.Init(2, 28, true);" << endl;
	s3.Init(2, 28, true);
	cout << "执行Student s4(s3);" << endl;
	Student s4(s3);
	cout << "执行Student s5(2022, 25647, 28, false);" << endl;
	Student s5(2022, 25647, 28, false);
	cout << "执行Student s6(3, 29, false);" << endl;
	Student s6(3, 29, false);
	cout << "执行Student stmp1 = s2.GetTmpStu();" << endl;
	Student stmp1 = s2.GetTmpStu();
	//Student stmp2=Student::GetTmpStu();
	//s6 = s3;
	//cout << "下面执行s1-s6和stmp1的打印函数" << endl;
	cout << "打印当前s1的信息" << endl;
	s1.Print();
	cout << "打印当前s2的信息" << endl;
	s2.Print();
	cout << "打印当前s3的信息" << endl;
	s3.Print();
	cout << "打印当前s4的信息" << endl;
	s4.Print();
	cout << "打印当前s5的信息" << endl;
	s5.Print();
	cout << "打印当前s6的信息" << endl;
	s6.Print();
	cout << "打印当前stmp1的信息" << endl;
	stmp1.Print();
	cout << "对s6对象使用赋值运算符函数" << endl;
	s6 = s3;
	cout << "执行s6=s3后" << endl;
	cout << "打印当前s6的信息" << endl;
	s6.Print();
	//下面测试流运算符重载 cin cout
	Student s7(5, 32, true);
	cout << s7 << endl;
	Student s7Test;
	cin >> s7Test;
	cout << s7Test << endl;
	
	//下面执行函数运算符的重载
	Student s8;
	s8 = s8(s5, s6);
	cout << s8 << endl;
	cout << s8.howmany() << endl;
	
	
	
	
	return 0;
}

istream& operator>>(istream &in, Student& dst)
{
	// TODO: 在此处插入 return 语句
	in >> dst.ID >> dst.AGE >> dst.SEX;
	return in;
}
//这两个流运算符重载函数特别要注意
//一开始我写函数声明的是istream& operator>>(istream in, Student& dst) 和ostream& operator<<(ostream out, const Student& src)
//前面没加引用标识符会报错
//编译器会提示无法引用 函数 “std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<char, //std::char_traits> &) [其中 _CharT=char, _Traits=std::char_traits]” (已隐式声明) – 它是已删除的函数
//但是在重载输出流符号的全局函数的返回值加上引用&就可以了,之前的错误可能是因为在重载输出流函数执行完毕返回的输出流类型被释放发生的错误。在加入引用后就解决掉了
ostream& operator<<(ostream &out, const Student& src)
{
	// TODO: 在此处插入 return 语句
	out <<hex <<&src <<"对象"<<"id是" << dec<<src.ID <<"年纪是" <<src.AGE <<"性别是"<< src.SEX << endl;
	return out;
}

相关运行结果参考如下
执行Student s1
008FFDC8号对象的:Student()被调用
执行Student s2(1, 27, true);
008FFDB4号对象的Student(int id, int age, bool sex) :ID(id), AGE(age), SEX(sex)被调用
执行Student s3;
008FFDA0号对象的:Student()被调用
执行s3.Init(2, 28, true);
008FFDA0号对象的:Init(int id, int age, bool sex)被调用
执行Student s4(s3);
008FFD8C号对象的:Student(const Student& stu)被调用
执行Student s5(2022, 25647, 28, false);
008FFD78号对象的Student(int year, int num, int age, bool sex):ID(year+num), AGE(age), SEX(sex)被调用
执行Student s6(3, 29, false);
008FFD64号对象的Student(int id, int age, bool sex) :ID(id), AGE(age), SEX(sex)被调用
执行Student stmp1 = s2.GetTmpStu();
008FFBE0号对象的:Student()被调用
该学生是-858993460号年纪是-858993460性别是男
008FFBE0号对象的:Print()被调用
008FFDB4号对象的:GetTmpStu()被调用
008FFD50号对象的:Student(const Student& stu)被调用
内存地址:008FFBE0 -858993460 号年纪是-858993460学生对象被销毁
count7
打印当前s1的信息
该学生是-858993460号年纪是-858993460性别是男
008FFDC8号对象的:Print()被调用
打印当前s2的信息
该学生是1号年纪是27性别是男
008FFDB4号对象的:Print()被调用
打印当前s3的信息
该学生是2号年纪是28性别是男
008FFDA0号对象的:Print()被调用
打印当前s4的信息
该学生是2号年纪是28性别是男
008FFD8C号对象的:Print()被调用
打印当前s5的信息
该学生是27669号年纪是28性别是女
008FFD78号对象的:Print()被调用
打印当前s6的信息
该学生是3号年纪是29性别是女
008FFD64号对象的:Print()被调用
打印当前stmp1的信息
该学生是-858993460号年纪是-858993460性别是男
008FFD50号对象的:Print()被调用
对s6对象使用赋值运算符函数
不是同一对象
008FFD64号对象的Student& operator = (const Student& right)被调用
执行s6=s3后
打印当前s6的信息
该学生是2号年纪是28性别是男
008FFD64号对象的:Print()被调用
008FFD3C号对象的Student(int id, int age, bool sex) :ID(id), AGE(age), SEX(sex)被调用
008FFD3C对象id是5年纪是32性别是1

008FFD28号对象的:Student()被调用
9 26 1
008FFD28对象id是9年纪是26性别是1

008FFD14号对象的:Student()被调用
008FFABC号对象的:Student()被调用
该学生是-858993460号年纪是-858993460性别是男
008FFABC号对象的:Print()被调用
008FFD14号对象的:GetTmpStu()被调用
008FFBD8号对象的:Student(const Student& stu)被调用
内存地址:008FFABC -858993460 号年纪是-858993460学生对象被销毁
count11
008FFD14Student::operator()(Student& s1, Student& s2)被调用
008FFC40号对象的:Student(const Student& stu)被调用
内存地址:008FFBD8 27671 号年纪是56学生对象被销毁
count11
不是同一对象
008FFD14号对象的Student& operator = (const Student& right)被调用
内存地址:008FFC40 27671 号年纪是56学生对象被销毁
count10
008FFD14对象id是27671年纪是56性别是0

10
内存地址:008FFD14 27671 号年纪是56学生对象被销毁
count9
内存地址:008FFD28 9 号年纪是26学生对象被销毁
count8
内存地址:008FFD3C 5 号年纪是32学生对象被销毁
count7
内存地址:008FFD50 -858993460 号年纪是-858993460学生对象被销毁
count6
内存地址:008FFD64 2 号年纪是28学生对象被销毁
count5
内存地址:008FFD78 27669 号年纪是28学生对象被销毁
count4
内存地址:008FFD8C 2 号年纪是28学生对象被销毁
count3
内存地址:008FFDA0 2 号年纪是28学生对象被销毁
count2
内存地址:008FFDB4 1 号年纪是27学生对象被销毁
count1
内存地址:008FFDC8 -858993460 号年纪是-858993460学生对象被销毁
count0

最后

用的是VS2019可能不同VS或许会有不同,今天就写这么多把,明天再敲下代码记录下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值