构造函数、析构函数
#include <iostream>
using namespace std;
class Person
{
private:
int age;
public:
Person()
{
cout << "构造函数的调用,无参" << endl;
}
Person(int a)
{
age = a;
cout << "构造函数的调用, 含参" << endl;
}
Person(const Person& p)
{
this->age = p.age;
cout << "构造函数的调用,拷贝" << endl;
}
~Person()
{
cout << "析构函数的调用" << endl;
}
int getAge() { return age; }
};
void test()
{
Person p1;
Person p2 = Person(10);
Person p3 = Person(p2);
Person p4 = 10;
Person p5 = p4;
}
int main()
{
test();
}
初始化列表
#include <iostream>
using namespace std;
class Person
{
public:
int age;
int height;
Person(int a, int b) :age(a), height(b)
{
;
}
};
void test()
{
Person man(28, 180);
cout << man.age << endl << man.height << endl;
}
int main()
{
test();
}
常函数、常对象
#include <iostream>
using namespace std;
class Person
{
public:
void showPerson() const
{
age = 30;
height = 180;
}
void change()
{
age = 40;
height = 166;
}
int age;
mutable int height;
};
void test1()
{
Person p;
p.showPerson();
}
void test2()
{
const Person p;
p.age = 22;
p.height = 175;
p.showPerson();
p.change();
}
int main()
{
test1();
test2();
}
运算符重载
+ 运算符 重载
#include <iostream>
using namespace std;
class Person
{
public:
int age;
int height;
Person operator+(const Person &p)
{
Person temp;
temp.age = this->age + p.age;
temp.height = this->height + p.height;
return temp;
}
};
Person operator+(const Person& p1, const Person& p2)
{
Person temp;
temp.age = p1.age + p2.age;
temp.height = p1.height + p2.height;
return temp;
}
void test()
{
Person p1;
p1.age = 22;
p1.height = 166;
Person p2;
p2.age = 28;
p2.height = 134;
Person p3 = p1 + p2;
cout << p3.age << endl << p3.height << endl;
Person p3 = p3 + 66;
}
int main()
{
test();
}
<< 运算符 重载
#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<<(ostream&, const Person&);
public:
Person(int age, int height) :age(age), height(height)
{
cout << "调用了构造函数" << endl;
}
private:
int age;
int height;
};
ostream& operator<<(ostream& cout, const Person& p)
{
cout << "[ age = " << p.age << ", height = " << p.height << " ]";
return cout;
}
void test()
{
Person p1(22, 166);
cout << p1 << endl;
}
int main()
{
test();
}
自增运算符 重载
#include <iostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream&, MyInteger);
public:
MyInteger(int a) :num(a)
{
cout << "调用了构造函数" << endl;
}
// 前置递增
MyInteger& operator++()
{
num++;
return *this;
}
// 后置递增
MyInteger operator++(int) // int 代表占位参数,可以用于区分前置和后置递增
{
// 先记录当前结果
MyInteger temp = *this;
// 后将自己进行递增
num++;
return temp;
}
private:
int num;
};
ostream& operator<<(ostream& cout, MyInteger obj)
{
cout << obj.num;
return cout;
}
void test()
{
MyInteger myint = 0;
cout << ++(++myint) << endl; // 输出:2
cout << myint << endl; // 输出:2
cout << myint++ << endl; // 输出:2
cout << myint<< endl; // 输出:3
}
int main()
{
test();
}
赋值运算符 重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
this->age = new int(age);
}
~Person()
{
if (age != NULL)
{
delete age;
age = NULL;
}
}
// 赋值运算符重载
Person& operator=(const Person& obj)
{
*age = *obj.age;
return *this;
}
int* age;
};
void test()
{
Person p1(10);
cout << "p1的年龄为:" << *p1.age << endl; // 输出结果:10
Person p2(20);
cout << "p2的年龄为:" << *p2.age << endl; // 输出结果:20
Person p3(30);
cout << "p3的年龄为:" << *p3.age << endl; // 输出结果:30
// 这不是拷贝构造函数,拷贝构造函数也是一种构造函数
// 这里是赋值语句,对象的赋值,编译器默认的行为是:将某对象的所有属性复制一份到另一个对象里面
// 因为默认行为的直接复制,对于需要浅拷贝的内容没什么影响,但是对于需要深拷贝的内容影响很大
// 为了避免恶劣影响,我们需要重载赋值运算符
p3 = p2 = p1;
cout << "修改后的p2的年龄为:" << *p2.age << endl; // 输出结果:10
cout << "修改后的p3的年龄为:" << *p3.age << endl; // 输出结果:10
}
int main()
{
test();
}
关系运算符 重载
#include <iostream>
#include <string>
using namespace std;
class Person
{
public:
string name;
int age;
Person(string name, int age) :name(name), age(age)
{
;
}
bool operator==(const Person& obj)
{
if (name == obj.name && age == obj.age)
{
return true;
}
return false;
}
bool operator!=(const Person& obj)
{
if (name == obj.name && age == obj.age)
{
return false;
}
return true;
}
};
void test()
{
Person p1("Jack", 18);
Person p2("Jack", 18);
Person p3("Tom", 18);
if (p1 == p2) cout << "p1 和 p2 相等" << endl;
if (p1 != p3) cout << "p1 和 p3 不相等" << endl;
}
int main()
{
test();
}
函数调用运算符 重载
#include <iostream>
#include <string>
using namespace std;
class MyPrint
{
public:
// 重载函数调用运算符
void operator()(string text, string end="\n")
{
cout << text << end;
}
};
class MyAdd
{
public:
// 重载函数调用运算符
int operator()(int a, int b)
{
return a + b;
}
};
void test()
{
MyPrint print;
MyAdd add;
print("hello world"); // 由于使用起来非常类似于函数调用,因此称为仿函数
string res = to_string(add(10, 20));
print(res);
// 匿名函数对象 -> MyAdd()
cout << MyAdd()(100, 100) << endl;
}
int main()
{
test();
}
继承知识补充
多态
基础应用
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
virtual void speak()
{
cout << "动物在叫" << endl;
}
};
class Cat :public Animal
{
public:
void speak()
{
cout << "猫在叫" << endl;
}
};
class Dog :public Animal
{
public:
void speak()
{
cout << "狗在叫" << endl;
}
};
void doSpeak(Animal& animal)
{
animal.speak();
}
void test()
{
Cat cat;
Dog dog;
doSpeak(cat);
doSpeak(dog);
}
int main()
{
test();
}
进阶应用
#include <iostream>
#include <string>
using namespace std;
class abstractDrinking
{
public:
virtual void Boil() = 0;
virtual void Brew() = 0;
virtual void PourInCup() = 0;
virtual void PutSomething() = 0;
void make()
{
Boil();
Brew();
PourInCup();
PutSomething();
}
};
class Coffee :public abstractDrinking
{
public:
virtual void Boil()
{
cout << "煮熟自来水" << endl;
}
virtual void Brew()
{
cout << "冲泡咖啡" << endl;
}
virtual void PourInCup()
{
cout << "倒入迷你的咖啡杯中" << endl;
}
virtual void PutSomething()
{
cout << "加入一些糖" << endl;
}
};
class Tea :public abstractDrinking
{
public:
virtual void Boil()
{
cout << "煮熟矿泉水" << endl;
}
virtual void Brew()
{
cout << "冲泡茶叶" << endl;
}
virtual void PourInCup()
{
cout << "倒入经典的茶杯中" << endl;
}
virtual void PutSomething()
{
cout << "加入一些香料" << endl;
}
};
void makeDrink(abstractDrinking* drink)
{
drink->make();
delete drink;
}
void test()
{
makeDrink(new Coffee);
makeDrink(new Tea);
}
int main()
{
test();
}
高级应用 (经典)
#include <iostream>
#include <string>
using namespace std;
class CPU
{
public:
virtual void calculate() = 0;
};
class VideoCard
{
public:
virtual void display() = 0;
};
class Memory
{
public:
virtual void storage() = 0;
};
class IntelCPU :public CPU
{
public:
virtual void calculate()
{
cout << "Intel 的 CPU 开始工作了" << endl;
}
};
class IntelVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "Intel 的 VideoCard 开始工作了" << endl;
}
};
class IntelMemory :public Memory
{
public:
virtual void storage()
{
cout << "Intel 的 Memory 开始工作了" << endl;
}
};
class LenovoCPU :public CPU
{
public:
virtual void calculate()
{
cout << "Lenovo 的 CPU 开始工作了" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "Lenovo 的 VideoCard 开始工作了" << endl;
}
};
class LenovoMemory :public Memory
{
public:
virtual void storage()
{
cout << "Lenovo 的 Memory 开始工作了" << endl;
}
};
class Computer
{
public:
Computer(CPU* cpu, VideoCard* vc, Memory* mem) : cpu(cpu), vc(vc), mem(mem)
{
;
}
void work()
{
cpu->calculate();
vc->display();
mem->storage();
}
~Computer()
{
if (cpu != NULL)
{
delete cpu;
cpu = NULL;
}
if (vc !=