C++基础学习(2)

一、运算符重载

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

1.1 加号运算符的重载

作用:

实现两个自定义数据类型相加的运算

#include<stdio.h>
#include<iostream>
using namespace std;

//重载运算符
class Person {
public:
	//成员函数重载+法运算符
	/*Person operator+(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() {};
	Person(int a,int b) {
		m_A = a;
		m_B = b;
	}

};

//通过全局来重载+法运算符
Person operator+ (Person & p1, Person & p2) {
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;

	return temp;
}

//还可以创建类似的+法重载函数
Person operator+ (Person& p1, int num) {
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;

	return temp;
}

void test() {
	Person p1(10, 10);
	Person p2(20, 30);

	Person p3 = p1 + p2;

	cout << "p3.m_B = " << p3.m_B << endl;
	cout << "p3.m_A = " << p3.m_A << endl;

	Person p4 = p2 + 100;
	cout << "p4.m_B = " << p4.m_B << endl;
	cout << "p4.m_A = " << p4.m_A << endl;
}

int main() {
	test();
}

总结:

  • 总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
  • 总结2:不要滥用运算符重载

1.2 左移运算符的重载

左移运算符:>>

作用:

可以输出自定义数据类型

#include<stdio.h>
#include<iostream>
using namespace std;

class Person {
public:
	//创建成员函数去调用<<重载,不可行,因为只能让cout在其右侧,重载不出来
	/*ostream& operator<<(ostream& cout) {
		cout << this->m_A << " " << this->m_B;
		return cout;
	}*/

public:
	int m_A;
	int m_B;

	Person(int a, int b) {
		m_A = a;
		m_B = b;
	}
};

ostream& operator<<(ostream& cout, Person& p) {
	cout << p.m_A << " " << p.m_B;
	return cout;
}
void test() {
	Person p(10, 20);
	/*p.operator<<(cout);
	若用成员函数只能写成这样
	*/

	//我们应该重写为下面的形式
	cout << p;

}

int main() {
	test();
}

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

1.3 递增运算符的重载

作用

通过重载递增运算符,实现自己的整型数据

#include<stdio.h>
#include<iostream>
using namespace std;

class MyInteger {
public:
	int MyNum;

	MyInteger() {
		MyNum = 0;
	}
	//++前置
	MyInteger& operator++() {
		//先将数字++;
		MyNum++;
		//再返回其数字
		return *this;
	}

	//后置++,通过后面是否跟上int占位符,跟着就为后置

	MyInteger operator++(int) {//这里必须是返回值,因为这里返回的是临时变量,调用后就会消失

		//首先创建一个临时变量来存储值,用作后面的返回
		MyInteger temp = *this;

		MyNum++;
		return temp;
	}
};

ostream& operator<<(ostream& cout,MyInteger p) {
	cout << p.MyNum;
	return cout;
}

void test() {
	/*MyInteger p1;
	cout<< ++p1 << endl;
	cout << p1;*/

	MyInteger p2;
	cout << p2++ << endl;
	cout << p2 << endl;
}

int main() {
	test();
}

1.4 赋值运算符的重载

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

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

为什么要进行赋值运算符的重载:

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

#include<stdio.h>
#include<iostream>
using namespace std;

class Person {
public:
	int* m_age;

	Person(int age) {
		this->m_age = new int(age);
	}

	//重载赋值运算符(必须返回的为引用数据类型)
	Person& operator= (Person& p) {
		//如之前的有数字的话,你们就先将其清理干净后在进行深拷贝
		if (m_age != NULL) {
			delete m_age;
			m_age = NULL;
		}

		//进行深拷贝方法
		m_age = new int(*p.m_age);

		return *this;//(返回当前的Person类,才能够进行链式编程)
	}

	~Person() {
		if (this->m_age != NULL) {
			delete m_age;
			m_age = NULL;
		}
	}

	
};

void test() {
	Person p2(20);
	Person p1(30);
	Person p3(30);

	p1 = p2 = p3;

	cout << "p1 = " << *p1.m_age << endl;
	cout << "p2 = " << *p2.m_age << endl;
	cout << "p3 = " << *p3.m_age << endl;
}

int main() {
	test();

}

1.5 关系运算符的重载

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

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;

class Person {
public:
	string m_name;
	int m_age;
	Person(string name, int age) {
		this->m_age = age;
		this->m_name = name;
	}

	//进行重载运算符
	bool operator== (Person& p) {
		if (( this->m_age == p.m_age) && (this->m_name = p.m_name)) {
			return true;
		}
		else return false;
	}
};

void test() {
	Person p1("asd", 12);
		Person p2("asd", 12);

		if ( p1 == p2) {
			cout << "他们相等" << endl;
		}
		else {
			cout << "他们不相等" << endl;
		}
}

int main() {
	test();
}

二、继承

继承是面向对象三大特性之一
在这里插入图片描述

  • 我们发现,定义这些类时,下级别的成员除了拥有上一级的共性,还有自己的特性.
  • 这个时候我们就可以考虑利用继承的技术,减少重复代码

2.1 继承的基本用法

总结:

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

语法
class A : public B;

  • A 类称为子类派生类
  • B类称为父类基类

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

  • 类是从基类继承过来的,一类是自己增加的成员
  • 从基类继承过过来的表现其共性,而新增的成员体现了其个性

2.2 继承方式

继承的语法: class 子类 : 继承方式 父类

继承方式一共有三种:

  • 公共继承
  • 保护继承
  • 私有继承

在这里插入图片描述

public继承如下 ,其他的类似

#include<stdio.h>
#include<iostream>
using namespace std;

