运算符重载
运算符重载是对已有的运算符重新定义,赋予另一种功能,以适应不同的数据类型
加号运算符重载代码:
#include<iostream>
using namespace std;
//加号运算符重载
class Person {
public:
//1.成员函数重载+号 本质 Person p3=p1.operator+(p2);
//Person operator+(Person& p) {
// Person temp;
// temp.m_A = p.m_A + this->m_A;
// temp.m_B = p.m_B + this->m_B;
// return temp;
//}
int m_A;
int m_B;
};
//2.全局函数重载+号 本质 Person p3=operator+(p1,p2);
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 a) {
Person temp;
temp.m_A = p1.m_A + a;
temp.m_B = p1.m_B + a;
return temp;
}
void test01() {
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
//1.成员函数重载 + 号
//2.全局函数重载+号
//运算符重载 也可以发生函数重载
Person p4= p1 + 10;//Person + int
cout << "p3.m_A=" << p3.m_A << endl;
cout << "p3.m_B=" << p3.m_B << endl;
cout << "p4.m_A=" << p4.m_A << endl;
cout << "p4.m_B=" << p4.m_B << endl;
}
int main() {
test01();
system("pause");
return 0;
}
注意:
不可以对内置的数据类型的表达式的运算符进行重载
不要滥用运算符重载
左移运算符重载代码:
代码:
#include<iostream>
using namespace std;
//左移运算符重载
class Person {
public:
//利用成员函数重载 左移运算符 p.operator<<(cout) 简化版本 p<<cout
//通常不会利用成员函数重载<<运算符,因为无法实现cout在左边
/*void operator<<(Person &p) {
}*/
int m_A;
int m_B;
};
//只能利用全局函数重载左移运算符
ostream& operator<< (ostream &out,Person &p) {//本质 operator<<(cout,p) 简化cout<<p
out << "m_A=" << p.m_A << "m_B=" << p.m_B;
return cout;
}
void test01() {
Person p;
p.m_A = 10;
p.m_B = 10;
//链式编程思想
cout << p << "hello world" << endl;
}
int main() {
test01();
system("pause");
return 0;
}
++运算符重载
代码:
#include<iostream>
using namespace std;
//重载递增运算符
//自定义的整型
class MyInteger {
friend ostream& operator<<(ostream& cout, MyInteger myint);
private:
int m_Num;
public:
MyInteger() {
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++() {
m_Num++;
return *this;//将自身返回
}
//重载后置++运算符
MyInteger operator++(int) {//后置递增要返回值 而不是引用,因为局部变量就被释放 只能返回值(拷贝构造)
//int 代表占位参数 可以用于区分前置和后置递增
//先记录结果
MyInteger temp = *this;
//后递增
m_Num++;
//最后将记录结果返回
return temp;
}
};
// 重载左移运算符
ostream& operator<<(ostream& cout, MyInteger myint) {
cout << myint.m_Num;
return cout;
}
void test01() {
MyInteger myint;
cout << ++(++myint) << endl;
cout << myint << endl;
}
void test02() {
MyInteger myint;
cout << myint++ << endl;
cout << myint << endl;
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
赋值运算符重载
代码:
#include<iostream>
using namespace std;
//赋值运算符的重载
class Person {
public:
Person(int age) {
m_Age=new int(age);
}
~Person() {
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
}
//重载 赋值运算符
Person& operator=(Person &p) {
//编译器提供的是浅拷贝
//m_Age = p.m_Age;
//先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
//深拷贝
m_Age = new int(*p.m_Age);
//返回对象本身
return *this;
}
int *m_Age;
};
void test01() {
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;//赋值操作
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl;
}
int main() {
test01();
//int a = 10;
//int b = 20;
//int c = 30;
//c = b = a;
//cout << "a=" << a << endl;
//cout << "b=" << b << endl;
//cout << "c=" << c << endl;
system("pause");
return 0;
}
关系运算符重载
代码:
#include<iostream>
using namespace std;
//重载关系运算符
class Person {
public:
//重载==运算符
bool operator==(Person &p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
}
return false;
}
//重载!=运算符
bool operator!=(Person& p) {
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return false;
}
return true;
}
Person(string name,int age ) {
m_Name = name;
m_Age = age;
}
string m_Name;
int m_Age;
};
void test01() {
Person p1("Tom",18);
Person p2("Tom", 18);
if (p1 == p2) {
cout << "p1 and p2 same" << endl;
}
else {
cout << "p1 and p2 no same" << endl;
}
if (p1 != p2) {
cout << "p1 and p2 no same" << endl;
}
else {
cout << "p1 and p2 same" << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
函数调用运算符重载
函数调用运算符()也可以重载
代码:
#include<iostream>
using namespace std;
//函数调用运算符重载
//打印输出类
class MyPrint {
public:
//重载的函数调用运算符
void operator()(string test) {
cout << test << endl;
}
};
void MyPrint02(string test) {
cout << test << endl;
}
void test01() {
MyPrint myPrint;
myPrint("Hello World");//由于用起来非常类似于函数调用,因此成为仿函数
MyPrint02("hello world");
}
//仿函数非常的灵活,没有固定的写法
//加法类
class MyAdd {
public:
int operator()(int num1,int num2) {
return num1 + num2;
}
};
void test02() {
MyAdd myAdd;
int ret=myAdd(100, 100);
cout << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
继承
语法:class 子类 : 继承方式 父类
代码:
#include<iostream>
using namespace std;
//普通实现页面
//java页面
//class Java {
//public:
// void header() {
// cout << "首页、公开课、登录、注册...(公共头部)" << endl;
// }
// void footer() {
// cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
// }
// void left() {
// cout << "Java、Python、C++、...(公共分类列表)" << endl;
// }
// void content() {
// cout << "Java学科视频" << endl;
// }
//
//};
//
Python页面
//class Python {
//public:
// void header() {
// cout << "首页、公开课、登录、注册...(公共头部)" << endl;
// }
// void footer() {
// cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
// }
// void left() {
// cout << "Java、Python、C++、...(公共分类列表)" << endl;
// }
// void content() {
// cout << "Python学科视频" << endl;
// }
//
//};
//
//
C++页面
//class Cpp {
//public:
//void header() {
// cout << "首页、公开课、登录、注册...(公共头部)" << endl;
//}
//void footer() {
// cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
//}
//void left() {
// cout << "java、python、c++、...(公共分类列表)" << endl;
//}
// void content() {
// cout << "C++学科视频" << endl;
// }
//
//};
//继承实现页面
//公共页面
class BasePage {
public:
void header() {
cout << "首页、公开课、登录、注册...(公共头部)" << endl;
}
void footer() {
cout << "帮助中心、交流合作、站内地图...(公共底部)" << endl;
}
void left() {
cout << "java、python、c++、...(公共分类列表)" << endl;
}
};
//继承的好处:减少重复代码
//语法:class 子类 : 继承方式 父类
//Java页面
class Java :public BasePage {//Java类继承BasePage类
public:
void content() {
cout << "Java学科视频" << endl;
}
};
//Python页面
class Python :public BasePage {
public:
void content() {
cout << "Java学科视频" << endl;
}
};
//C++页面
class Cpp :public BasePage {
public:
void content() {
cout << "Java学科视频" << endl;
}
};
void test01() {
cout << "Java下载视频页面如下:" << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "----------------------------------------------" << endl;
cout << "Python下载视频页面如下:" << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "----------------------------------------------" << endl;
cout << "C++下载视频页面如下:" << endl;
Cpp cpp;
cpp.header();
cpp.footer();
cpp.left();
cpp.content();
}
int main() {
test01();
system("pause");
return 0;
}
继承方式
语法:class 子类 : 继承方式 父类
继承方式一共有三种:
公共继承
保护继承
私有继承
图解:
代码:
#include<iostream>
using namespace std;
//继承方式
//公共继承
class Base1 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son1 :public Base1 {
public:
void func() {
m_A = 10;//父类中的公共权限成员 到子类中依然是公共权限
m_B = 10;//父类中的保护权限成员 到子类中依然是保护权限
//m_C = 10;//父类中的私有权限成员 到子类中访问不到
}
};
void test01() {
Son1 s1;
s1.m_A = 100;
//s1.m_B = 100;//到Son1中 m_B是保护权限 类外访问不到
}
//保护继承
class Base2 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son2 :protected Base2 {
public:
void func() {
m_A = 100;//父类中的公共权限成员 到子类中变为保护权限
m_B = 100;//父类中的保护权限成员 到子类中变为保护权限
//m_C = 100;//父类中的私有权限成员 到子类中访问不到
}
};
void test02() {
Son2 s2;
//s2.m_A = 1000;//在Son2中 m_A变为了保护权限,因此类外访问不到
//s2.m_B = 1000;//在Son2中 m_B保护权限 不可以访问
}
//私有继承
class Base3 {
public:
int m_A;
protected:
int m_B;
private:
int m_C;
};
class Son3 :private Base3 {
public:
void func() {
m_A = 100;//父类中的公共权限成员 到子类中变为私有权限
m_B = 100;//父类中的保护权限成员 到子类中变为私有权限
//m_C = 100;//父类中的私有权限成员 到子类中访问不到
}
};
class GrandSon3 :public Son3{
public:
void func() {
//m_A = 1000;//到了Son3中 m_A变为私有,即使是儿子,也是访问不到
//m_B = 1000;//到了Son3中 m_B变为私有,即使是儿子,也是访问不到
}
}
void test03() {
Son3 s3;
//s3.m_A = 100;//到了Son3中 变为 私有成员 类外访问不到
//s3.m_B = 100;//到了Son3中 变为 私有成员 类外访问不到
}
int main() {
system("pause");
return 0;
}
继承中的对象模型
问题 :从父类继承过来的成员属于那个子类?
代码:
#include<iostream>
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;
};
//利用开发人员命令提示工具查看对象模型
//跳转盘符 F:
//跳转文件路径 cd 具体路径
//查看命令
// cl /d1 reportSingleClassLayout类名 文件名
void test01() {
//16
//父类中所有非静态成员属性都会被子类继承下去
//父类中私有成员属性 是被编译器给隐藏了,因此访问不到,但是确实继承下去了
cout << "size of Son=:" << sizeof(Son) << endl;
}
int main() {
test01();
system("pause");
return 0;
}
继承中构造和析构顺序
子类继承父类后,当创建子类对象,也会调用父类构造函数
问题 :父类和子类的构造和析构顺序是谁先谁后?
代码:
#include<iostream>
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() {
//Base b;
Son s;//继承中的构造和析构顺序如下:
//先构造父类,再构造子类,析构的顺序和构造相反 白发人送黑发人
}
int main() {
test01();
system("pause");
return 0;
}
继承同名成员处理方式
问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据呢?
访问子类同名成员 直接访问即可
访问父类同名成员 需加作用域
代码:
#include<iostream>
using namespace std;
//继承同名成员处理
class Base {
public:
Base() {
m_A = 100;
}
void func() {
cout << "Base---func() invoke" << endl;
}
void func(int a) {
cout << "Base---func(int a) invoke" << endl;
}
int m_A;
};
class Son :public Base {
public:
int m_A;
void func() {
cout << "Son---func() invoke" << endl;
}
Son() {
m_A = 200;
}
};
//同名的成员属性处理方式
void test01() {
Son s;
cout << "Son 下的m_A= " << s.m_A << endl;
//如果通过子类对象访问到父类中同名成员,需要加作用域
cout << "Base 下的m_A= " << s.Base::m_A << endl;
}
//同名的成员函数处理方式
void test02() {
Son s;
s.func();//直接调用 调用的是子类中的同名成员
s.Base::func();//加作用域调用父类
//如果子类中出现了和父类同名的成员函数,子类的同名成员会隐藏掉父类中所有的同名成员函数
//如果想访问到父类中被隐藏的同名成员函数,需要加作用域
s.Base::func(100);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
结论:
1.子类对象可以直接访问到子类中同名成员
2.子类对象加作用域可以访问到父类同名成员
3.当子类与父类拥有同名的成员函数,子类会隐藏父类中同名成员函数,加作用域可以访问到父类中同名函数
继承同名静态成员处理方式
问题 :继承中同名的静态成员在子类对象上如何访问?
静态成员和非静态成员出现同名,处理方式一致
1.访问子类同名成员 直接访问即可
2.访问父类同名成员 需加作用域
代码:
#include<iostream>
using namespace std;
//继承中的同名静态成员处理方式
class Base {
public:
static int m_A;
static void func() {
cout << "Base - static void func() " << endl;
}
static void func(int a) {
cout << "Base - static void func(int a) " << endl;
}
};
int Base::m_A = 100;
class Son :public Base {
public:
static int m_A;
static void func() {
cout << "Son - static void func() " << endl;
}
};
int Son::m_A = 200;
//同名静态成员属性
void test01() {
//1.通过对象来访问
cout << "通过对象来访问" << endl;
Son s;
cout << "Son m_A=" << s.m_A << endl;
cout << "Base m_A=" << s.Base::m_A << endl;
//2.通过类名来访问数据
cout << "通过类名来访问" << endl;
cout << "Son m_A=" << Son::m_A << endl;
//第一个::代表通过类名方式访问 第二个::代表访问父类作用域下
cout << "Base m_A=" << Son::Base::m_A << endl;
}
//同名静态成员函数
void test02() {
//1.通过对象访问
cout << "通过对象来访问" << endl;
Son s;
s.func();
s.Base::func();
//2.通过类名的方式访问
cout << "通过类名来访问" << endl;
Son::func();
Son::Base::func();
//子类出现和父类同名的静态成员函数,也会隐藏父类中所有同名成员函数
//如果想访问父类中被隐藏的同名成员,需加作用域
Son::Base::func(100);
}
int main() {
//test01();
test02();
system("pause");
return 0;
}
多继承语法
C++允许一个类继承多个类
语法:class 子类 :继承方式 父类1,继承方式 父类2…
多继承可能会引发父类中有同名成员出现,需要加作用区分
c++实际开发中不建议用多继承
代码:
#include<iostream>
using namespace std;
//多继承语法
class Base1 {
public:
int m_A;
Base1() {
m_A = 100;
}
};
class Base2 {
public:
int m_A;
Base2() {
m_A = 200;
}
};
//子类 需要继承Base1 和Base2
//语法: class 子类:继承方式 父类1,继承方式 父类2
class Son : public Base1, public Base2 {
public:
Son() {
m_C = 300;
m_D = 400;
}
int m_C;
int m_D;
};
void test01() {
Son s;
cout << "sizeof Son=" << sizeof(s) << endl;
//当父类中出现同名成员,需要加作用域区分
cout << "m_A=" << s.Base1::m_A << endl;
cout << "m_A=" << s.Base2::m_A << endl;
}
int main() {
test01();
system("pause");
return 0;
}
菱形继承
菱形继承概念:
两个派生类继承同一个基类
又有某个类同时继承这两个类
这种继承被称为菱形继承,或者钻石继承
代码:
#include<iostream>
using namespace std;
//动物类
class Animal {
public:
int m_Age;
};
//利用虚继承 解决菱形继承的问题
//羊类
//在继承前加 virtual 变为虚继承
//Animal 类称为虚基类
class Sheep:virtual public Animal {
};
//驼类
class Tuo :virtual public Animal{
};
//羊驼类
class SheepTuo :public Sheep, public Tuo {
};
void test01() {
SheepTuo st;
st.Sheep::m_Age = 18;
st.Tuo::m_Age = 28;
//当菱形继承,两个父类拥有相同的数据,需要加以作用域区分
cout << "st.Sheep::m_Age=" << st.Sheep::m_Age << endl;
cout << "st.Tuo::m_Age =" << st.Tuo::m_Age << endl;
//这份数据只要有一份就可以了 菱形继承导致数据有两份 资源浪费
//利用虚继承 解决菱形继承的问题
cout << "st.m_Age =" << st.m_Age << endl;
}
int main() {
test01();
system("pause");
return 0;
}