其他未完成

This commit is contained in:
2023-08-14 17:20:39 +08:00
parent 9290e4c051
commit 4c986179b4
65 changed files with 2650 additions and 11 deletions
+44
View File
@@ -0,0 +1,44 @@
// 多继承(同属一个超级父类)产生的问题
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class A
{
public:
int x;
};
class B : virtual public A
{
public:
B()
{
x = 50;
}
};
class C : virtual public A
{
public:
C()
{
x = 100;
}
};
class D : public C, public B
{
};
int main()
{
D d1;
cout << d1.x << endl;
d1.x = 0; // x 是从 B 来的,还是从 C 来的? C ,使用就近原则
cout << d1.x << endl;
return 0;
}
+66
View File
@@ -0,0 +1,66 @@
// 初尝多态
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// 抽象类:
// 至少有一个纯虚函数的类
// 抽象类不能实现对象
class AbstractCaculator // 定义计算器
{
protected: // 使用保护权限(目的是使子类可以访问,而其他类不可访问)
int a, b;
public:
void setA(int a) { this->a = a; }
void setB(int b) { this->b = b; }
public:
// 扩展功能,让子类去实现
virtual int getResult() = 0; // 纯虚函数,声明函数并赋值为 0
};
class AddCa : public AbstractCaculator
{
// 必须要去实现纯虚函数
int getResult()
{
return a + b;
}
};
class SubCa : public AbstractCaculator
{
// 必须要去实现纯虚函数
int getResult()
{
return a - b;
}
};
// 多态的体现:
// 1) 定义函数时,形参的类型为父类对象的指针
// 2) 调用函数时,实参的类型为子类对象的指针
int Result(AbstractCaculator *caculator, int a, int b)
{
caculator->setA(a);
caculator->setB(b);
return caculator->getResult();
}
int main()
{
// 抽象类型不能去定义对象
// error: cannot declare variable a1 to be of abstract type AbstractCaculator
// AbstractCaculator a1;
AddCa *a2 = new AddCa;
cout << Result(a2, 10, 20) << endl;
cout << Result(new SubCa, 10, 20) << endl; // new SubCa 返回的是一个临时对象,不需要 delete
delete a2;
// delete new SubCa; // error: cannot delete expression of type SubCa
return 0;
}
+41
View File
@@ -0,0 +1,41 @@
// 向上类型转换
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Printer
{
public:
void open()
{
cout << "打印机正在开机..." << endl;
}
};
class DellPrinter : public Printer
{
void open()
{
cout << "Dell打印机..." << endl;
}
};
class HuaweiPrinter : public Printer
{
void open()
{
cout << "Huawei打印机..." << endl;
}
};
int main()
{
DellPrinter dellprinter;
// 多态: 通过父类的引用或指针调用子类对象的成员
// 子类对象自动向上转换位父类的类型
Printer &printer = dellprinter;
printer.open(); // 输出: 打印机正在开机...
return 0;
}
+21
View File
@@ -0,0 +1,21 @@
// 将父类的函数改为虚函数
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
clacc Printer
{
public:
virtual void open()
{
cout << ""
}
}
int main()
{
return 0;
}
+88
View File
@@ -0,0 +1,88 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
protected:
string name;
float price;
public:
Animal(const string &name, float price)
{
this->name = name;
this->price = price;
}
virtual void buy(); // 类内声明的虚函数
};
// 类外实现类中的虚函数
void Animal::buy()
{
cout << "小宠物 " << name << " 卖了 " << price << endl;
}
class Dog : public Animal
{
private:
int age;
public:
Dog(string name, float price, int age) : Animal(name, price), age(age)
{
}
virtual void buy() // 重写父类的虚函数
{
cout << age << " 岁小狗 " << name << " 卖了 " << price << endl;
}
void eat() // 扩展的新功能
{
cout << name << " 喝酒" << endl;
}
};
class Cat : public Animal
{
private:
int age;
public:
Cat(string name, float price) : Animal(name, price)
{
}
virtual void buy() // 重写父类的虚函数
{
cout << "小猫 " << name << " 卖了 " << price << endl;
}
void eat() // 扩展的新功能
{
cout << name << " 吃鱼" << endl;
}
};
class AnimalShop
{
public:
// 多态应用之一
void buy(Animal *animal)
{
cout << "-----动物之家正在出售小宠物-----" << endl;
animal->buy();
cout << "-----出售结束-----" << endl;
delete animal; // 释放堆区空间
}
};
int main()
{
AnimalShop shop;
Dog dog("旺财", 1000, 3);
// 多态的应用
shop.buy(&dog);
shop.buy(new Cat("小花", 500));
return 0;
}
+49
View File
@@ -0,0 +1,49 @@
// 函数模版
/*
template <class T>
函数返回值 函数名(T &n1, T &n2)
{
// 函数体
}
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
namespace my
{
// 推荐使用 typename 而不是 class,因为 typename 更通用
template <typename T>
void swap(T &a, T &b)
{
a ^= b;
b ^= a;
a ^= b;
}
// 函数模版重载: 用于不同类型的参数
template <typename T1, typename T2>
void swap(T1 &a, T2 &b)
{
a ^= b;
b ^= a;
a ^= b;
}
}
int main()
{
int a = 2, b = 6;
cout << a << ", " << b << endl;
my::swap(a, b); // 自动推演类型: swap<int>(a, b)
cout << a << ", " << b << endl;
char c = 30;
cout << a << ", " << (int)c << endl;
my::swap(a, c); // 自动推演类型
cout << a << ", " << (int)c << endl;
return 0;
}
+55
View File
@@ -0,0 +1,55 @@
// 使用函数模版实现对 char 和 int 数组的排序和打印
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
void sort(T arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
int min = i;
for (int j = i + 1; j < size; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
}
if (min != i)
{
arr[i] ^= arr[min];
arr[min] ^= arr[i];
arr[i] ^= arr[min];
// T temp = arr[i];
// arr[i] = arr[min];
// arr[min] = temp;
}
}
}
template <typename T>
void printArr(T arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
int arr1[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 0};
char arr2[] = {'a', 'c', 'e', 'G', 'I', 'b', 'd', 'f', 'h', 'j'};
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int size2 = sizeof(arr2) / sizeof(arr2[0]);
sort(arr1, size1);
sort(arr2, size2);
printArr(arr1, size1);
printArr(arr2, size2);
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
// 多态本质:
// 父类引用或指针指向子类对象,通过父类指针或引用来(操作子类对象)调用子类中重写的成员函数
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
public:
virtual void speak()
{
cout << "动物在唱歌..." << endl;
}
};
class Dog : public Animal
{
public:
void speak()
{
cout << "狗在唱歌..." << endl;
}
};
void DoBusiness(Animal &animal)
{
animal.speak();
}
int main()
{
Dog dog;
DoBusiness(dog);
return 0;
}