嵌入式开发学习 C++:day04

C++:第04天笔记

友元

生活中的你家有客厅(public),有卧室(private)

客厅所有来的客人都可以进去,但是你的卧室是私有的,也就是说只有你自己可以进入。

但是呢,你也可以允许客人中你的朋友进去。

在程序里,有些私有成员,也想让类特殊的一些函数或者类进行访问,就需要用到友元技术。

友元的目的是让一个函数或者类访问另一个类中的私有成员。

友元的关键字为friend

友元的三种实现:

全局函数做友元

做友元

成员函数做友元

全局函数做友元

/*************************************************************************
  > File Name:    demo01.cpp
  > Author:       小刘
  > Description:  友元
  > Created Time: 2025-08-21 09:45:41
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// 创建Buiding类
class Building
{
// 告诉编译器 goodfriend 全局函数 是building类的好朋友,可以访问类中的私有成员
	friend void goodfriend(Building* building);
public :
	// 无参构造,此时会覆盖掉默认的无参
	Building()
	{
		// 复制
		this->sittingRoom = "客厅";
		this->bedroom     = "卧室";
	}
public :
	string sittingRoom; // 客厅  公共成员
private :
	string bedroom;     // 卧室  私有成员
};

// 准备一个全局函数,用来做友元,实现对类中私有成员 bedroom的访问
void goodfriend(Building* building)
{
	cout << "好朋友正在造访:" << building->sittingRoom << endl; //客厅,正在访问
	cout << "好朋友正在造访:" << building->bedroom     << endl; //卧室,需要借助友元访问
}

int main(int argc ,char *argv[])
{
	Building b;
	goodfriend(&b);
	
	system("pause");
	return 0;
}

类做友元

/*************************************************************************
  > File Name:    demo01.cpp
  > Author:       小刘
  > Description:  类做友元
  > Created Time: 2025-08-21 09:59:06
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// 创建Building 类
class Building
{
	// 告诉编译器 GoodFrien类是Building类的好朋友,可以访问到Building类中的私有成员
	friend class GoodFriend; // frien 函数声明
public:
	Building();
public:
	string sittingRoom; // 客厅  公共成员
private:
	string bedroom;      // 卧室  公共成员
};

// 定义Building类的构造函数
Building::Building()
{
	this->sittingRoom = "客厅";
	this->bedroom      = "卧室";
}

// 创建类GoodFriend
class GoodFriend
{
public :
	GoodFriend()
	{
		this->building= new Building(); // 此时就不在栈区,在自由存储区,大概率在堆区
	}
	
	// 访问好朋友Building的私有成员
	// void visit(); // 将成员函数的实现写到类的外部
	void  visit()
	{
		cout << "好朋友正在造访:" << building->sittingRoom << endl; //客厅,正在访问
		cout << "好朋友正在造访:" << building->bedroom     << endl; //卧室,需要借助友元访问
	}
private:
	Building *building;
};

//void GoodFriend::visit()
//{
//	cout << "好朋友正在造访:" << building->sittingRoom << endl; //客厅,正在访问
//	cout << "好朋友正在造访:" << building->bedroom     << endl; //卧室,需要借助友元访问
//}

int main(int argc ,char *argv[])
{
	GoodFriend gf;
	gf.visit();
	system("pause");
	return 0;
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

成员函数做友元

/*************************************************************************
  > File Name:    demo03.cpp
  > Author:       小刘
  > Description:  友元:成员函数做友元
  > Created Time: 2025-08-21 09:48:50
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// 类的声明:主要解决类的嵌套
class Building;

// 类的定义
// 创建友元类
class GoodFriend
{
public:
	GoodFriend();
	void visit();
private:
	Building *building;
};

// 创建Building类
class Building
{
	// 告诉编译器GoodFriend类中某个函数(visit)作为Building类的好朋友,可以访问Building中的私有成员
	friend void GoodFriend::visit();
public:
	// 无参构造函数
	Building()
	{
		// 初始化(所谓的初始化,就是给类对象的成员变量赋值)
		this->sittingroom = "客厅";
		this->bedroom     = "卧室";
	}
public:
	string sittingroom; //客厅  公共成员
private:
	string bedroom;     //卧室  私有成员
};

GoodFriend::GoodFriend()
{
	this ->building = new Building();
}

void GoodFriend::visit()
{
	cout << "好朋友正在访问:" << building->sittingroom << endl;
	cout << "好朋友指针访问:" << building->bedroom << endl;
}

int main(int argc ,char *argv[])
{
	GoodFriend gf;
	gf.visit();
	system("pause");
	return 0;
}

如果涉及到类的嵌套访问,比如成员函数做友元的时候。我们建议将成员函数定义到类的外部,同时需要给类加上声明。

运算符重载[了解]

运算符重载的概念:对已有的运算符重新定义,赋予其另一种功能,以适应不同的数据类型。

加号运算符

**作用:**实现两个自定义数据类型相加运算

示例:

/*************************************************************************
  > File Name:    demo04.cpp
  > Author:       小刘
  > Description:  运算符重载
  > Created Time: 2025-08-21 10:53:41
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Person
{
public:
	Person(){};
	Person(int a,int b)
	{
		this-> a = a;
		this-> b = b;
	}
	
	// 成员函数实现 + 号运算符重载
	Person operator + (const Person& p)
	{
		Person temp;
		temp.a = this->a + p.a;
		temp.b = this->b + p.b;
		return temp;
	}
public :
	int a;
	int b;
};

Person operator + (const Person&p2, int val)
{
	Person temp;
	temp.a = p2.a + val;
	temp.b = p2.b + val;
	return temp;
}

// 全局函数实现 + 号运算符重载
Person operator + (const Person& p1,const Person& p2)
{
	Person temp;
	temp.a = p2.a + p1.a;
	temp.b = p2.b + p1.b;
	return temp;
}

int main(int argc ,char *argv[])
{
	Person p1(10,20);
	Person p2(20,20);
	
	// 测试成员函数方式
	Person p3 = p1 +p2; // 相当于 p2.operator + (p1)
	cout << "a = " << p3.a << ",b = " << p3.b << endl;
	
	Person p4 = p3 + 10; // 相当于 operator + (p3,10);
	cout << "a = " << p4.a << ",b = " << p4.b << endl;
	system("pause");
	return 0;
}

总结1:对于内置的数据类型的表达式的运算符是不可能改变的

总结2:不要滥用运算符重载

左移运算符重载

作用:可以输出自定义数据类型

/*************************************************************************
  > File Name:    demo10.cpp
  > Author:       小刘
  > Description:  运算符重载
  > Created Time: 2025-08-30 17:11:01
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

class Person
{
	friend ostream& operator<< (ostream& out,Person& p);
public:
	Person(int a,int b)
	{
		this->m_A = a;
		this->m_B = b;
	}
	
	// 成员函数 实现不了 p << cout 不是我们想要的效果
	// void operator(Person& p){}
private:
	int m_A;
	int m_B;
};

// 全局函数实现左移重载
// ostream对象只能有一个
ostream& operator<< (ostream& out,Person& p)
{
	cout << "a = " << p.m_A << ",b = " << p.m_B;
	return out; // 临时存储输出信息
}

void test01()
{
	Person p1(10,20);
	
	cout << p1 << "hello world" << endl;
}

int main(int argc ,char *argv[])
{
	test01();
	
	system("pause");
	
	return 0;
}

总结:重载左移运算符配合友元以实现输出自定义数据类型

递增运算符重载

作用:通过重载递增运算符,实现自己的数据类型

/*************************************************************************
  > File Name:    demo12.cpp
  > Author:       小刘
  > Description:  运算符重载
  > Created Time: 2025-08-30 17:44:35
 ************************************************************************/
