文章目录
一、运算符重载
1.加号运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
//1.成员函数重载+号
/*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;
}*/
int m_A;
int m_B;
};
//2.全局函数重载+号
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 test01()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
//成员函数重载本质调用
//Person p3 = p1.operator+(p2);
//全局函数重载本质调用
//Person p3 = operator+(p1, p2);
Person p3 = p1 + p2;
//运算符重载 也可以发生函数重载
Person p4 = p1 + 100;//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();
return 0;
}
2.左移运算符重载
#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream& out, Person p);
public:
Person(int a, int b)
{
m_A = a;
m_B = b;
}
private:
//利用成员函数重载 左移运算符 p.operator << (cout) 简化版本: p << cout
//不会利用成员函数重载 << 运算符,因为无法实现 cout 在左侧
/*void operator<<(Person& cout)
{
}*/
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 out;
}
void test01()
{
Person p(10, 10);
//p.m_A = 10;
//p.m_B = 10;
cout << p << " hhhhhhh" << endl;
}
int main()
{
test01();
return 0;
}
3.递增运算符重载
#include <iostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger myInt);
public:
MyInteger()
{
m_Num = 0;
}
//重载前置++运算符
MyInteger& operator++()//返回引用为了一直对一个数据进行递增操作
{
//先进行++计算
++m_Num;
//再将自身做返回
return *this;
}
//重载后置++运算符
MyInteger operator++(int)//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()
{
test01();
/*int a = 0;
cout << ++(++a) << endl;
cout << a << endl;*/
test02();
return 0;
}
4.赋值运算符重载
#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;*/
return 0;
}
5.关系运算符重载
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
//重载 == 号
bool operator==(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
{
return true;
}
return false;
}
string m_Name;
int m_Age;
};
void test01()
{
Person p1("Tom", 18);
Person p2("Tom", 18);
if (p1 == p2)
{
cout << "p1 和 p2 是相等的!" << endl;
}
else
{
cout << "p1 和 p2 是不相等的!" << endl;
}
}
int main()
{
test01();
return 0;
}
6.函数调用运算符重载
#include <iostream>
using namespace std;
#include <string>
//打印输出类
class MyPrint
{
public:
//重载函数调用运算符
void operator()(string test)
{
cout << test << endl;
}
};
//仿函数非常灵活,没有固定的写法
//加法类
class MyAdd
{
public:
int operator()(int num1, int num2)
{
return num1 + num2;
}
};
void test02()
{
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret=" << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
}
void MyPrint02(string test)
{
cout << test << endl;
}
void test01()
{
MyPrint myPrint;
myPrint("hello world");//由于使用起来非常类似函数调用,因此称为伪函数
MyPrint02("hello world");
}
int main()
{
//test01();
test02();
return 0;
}
二、继承
1.继承的基本语法
#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;
// }
//};
//
CPP页面
//class CPP
//{
//public:
// void header()
// {
// cout << "首页、公开课、登录、注册、……(公共头部)" << endl;
// }
// void footer()
// {
// cout << "帮助中心,交流合作、站内地图……(公共底部)" << endl;
// }
// void left()
// {
// cout << "Java、Python、C++、……(公共分类列表)" << endl;
// }
// void content()
// {
// cout << "CPP学习视频" << 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
{
public:
void content()
{
cout << "Java学科视频" << endl;
}
};
//Python页面
class Python :public BasePage
{
public:
void content()
{
cout << "Python学科视频" << endl;
}
};
//C++页面
class CPP :public BasePage
{
public:
void content()
{
cout << "CPP学科视频" << endl;
}
};
void test01()
{
cout << "Java下载视频页面如下:" << endl;
Java ja;
ja.header();
ja.footer();
ja.left();
ja.content();
cout << "---------------------------------------------" << endl;
cout << "Pythob下载视频页面如下:" << endl;
Python py;
py.header();
py.footer();
py.left();
py.content();
cout << "---------------------------------------------" << endl;
cout << "Cpp下载视频页面如下:" << endl;
CPP cpp;
cpp.header();
cpp.footer();
cpp.left();
cpp.content();
cout << "---------------------------------------------" << endl;
}
int main()
{
test01();
return 0;
}
2.继承方式
#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 s1;
//s1.m_A = 1000;//在Son2中 m_A变为保护权限,因此类外访问不到
//s1.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 = 1000;//父类中公共成员 到子类中变为 私有成员
m_B = 1000;//父类中保护成员 到子类中变为 私有成员
//m_C = 1000;//父类中私有成员 子类访问不到
}
};
void test03()
{
Son3 s1;
//s1.m_A = 10000;//在Son3中 m_A变为私有权限,子类访问不到
//s1.m_B = 10000;//在Son3中 m_B变为私有权限,子类访问不到
}
int main()
{
return 0;
}
3.继承中的对象模型
4.继承中构造和析构顺序
5.继承同名成员处理方式
#include <iostream>
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;
}
int m_A;
};
class Son :public Base
{
public:
Son()
{
m_A = 200;
}
void func()
{
cout << "Son-func调用" << endl;
}
int m_A;
};
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();
return 0;
}
6.继承同名静态成员处理方式
#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();
return 0;
}
7.多继承语法
#include <iostream>
using namespace std;
class Base1
{
public:
Base1()
{
m_A = 100;
}
int m_A;
};
class Base2
{
public:
Base2()
{
m_A = 200;
}
int m_A;
};
//子类 需要继承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 << "Base1::m_A=" << s.Base1::m_A << endl;
cout << "Base2::m_A=" << s.Base2::m_A << endl;
}
int main()
{
test01();
return 0;
}
7.菱形继承
#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();
return 0;
}