C++学习记录(八)纯虚函数、虚析构、模板函数、模板类、栈模板

这是第八天的学习记录。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 class A
 6 {
 7 public:
 8     A() { cout << "A" << endl; }
 9     A(const A& a) { cout << "copy A" << endl; }
10     //virtual ~A() { cout << "~A" << endl; }
11     ~A() { cout << "~A" << endl; }
12 
13     //virtual void show() = 0;
14     //virtual void showInfo() { cout << "A info" << endl; }
15     void showInfo() { cout << "A info" << endl; }
16 };
17 class B : public A
18 {
19     int *p;
20 public:
21     B()
22     {
23         p = new int[1024];
24         cout << "B" << endl;
25     }
26     ~B()
27     {
28         delete []p;
29         cout << "~B" << endl;
30     }
31 
32     //void show() override { cout << "hello" << endl; }
33     //void showInfo() override { cout << "B info" << endl; }
34 };
35 
36 int main()
37 {
38     // -1- 纯虚函数,没有函数体的虚函数,含有纯虚函数的类叫抽象类
39     // 主要用做基类使用virtual 返回值 函数名(形参) = 0;
40     // 这种抽象类不可以产生对象,但是可以定义指针,子类可对其定义
41     //A* a = new B;
42     //a->show();
43 
44     // -2- 虚析构,在基类的虚构函数前叫virtual
45     // 子类析构时,会自动调用基类中的析构
46     // 主要用于多态发生时,无法调用子类析构而出现的内存泄露
47     //a->showInfo();
48     //delete a;
49 
50     // -3- 子类的初始化列表
51     // 子类初始化列表不可以对父类初始化
52 
53     // -4- 拷贝构造和虚指针
54     // 两个不同类之间的赋值,会发生拷贝构造函数
55 
56     // -5- 临时对象,两个对象不会指向同一个空间
57     //A a1 = B();         // a1通过A的拷贝构造产生,B()为临时构造,生命周期为本行
58     //const A& a2 = B();
59 
60     // -6- 常引用
61 
62     // -7- 右值引用
63     // 临时对象又叫右值
64     A&& a3 = B();
65     a3.showInfo();
66 
67     //cout << "Hello World!" << endl;
68     return 0;
69 }
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 // 范型函数模板
 6 template<class T>
 7 T Add(T t1, T t2)
 8 {
 9     return t1 + t2;
10 };
11 // 特化函数模板,一般不进行特化
12 template<class T>
13 T Add(int t1, int t2)
14 {
15     return t1 + t2;
16 };
17 
18 // 类模板
19 template<class A, class B, class C>
20 class Base
21 {
22     A a;
23     B b;
24     C c;
25 public:
26     void show(A a, B b) { cout << "show" << endl;}
27 };
28 template<class A, class B, class C, class D>
29 class Son : public Base<A, B, C>        // 模板写类的继承
30 {
31 public:
32     void showInfo(A a, D d) { cout << "show Son" << endl; }
33 };
34 
35 int main()
36 {
37     // -1- 函数模板,类模板
38     cout << Add<int>(2,3) << endl;
39     cout << Add<float>(4,5) << endl;
40     cout << Add(6,7) << endl;           // 不推荐使用这种
41 
42     // -2- 调用类模板去定义对象是,一定要显示指定具体值
43     Base<int,float,string> a;
44     a.show(1, 2.1);
45 
46     Son<int,float,string,double> b;
47     b.showInfo(2,3.2);
48 
49     cout << "Hello World!" << endl;
50     return 0;
51 }
 1 #ifndef MY_STACK_H
 2 #define MY_STACK_H
 3 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 template<class T>
 8 class My_stack
 9 {
10 private:
11     int capacity;
12     int size;
13     T* data;
14 public:
15     My_stack(int c = 10);
16     ~My_stack();
17 
18     bool push(T t);
19     bool pop();
20     T top();
21     bool empty();
22 
23 };
24 
25 template<class T>
26 My_stack<T>::My_stack(int c):size(0),capacity(c)
27 {
28     data = new T[c];
29 }
30 
31 template<class T>
32 My_stack<T>::~My_stack()
33 {
34     delete []data;
35 }
36 
37 template<class T>
38 bool My_stack<T>::push(T t)
39 {
40     if (size > capacity) { return false;}
41     data[size] = t;
42     size++;
43     return true;
44 }
45 
46 template<class T>
47 bool My_stack<T>::pop()
48 {
49     if (size == 0) { return false; }
50     size--;
51 }
52 
53 template<class T>
54 T My_stack<T>::top()
55 {
56     if (size > 0)
57     {
58         T temp = data[size - 1];
59         return temp;
60     }
61 }
62 
63 template<class T>
64 bool My_stack<T>::empty()
65 {
66     if (size == 0) { return true; }
67     return false;
68 }
69 
70 #endif // MY_STACK_H
 1 #include <iostream>
 2 #include "my_stack.h"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     My_stack<int> stack;
 8     stack.push(1);
 9     stack.push(2);
10     stack.push(3);
11     // 先入后出
12     for (int i = 0; !stack.empty(); i++)
13     {
14         cout << stack.top() << endl;
15         stack.pop();
16     }
17     //cout << "Hello World!" << endl;
18     return 0;
19 }

 

posted @ 2022-05-12 21:11  Z_He  阅读(72)  评论(0编辑  收藏  举报