#include <iostream>
//#include <string>
//#include <iomanip>

using namespace std;

class MyInteger
{
	friend ostream& operator << (ostream& out,MyInteger myint);
public:
	MyInteger(){m_num = 0;}
	// 前置++
	MyInteger& operator++()
	{
		// 先++
		m_num++;
		// 再返回
		return *this;
	}
	
	// 后置++
	MyInteger operator++(int)
	{
		// 先返回
		MyInteger temp = *this; // 记录当前本身的值,然后让本身的值加以,大师返回值是以前的值,达到先返回后++
		m_num++;
		return temp;
	}
private:
	int m_num;
};

ostream& operator << (ostream& out,MyInteger myint)
{
	out << myint.m_num;
	return out;
}

void test01()
{
	MyInteger myint;
	cout << ++myint << endl;
	cout << myint << endl;
}

void test02()
{
	MyInteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main(int argc ,char *argv[])
{
	test01();
	test02();
	system("pause");
	return 0;
}

总结:前置递增返回引用,后置递增返回值

赋值运算符重载

C++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)
  2. 默认析构函数(无参,函数体为空)
  3. 默认拷贝构造函数,对属性进行值拷贝
  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

示例:

/*************************************************************************
  > File Name:    demo13.cpp
  > Author:       小刘
  > Description:  
  > Created Time: 2025-08-30 17:57:34
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Person
{
public:
	Person(int age)
	{
		// 将年龄数据开辟到堆区
		m_Age = new int(age);
	}
	
	// 重载赋值运算符
	Person& operator = (Person &p)
	{
		if(m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		// 编译器提供的代码是浅拷贝
		// m_Age = p.m_Age;
		
		// 提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);
		
		// 返回自身
		return *this;
	}
	
	~Person()
	{
		if(m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
	}
	

	int *m_Age;
};

void test01()
{
	Person p1(18);
	
	Person p2(20);
	
	Person p3(30);
	
	p2 = p1; // 赋值操作
	
	cout << "p1的年龄为:" << *p1.m_Age << endl;
	cout << "p2的年龄为:" << *p2.m_Age << endl;
	cout << "p3的年龄为:" << *p3.m_Age << endl;
}

int main(int argc ,char *argv[])
{
	test01();
	system("pause");
	return 0;
}

关系运算符重载

**作用:**重载关系运算符,可以让两个自定义类型对象进行比较操作

示例:

/*************************************************************************
  > File Name:    demo14.cpp
  > Author:       小刘
  > Description:  运算符重载
  > Created Time: 2025-08-30 18:22:14
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Person
{
public:
	Person(string name,int age)
	{
		this->m_name = name;
		this->m_Age  = age;
	}
	// == 重载函数实现
	bool operator == (Person& p)
	{
		if(this->m_name == p.m_name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	// != 重载函数实现
	bool operator != (Person & p)
	{
		if(this -> m_name == p.m_name && this -> m_Age == p.m_Age)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	string m_name;
	int m_Age;
};

void test01()
{
	Person a("张三",18);
	Person b("张三",19);
	
	if(a == b)
	{
		cout << "a和b相等" << endl;
	}
	else
	{
		cout << "a和b不相等" << endl;
	}
	
	if(a != b)
	{
		cout << "a和b不相等" << endl;
	}
	else
	{
		cout << "a和b相等" << endl;
	}
}

int main(int argc ,char *argv[])
{
	test01();
	system("pause");
	return 0;
}

函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活

示例:

/*************************************************************************
  > File Name:    demo05.cpp
  > Author:       小刘
  > Description:  函数调用运算符
  > Created Time: 2025-08-21 14:10:57
 ************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class MyPrint
{
public:
	// ()运算符重载
	void operator()(string text)
	{
		cout << text << endl;
	}
};

void test01()
{
	// 重载的()操作符,也称为仿函数
	MyPrint myFunc;
	myFunc("hello woreld");
}

class MyAdd
{
public:
	int operator()(int num1,int num2)
	{
		return num1 + num2;
	}
};
void test02()
{
	MyAdd add;
	int ret = add(10,20);
	cout << "ret = " << ret << endl;
	
	// 匿名对象调用
	cout << "MyAdd()(100,100) = " << MyAdd()(100,100) << endl;
}

int main(int argc ,char *argv[])
{
	test01();
	test02();
	
	system("pause");
	return 0;
}

继承

继承是面向对象三大特征之一

有些类与类之间存在特殊的关系,例如下图:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

我们发现,定义这些类时,下一级的成员除了拥有上一级共性(特征+行为),还有自己的特征(特征+ 行为)。

这个时候我们就可以考虑利用继承技术,减少重复代码。

继承的基本语法

例如我们看到很多网站中,都有公共的头部,公共的尾部,甚至公共的左侧列表,只有中心内容不同。

接下来我们分别利用普通写法和继承写法来实现网页的内容,看一下继承存在的意义以及好处。

普通继承

/*************************************************************************
  > File Name:    demo06.cpp
  > Author:       小刘
  > Description:  继承-普通实现
  > Created Time: 2025-08-21 14:13:01
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

// Java方向对应的界面
class Java
{
public:
	// 头部
	void header()
	{
		cout << "首页、公开课、登录、注册……(公共头部) " << endl;
	}
	// 底部
	void footer()
	{
		cout << "帮助中心、交流合作、版权信息、站内导航……(公共底部)" << endl;
	}
	
	// 左侧菜单
	void left()
	{
		cout << "java、veb、C++(公共分类菜单)" <<endl;
	}
	
	// 右侧菜单
	void content()
	{
		cout << "java大纲、Java学习视频、Java讲师介绍" <<endl;
	}
};

// web 方向对应的界面
class Web
{
public:
	// 头部
	void header()
	{
		cout << "首页、公开课、登录、注册……(公共头部) " << endl;
	}
	// 底部
	void footer()
	{
		cout << "帮助中心、交流合作、版权信息、站内导航……(公共底部)" << endl;
	}
	
	// 左侧菜单
	void left()
	{
		cout << "java、veb、C++(公共分类菜单)" <<endl;
	}
	// 右侧菜单
	void content()
	{
		cout << "Web大纲、Web学习视频、Web讲师介绍" <<endl;
	}
	
};

// C++方向对应的界面
class CPP
{
public:
	// 头部
	void header()
	{
		cout << "首页、公开课、登录、注册……(公共头部) " << endl;
	}
	// 底部
	void footer()
	{
		cout << "帮助中心、交流合作、版权信息、站内导航……(公共底部)" << endl;
	}
	
	// 左侧菜单
	void left()
	{
		cout << "java、veb、C++(公共分类菜单)" <<endl;
	}
	// 
	void content()
	{
		cout << "C++大纲、C++学习视频、C++讲师介绍" <<endl;
	}
};

int main(int argc ,char *argv[])
{
	// Java方向
	cout << "Java方向:" << endl;
	Java ja;
	ja.header();
	ja.footer();
	ja.left();
	ja.content();
	cout << "------------------" << endl;
	
	// Web方向
	cout << "Web方向:" << endl;
	Web we;
	we.header();
	we.footer();
	we.left();
	we.content();
	cout << "------------------" << endl;
	
	// C++方向
	cout << "C++方向:" << endl;
	CPP cp;
	cp.header();
	cp.footer();
	cp.left();
	cp.content();
	cout << "------------------" << endl;
	return 0;
}

继承实现:

/*************************************************************************
  > File Name:    demo07.cpp
  > Author:       小刘
  > Description:  
  > Created Time: 2025-08-21 14:37:04
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

// 公共类界面(存放公共的成员)
class Basepage
{
public:
	// 头部
	void header()
	{
		cout << "首页、公开课、登录、注册……(公共头部) " << endl;
	}
	// 底部
	void footer()
	{
		cout << "帮助中心、交流合作、版权信息、站内导航……(公共底部)" << endl;
	}
	
	// 左侧菜单
	void left()
	{
		cout << "java、veb、C++(公共分类菜单)" <<endl;
	}
};


// Java子类 继承父类 说明:继承父类后,就可以访问父类的非私有成员了
class Java : public Basepage
{
public :
	// 右侧菜单
	void content()
	{
		cout << "java大纲、Java学习视频、Java讲师介绍" <<endl;
	}
};

class Web : public Basepage
{
public :
	// 右侧菜单
	void content()
	{
		cout << "Web大纲、Web学习视频、Web讲师介绍" <<endl;
	}
};

class CPP : public Basepage
{
public :
	// 右侧菜单
	void content()
	{
		cout << "C++大纲、C++学习视频、C++讲师介绍" <<endl;
	}
};

int main(int argc ,char *argv[])
{
	// Java方向
	cout << "Java方向:" << endl;
	Java ja;
	ja.header();
	ja.footer();
	ja.left();
	ja.content();
	cout << "------------------" << endl;
	
	// Web方向
	cout << "Web方向:" << endl;
	Web we;
	we.header();
	we.footer();
	we.left();
	we.content();
	cout << "------------------" << endl;
	
	// C++方向
	cout << "C++方向:" << endl;
	CPP cp;
	cp.header();
	cp.footer();
	cp.left();
	cp.content();
	cout << "------------------" << endl;
	return 0;
}

总结

继承的好处:可以减少重复代码

class A : public B;

说明:

①A类称为子类 或者 派生类

②B类称为父类 或者 基类

派生类中的成员,包含两大部分:

类是从基类继承过来的,一类是自己增加的成员。

从基类继承过过来的表现其共性,而新增的成员体现了其个性。

继承方式

继承的语法:

class 子类 :继承方式 父类{..}

继承方式一共有三种:

  • 公共继承【:public】
  • 保护继承【:protected】
  • 私有继承【:private】

示例:

/*************************************************************************
  > File Name:    demo08.cpp
  > Author:       小刘
  > Description:  继承方式
  > Created Time: 2025-08-21 14:28:02
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

// 父类
class Base1
{
public:
	int m_A = 10;
protected:
	int m_B = 20;
private:
	int m_C = 30;
};

// 子类 公共继承
class Son1 : public Base1
{
public:
	// 子类成员 访问父类成员
	void fun()
	{
		m_A;  // 可访问 public 权限   
		m_B;  // 可访问 protected 权限
		// m_c; // 不可访问 private 权限
	}
};

// 其他类成员访问
void MyClass()
{
	Son1 son1;
	son1.m_A; // 可访问     public权限,同一类中、具有父子关系的类、其他类都可以访问
	//son1.m_B; // 不可访问 protected:权限,同一类中、具有父子关系的类
	//son1.m_C; // 不可访问 private : 权限,同一类中
}

// -----------------------------------------------

// 保护成员访问
class Son2 : protected Base1
{
public:
	// 子类成员 访问父类成员
	void fun()
	{
		//m_A;  // 可访问 public 权限   
		//m_B;  // 可访问 protected 权限
		// m_c; // 不可访问 private 权限
	}
};

// 其他类成员访问
void MyClass2()
{
	Son2 son2;
	//son2.m_A; // 不可访问 public权限,同一类中、具有父子关系的类、其他类都可以访问
	//son1.m_B; // 不可访问 protected:权限,同一类中、具有父子关系的类
	//son1.m_C; // 不可访问 private : 权限,同一类中
}

//--------------------------------------------------------
//---------------------------------------------------
// 私有成员访问
class Son3 : protected Base1
{
public:
	// 子类成员 访问父类成员
	void fun()
	{
		m_A;  // 可访问 public 权限   
		m_B;  // 可访问 protected 权限
		// m_c; // 不可访问 private 权限
	}
};

// 其他类成员访问
void MyClass3()
{
	Son3 son3;
	//son2.m_A; // 不可访问 public权限,同一类中、具有父子关系的类、其他类都可以访问
	//son1.m_B; // 不可访问 protected:权限,同一类中、具有父子关系的类
	//son1.m_C; // 不可访问 private : 权限,同一类中
}

int main(int argc ,char *argv[])
{
	
	return 0;
}

继承中构造和析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数

**问题:**父类和子类的构造和析构顺序是谁先谁后

/*************************************************************************
  > File Name:    demo09.cpp
  > Author:       小刘
  > Description:  继承 构造和析构顺序
  > Created Time: 2025-08-21 14:59:26
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

// 定义父类
class Base
{
public:
	Base()
	{
		cout << "Base 构造函数!" << endl;
	}
	
	~Base()
	{
		cout << "Base 析构函数!" << endl;
	}
};

// 定义子类
class Son : public Base
{
public:
	Son ()
	{
		cout << "Son 构造函数!" << endl;
	}
	
	~Son()
	{
		cout << "Son 析构函数!" << endl;
	}
};

void test01()
{
	Son s;
}

int main(int argc ,char *argv[])
{
	test01(); // Base构造函数 Son构造函数 Son析构函数 Base析构函数
	
	system("pause");
	return 0;
}

总结:继承中先调用父类析构,再调用子类构造函数,析构顺序和构造相反。

继承同名成员处理方式

**问题:**当子类与父类出现同名成员,如果通过子类对象,访问到子类或父类的同名数据?

  • 访问子类同名成员:直接访问即可
  • 访问父类同名成员:作用域

示例:

/*************************************************************************
  > File Name:    demo10.cpp
  > Author:       小刘
  > Description:  
  > Created Time: 2025-08-21 14:14:58
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

// 定义父类
class Base
{
public:
	Base()
	{
		m_A = 100;// 父类同名成员成员变量 200
	}
	void func()
	{
		cout << "Base - func() 调用" <<endl;
	}
	void func(int a)
	{
		cout << "Base - func(int a) 调用" <<endl;
	}
public:
	int m_A;
};

class Son : public Base
{
public:
	Son()
	{
		this -> m_A = 200; // 不涉及命名冲突的情况下 this-> 写不写无所谓
	}
	
	// 当子类与父类拥有同名函数,子类会隐藏父类所有版本的同名成员函数
	// 如果访问父类中被隐藏的同名成员函数,需要加父类作用域
	void func()
	{
		cout << "Son - func() 调用" << endl;
	}
public:
	int m_A; // 子类同名成员成员变量 200
};

void test01()
{
	Son s;
	
	// 默认访问子类同名成员
	cout << "Son下的m_A = " << s.m_A << endl;
	// 访问父类同名成员
	cout << "Base 下的m_A = " <<s.Base :: m_A << endl;
	s.func();
	s.Base::func();
	s.Base::func(10);
}

int main(int argc ,char *argv[])
{
	test01();
	
	system("pause");
	return 0;
}

总结

①子类对象可以直接访问到子类中同名成员

②子类对象加作用域可以访问到父类同名成员

③当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类同名成员函数。

子类调用父类构造进行初始化

**问题:**继承中我们如何调用父类构造完成初始化?比如部分成员变量在父类,部分成员变量在子类。

子类构造时会调用父类构造,有以下三种情况。

  • 隐式调用(默认)
  • 显示调用父类构造函数
  • 多继承情况(后续讲)

示例:

/*************************************************************************
  > File Name:    demo11.cpp
  > Author:       小刘
  > Description:  
  > Created Time: 2025-08-21 16:59:37
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

class Base
{
public:
	Base(){}
	Base(int a,int b,int c)
	{
		m_A = a;
		m_B = b;
		m_C = c;
	}
public:
	int m_A;
	int m_B;
	int m_C;
};

// 定义子类
class Son : public Base
{
public:
	Son(){}
	Son(int a,int b,int c,int d) : Base(a,b,c) // 显示调用父类构造函数 Base(int a,int b,int c);
	{
		m_D = d;
	}
	
	void printAll()
	{
		cout << "m_A = " << m_A << endl;
		cout << "m_B = " << m_B << endl;
		cout << "m_C = " << m_C << endl;
		cout << "m_D = " << m_D << endl;
	}
public:
	int m_D;
};

void test01()
{
	Son s(11,12,13,14);
	s.printAll();
}

int main(int argc ,char *argv[])
{
	test01();
	system("pause");
	return 0;
}

");
return 0;
}


**总结**

①子类对象可以直接访问到子类中同名成员

②子类对象加作用域可以访问到父类同名成员

③当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类同名成员函数。

### 子类调用父类构造进行初始化

**问题:**继承中我们如何调用父类构造完成初始化?比如部分成员变量在父类,部分成员变量在子类。

子类构造时会调用父类构造,有以下三种情况。

- 隐式调用(默认)
- 显示调用父类构造函数
- 多继承情况(后续讲)

**示例:**

```c
/*************************************************************************
  > File Name:    demo11.cpp
  > Author:       小刘
  > Description:  
  > Created Time: 2025-08-21 16:59:37
 ************************************************************************/
#include <iostream>
#include <string>
#include <string>

using namespace std;

class Base
{
public:
	Base(){}
	Base(int a,int b,int c)
	{
		m_A = a;
		m_B = b;
		m_C = c;
	}
public:
	int m_A;
	int m_B;
	int m_C;
};

// 定义子类
class Son : public Base
{
public:
	Son(){}
	Son(int a,int b,int c,int d) : Base(a,b,c) // 显示调用父类构造函数 Base(int a,int b,int c);
	{
		m_D = d;
	}
	
	void printAll()
	{
		cout << "m_A = " << m_A << endl;
		cout << "m_B = " << m_B << endl;
		cout << "m_C = " << m_C << endl;
		cout << "m_D = " << m_D << endl;
	}
public:
	int m_D;
};

void test01()
{
	Son s(11,12,13,14);
	s.printAll();
}

int main(int argc ,char *argv[])
{
	test01();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值