黑马程序员:C++核心编程——4(1)

//空指针访问成员函数
class Person {
public:
void ShowClassName() {
cout << “我是Person类!” << endl;
}
void ShowPerson() {
if (this == NULL) {
return;
}
cout << mAge << endl;
}
public:
int mAge;
};
void test01()
{
Person * p = NULL;
p->ShowClassName(); //空指针,可以调用成员函数
p->ShowPerson(); //但是如果成员函数中用到了this指针,就不可以了
}
int main() {
test01();
system(“pause”);
return 0;
}


### 4.**const****修饰成员函数**


**常函数**:


* 成员函数后加const后我们称为这个函数为**常函数**
* 常函数内不可以修改成员属性,不做赋值和改值操作
* 成员属性声明时加关键字**mutable**后,在常函数中依然可以修改



**常对象:** 

* 声明对象前加const称该对象为常对象
* **常对象只能调用常函数(因为普通成员函数可以修改成员属性,不能让常对象间接修改成员属性)**


**常对象和常函数是配套的!!!!!**



class Person {
public:
Person() {
m_A = 0;
m_B = 0;
}
//this指针的本质是一个指针常量,指针的指向不可修改
//如果想让指针指向的值也不可以修改,需要声明常函数
void ShowPerson() const {
//本质的意思就是const Type* const this; 但前面这个const思来想去最后加在了{}前面
//this = NULL; //不能修改指针的指向 Person* const this;
//this->mA = 100; //但是this指针指向的对象的数据是可以修改的
//const修饰成员函数,表示指针指向的内存空间的数据不能修改,除了mutable修饰的变量
this->m_B = 100;
}
void MyFunc() const {
//mA = 10000;
}
public:
int m_A;
mutable int m_B; //可修改 可变的
};
//const修饰对象 常对象
void test01() {
const Person person; //常量对象
cout << person.m_A << endl;
//person.mA = 100; //常对象不能修改成员变量的值,但是可以访问
person.m_B = 100; //但是常对象可以修改mutable修饰成员变量
//常对象访问成员函数
person.MyFunc(); //常对象不能调用const的函数
}
int main() {
test01();
system(“pause”);
return 0;
}


## 4.4 友元


      假如想让某一些类内的私有数据被类外的一些函数或者类所访问,我们就需要用到友元(好朋友)。即**友元的目的就是让一个类或者函数访问另一个类中的私有成员。**


### 1.友元的三种实现


      **友元的三种实现:全局函数、类、成员函数。**


**1)全局函数**



#include
using namespace std;

class Building
{
//告诉编译器 goodGay全局函数 是 Building类的好朋友,可以访问类中的私有内容
friend void goodGay(Building* building); //friend 函数声明;
public:
Building()
{
this->m_SittingRoom = “客厅”;
this->m_BedRoom = “卧室”;
}
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom; //卧室
};
void goodGay(Building* building)
{
cout << "好基友正在访问: " << building->m_SittingRoom << endl;
cout << "好基友正在访问: " << building->m_BedRoom << endl;
}
void test01()
{
Building b;
goodGay(&b);
}
int main() {
test01();
system(“pause”);
return 0;
}


**2)类**



#include
using namespace std;

class Building;
class goodGay
{
public:
goodGay();
void visit();
void visitOut(Building* b);
private:
Building* building;
};
class Building
{
//告诉编译器 goodGay类是Building类的好朋友,可以访问到Building类中私有内容
friend class goodGay;
public:
Building();
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom;//卧室
};
//类外实现构造函数和成员函数
Building::Building()
{
this->m_SittingRoom = “客厅”;
this->m_BedRoom = “卧室”;
}
goodGay::goodGay()
{
building = new Building;
}
//友元声明后可以访问类内类成员对象中的私有属性
void goodGay::visit()
{
cout << “好基友正在访问” << building->m_SittingRoom << endl;
cout << “好基友正在访问” << building->m_BedRoom << endl;
}
//友元声明后可以访问类外对象中的私有属性
void goodGay::visitOut(Building* b)
{
cout << “好基友正在访问” << building->m_SittingRoom << endl;
cout << “好基友正在访问” << building->m_BedRoom << endl;
}
void test01()
{
goodGay gg;
gg.visit();
}
void test02()
{
goodGay gg;
Building b;
gg.visitOut(&b);
}
int main() {
test01();
test02();
system(“pause”);
return 0;
}


**3)成员函数**



#include
using namespace std;