class Base {
public:
	int age;
protected:
	int ID;
private:
	int height;
};

class son1 :public Base {
public:
	son1 s1() {
		age = 100;//可以修改
		ID = 15;//可以修改
		//height = 180; //不能出现
	}
};

	void test1() {
		son1 s1;
		s1.age = 100;//可以在类外修改
		//s1.ID = 15;//不可以
		//s1.height = 180; //不能
	}

	int main() {
		test1();
	}

2.3 继承当中的对象模型

说明:

  • 父类中所有非静态成员属性都会被子类继承下去
  • 父类中私有成员属性 是被编译器给隐藏了,因此是访问不到,但是确实被继承下去了

2.4 继承当中的构造和析构函数的顺序

  • 首先先创建父类的构造函数,在创建子类的构造函数
  • 析构函数的顺序是与构造函数的调用方法相反

2.5 继承同名成员处理方式

问题: 当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?
  • 访问子类同名成员直接访问即可
  • 访问父类同名成员需要加作用域
#include<stdio.h>
#include<iostream>
using namespace std;

class Base {
public:
	int age;

	void func() {
		cout << "父类的func:函数的调用" << endl;
	}
	void func(int a) {
		cout << "父类的func:(int a)函数的调用" << endl;
	}
};

class Son :public Base {
public:
	int age;
	void func() {
		cout << "子类的func函数:调用" << endl;
	}
};

void test() {
	Son s1;

	//调用子类的func函数
	s1.func();

	//s1.func(10); 这里会报错,因为这里表示的为调用子类的有参成员函数,只有父类才有有参的成员
	//函数,当未加上与解析符的时候,那么会覆盖子类的所有成员函数

	//调用父类的func函数
	s1.Base::func();
	s1.Base::func(10);
}

int main() {
	test();
}

总结:

  1. 子类对象可以直接访问到子类中同名成员
  2. 子类对象加作用域可以访问到父类同名成员
  3. 当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数

2.6继承同名静态成员处理方式

问题: 继承中同名的静态成员在子类对象上如何进行访问?
#include<stdio.h>
#include<iostream>
using namespace std;

class Base {
public:
	static void func() {
		cout << "调用Base当中的静态成员函数" << endl;
	}
	static void func(int a ) {
		cout << "调用Base(int a)当中的静态成员函数" << endl;
	}

	static int age;
};

int Base::age = 100;

class Son : public Base {
public:
	static void func() {
		cout << "调用Son当中的静态成员函数" << endl;
	}
	

	static int age;

};

int Son::age = 200;

void test() {
	//1. 通过成员函数来调用
	Son s1;

	cout << "调用子类的成员属性" << s1.age << endl;
	cout << "调用父类的成员属性" << s1.Base::age << endl;
	s1.func();
	s1.Base::func();


	//2.通过类名来调用
	cout << "调用子类的成员属性" << Son::age<< endl;
	cout << "调用父类的成员属性" << Son::Base::age<< endl;
	Son::func();
	Son::Base::func();
	
}

int main() {
	test();
}

静态成员和非静态成员出现同名,处理方式一致

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

总结:

同名静态成员处理方式和非静态处理方式一样,只不过有两种访问的方式(通过对象和通过类名)

2.7 多继承语法

C++允许一个类继承多个类

语法: class 子类 : 继承方式 ,父类1继承方式 父类2...

多继承可能会引发父类中有同名成员出现,需要加作用域区分

C++实际开发中不建议用多继承

#include<stdio.h>
#include<iostream>
using namespace std;

class Base1 {
public:
	int age;
};

class Base2 {
public:
	int age;
};

class Son : public Base1, public Base2 {
public:
	int a;
	int b;

	Son() {
		Base1::age = 100;
		Base2::age = 320;
		a = 1;
		b = 1;
	}
};

void test() {
	Son s1;

	cout << "Base1类当中的age: " << s1.Base1::age << endl;
	cout << "Base2类当中的age: " << s1.Base2::age << endl;
}

int main() {
	test();
}

总结: 多继承中如果父类中出现了同名情况,子类使用时候要加作用域

三、多态

3.1 多态的基本概念

多态是C++面向对象三大特性之一

多态分为两类

  • 静态多态: 函数重载 和 运算符重载属于静态多态,复用函数名
  • 动态多态: 派生类和虚函数实现运行时多态

静态多态和动态多态区别:

  • 静态多态的函数地址早绑定 编译阶段确定函数地址
  • 动态多态的函数地址晚绑定 运行阶段确定函数地址

实现多态的条件

1、有继承关系
2、子类重写父类的虚函数

动态多态使用父类的指针或者引用 指向子类对象

下面通过案例进行讲解多态

#include<stdio.h>
#include<iostream>
using namespace std;

class Animal {
public:
	virtual void speak(Animal& a) {
		cout << "动物在说话" << endl;
	}
};

class Cat :public Animal {
public:
	//重写虚函数,但是重写父类的虚函数对象与重载不同,返回值,参数必须完全相同
	void speak(Animal& a) {
		cout << "小猫在说话" << endl;
	}
};

void test() {
	Cat cat;
	cat.speak(cat);
}

int main() {
	test();
}

3.2 纯虚函数和抽象类

在多态中,通常父类中虚函数的实现是毫无意义的,主要都是调用子类重写的内容

  • 因此可以将虚函数改为纯虚函数

  • 纯虚函数语法: virtual 返回值类型 函数名(参数列表) = 0;

  • 当类中有了纯虚函数,这个类也称为抽象类

抽象类特点

  • 无法实例化对象
  • 子类必须重写抽象类中的纯虚函数,否则也属于抽象类
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值