class Building;
class goodGay
{
public:
goodGay();
void visit(); //只让visit函数作为Building的好朋友,可以发访问Building中私有内容
void visitOut(Building* b);
void visit2();
private:
Building* building;
};
class Building
{
//告诉编译器 goodGay类中的visit成员函数 是Building好朋友,可以访问私有内容
friend void goodGay::visit();
friend void goodGay::visitOut(Building* b);
public:
Building();
public:
string m_SittingRoom; //客厅
private:
string m_BedRoom;//卧室
};
Building::Building()
{
this->m_SittingRoom = “客厅”;
this->m_BedRoom = “卧室”;
}
goodGay::goodGay()
{
building = new Building;
}
void goodGay::visit()
{
cout << “好基友正在访问” << building->m_SittingRoom << endl;
cout << “好基友正在访问” << building->m_BedRoom << endl;
}
void goodGay::visitOut(Building* b)
{
cout << “好基友正在访问” << b->m_SittingRoom << endl;
cout << “好基友正在访问” << b->m_BedRoom << endl;
}
void goodGay::visit2()
{
cout << “好基友正在访问” << building->m_SittingRoom << endl;
//不是友元成员函数不可访问
//cout << “好基友正在访问” << building->m_BedRoom << endl;
}
//友元成员函数可以访问类内类对象的私有属性
void test01()
{
goodGay gg;
gg.visit();
}
//友元成员函数可以访问类外类对象的私有属性
void test02()
{
goodGay gg;
Building b;
gg.visitOut(&b);
}
int main() {
test01();
test02();
system(“pause”);
return 0;
}


## 4.5 运算符重载


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


**汇总:**



//1.加号运算符重载
Person operator+(const Person& p)

//2.左移运算符重载
ostream& operator<<(ostream& cout, Person& p)

//3.递增运算符重载
//前置++
className& operator++()
//后置++
className& operator++(int)

//4.赋值运算符重载
className& operator=(className &p)

//5.关系运算符重载
bool operator==(className &p)
bool operator!=(className &p)

//6.函数调用运算符重载
dataType operator()(dataType1 p1,…,dataTypen pn)


### **1.加号运算符重载**


**这里可以是成员函数实现,也可以是全局函数,而且可以发生重载,非常灵活。**



#include
using namespace std;

class Person {
public:
Person() {};
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
//成员函数实现 + 号运算符重载
Person operator+(const Person& p) { //这里只需要再传入一个参数就行
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
public:
int m_A;
int m_B;
};
//全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
//Person temp(0, 0);
//temp.m_A = p1.m_A + p2.m_A;
//temp.m_B = p1.m_B + p2.m_B;
//return temp;
//}
//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int val)
{
Person temp;
temp.m_A = p2.m_A + val;
temp.m_B = p2.m_B + val;
return temp;
}
void test() {
Person p1(10, 10);
Person p2(20, 20);
//成员函数方式
Person p3 = p2 + p1; //相当于 p2.operaor+(p1)
cout << “mA:” << p3.m_A << " mB:" << p3.m_B << endl;
Person p4 = p3 + 10; //相当于 operator+(p3,10)
cout << “mA:” << p4.m_A << " mB:" << p4.m_B << endl;
}
int main() {
test();
system(“pause”);
return 0;
}


### 2.左移运算符重载


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


**注意:一般不用成员函数重载**,因为cout不能在左边,会实现成:p << cout;


这里这个函数为什么使用引用参数且返回引用数据类型呢?


答:下面有链式调用,因此如果写成void就不能成功调用。而且我们希望一直返回这个唯一的对象,所以我们一般使用引用来获取这个对象。



#include
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) {
out << “a:” << p.m_A << " b:" << p.m_B;
return out;
}
void test() {
Person p1(10, 20);
cout << p1 << “hello world” << endl; //链式编程
}
int main() {
test();
system(“pause”);
return 0;
}


### 3.递增运算符重载


**作用:实现自己的整型数据。**


**注意:这里的后置不能返回引用,因为返回的是一个临时数据!!!**



#include
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) {
//先返回
//记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++
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() {
test01();
cout<<endl;
test02();
system(“pause”);
return 0;
}


### 4.复制运算符重载


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


1. **默认构造函数**
2. **默认析构函数**
3. **默认拷贝构造**
4. **赋值运算符operator=**


注意:如果类中有在堆区开辟的属性,也会出现深浅拷贝的问题。(重载就可以解决这个问题)



#include
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);
p1 = p2 = p3; //赋值操作,这里连续赋值需要返回对象本身 从后往前执行p2 = p3,p1 = p2
cout << “p1的年龄为:” << *p1.m_Age << endl;
cout << “p2的年龄为:” << *p2.m_Age << endl;
cout << “p3的年龄为:” << *p3.m_Age << endl;
}
int main() {
test01();
system(“pause”);
return 0;
}


### 5.关系运算符重载


作用:让两个自定义类型对象进行对比操作。



#include
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()
{
//int a = 0;
//int b = 0;
Person a(“孙悟空”, 18);
Person b(“孙悟空”, 18);
if (a == b)
{
cout << “a和b相等” << endl;
}
else
{
cout << “a和b不相等” << endl;
}
if (a.operator!=(b))
{
cout << “a和b不相等” << endl;
}
else
{
cout << “a和b相等” << endl;
}
}
int main() {
test01();
system(“pause”);
return 0;
}


### 6.函数调用运算符重载



* 由于重载后使用的方式非常像函数的调用,因此称为**仿函数**
* **仿函数没有固定写法,非常灵活**



#include
using namespace std;
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
//重载的()操作符 也称为仿函数
MyPrint myFunc;
myFunc(“hello world”);
}
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
//这里创建了一个匿名对象MyAdd(),然后调用了这个仿函数
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}
int main() {
test01();
test02();
system(“pause”);
return 0;
}


## 4.6 继承


**继承是面向对象三大特性之一!**


也就是说子类有父类的一些特性,我们就可以考虑**利用继承的技术减少重复代码**。


### 1.继承的基本语法


**class A :public B**(A称为子类或者派生类,B称为父类或基类)


派生类中有两大部分:一类是从基类继承过来的,另一类是自身增加的成员。(共性和特性)


### 2.继承方式


**公共继承、保护继承、私有继承**


**总结区别:**


1. 父类的隐私属性哪种继承都无法访问
2. 保护继承将其他属性都变成保护属性
3. 公有继承将其他属性都变成公有属性
4. 私有属性将其他属性都变成私有属性


![](https://img-blog.csdnimg.cn/direct/024bee36f9c74f49a1e48a6c91d73647.png)


### 3.继承中的对象模型


**父类中所有非静态的属性都会被继承下来。虽然访问不了。**



#include
using namespace std;
class Base
{
public:
int m_A;
protected:
int m_B;
private:
int m_C; //私有成员只是被隐藏了,但是还是会继承下去
};
//公共继承
class Son :public Base
{
public:
int m_D;
};
void test01()
{
cout << "sizeof Son = " << sizeof(Son) << endl;
}
int main() {
test01();
system(“pause”);
return 0;
}


![](https://img-blog.csdnimg.cn/direct/a1ac239f68ac41b1bde61d750b188ac1.png)


![](https://img-blog.csdnimg.cn/direct/ad84df1832d84d71b0f3364a2a48c8fb.png)


### 4.继承中构造和析构顺序


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


**顺序:先构造父类(有父才有子)**



#include
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() {
test01();
system(“pause”);
return 0;
}


![](https://img-blog.csdnimg.cn/direct/07af5cc947a84afe9ea520735eb5273b.png)


### 5.继承同名成员处理方式


**当子类和父类出现同名属性或函数时,编译器会自动隐藏父类的同名属性和函数,访问父类同名成员需要加作用域!**


**注意:就算两个同名函数是重载也无法访问父类中的同名函数!**



#include
using namespace std;
class Base {
public:
Base()
{
m_A = 100;
}
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()
{
m_A = 200;
}
//当子类与父类拥有同名的成员函数,子类会隐藏父类中所有版本的同名成员函数
//如果想访问父类中被隐藏的同名成员函数,需要加父类的作用域
void func()
{
cout << “Son - func()调用” << endl;
}
public:
int m_A;
};
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);
//s.func(10); 会报错,过多参数
}
int main() {
test01();
system(“pause”);
return 0;
}


### 6.继承同名静态成员处理方式



       静态成员和非静态成员出现同名,处理方式一致,子类静态成员可以直接访问,父类的需要加作用域。


       不同于非静态的在于访问静态成员有两种方式,通过对象访问和之前相同,多的一种方法是通过类名进行访问。



#include
using namespace std;
class Base {
public:
static void func()
{
cout << “Base - static void func()” << endl;
}
static void func(int a)
{
cout << “Base - static void func(int a)” << endl;
}
static int m_A;
};
int Base::m_A = 100;
class Son : public Base {
public:
static void func()
{
cout << “Son - static void func()” << endl;
}
static int m_A;
};
int Son::m_A = 200;
//同名成员属性
void test01()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
cout << "Son 下 m_A = " << s.m_A << endl;
cout << "Base 下 m_A = " << s.Base::m_A << endl;
//通过类名访问
cout << "通过类名访问: " << endl;
cout << "Son 下 m_A = " << Son::m_A << endl;
cout << "Base 下 m_A = " << Son::Base::m_A << endl;
}
//同名成员函数
void test02()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问: " << endl;
Son::func();
Son::Base::func();
//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
//通过类名方式访问
Son::Base::func(100);
}
int main() {
//test01();
test02();
system(“pause”);
return 0;
}
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

= " << Son::Base::m_A << endl;
}
//同名成员函数
void test02()
{
//通过对象访问
cout << "通过对象访问: " << endl;
Son s;
s.func();
s.Base::func();
cout << "通过类名访问: " << endl;
Son::func();
Son::Base::func();
//出现同名,子类会隐藏掉父类中所有同名成员函数,需要加作作用域访问
//通过类名方式访问
Son::Base::func(100);
}
int main() {
//test01();
test02();
system(“pause”);
return 0;
}
先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前在阿里

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Linux运维全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
[外链图片转存中…(img-hrQxgSS9-1714397484536)]
[外链图片转存中…(img-pmbK7KBM-1714397484537)]
[外链图片转存中…(img-5lVlxJvK-1714397484537)]
[外链图片转存中…(img-5zzDuuPF-1714397484537)]
[外链图片转存中…(img-uMpktQlb-1714397484537)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以点击这里获取!

  • 25
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值