Compare commits

..

No commits in common. "aae9aba049f532877a3699ee7c392d2cab18c7bf" and "e5577ed3e5c0a22119b3e476a3944869d5e2dffa" have entirely different histories.

26 changed files with 0 additions and 1163 deletions

View File

@ -1,20 +0,0 @@
// 编写一个模板函数 maxValue接受两个参数并返回其中较大的值。
// 【提示】T maxVal(T &v1, T &v2)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T maxVal(T &v1, T &v2)
{
return v1 > v2 ? v1 : v2;
}
int main()
{
int a = 2, b = 5;
cout << "更大的值是: " << maxVal(a, b) << endl;
return 0;
}

View File

@ -1,29 +0,0 @@
// 编写一个模板函数 maxValueInArray接受一个数组和数组的大小并返回数组中的最大值。
//【提示】T maxValueInArray(T arr[], int size)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T maxValueInArray(T arr[], int size)
{
T max = arr[0];
for (int i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
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]);
cout << "arr1 的最大值是: " << maxValueInArray(arr1, size1) << endl;
cout << "arr2 的最大值是: " << maxValueInArray(arr2, size2) << endl;
return 0;
}

View File

@ -1,40 +0,0 @@
// 创建一个基类 Person其中包含一个纯虚函数 display() ,用于显示人的信息。从 Person 派生出 Student 类和 Teacher 类,分别实现不同类型人的信息显示。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Person
{
public:
virtual void display() = 0;
};
class Student : public Person
{
public:
// 子类的 virtual 函数可以不写 virtual 关键字
virtual void display()
{
cout << "Student" << endl;
}
};
class Teacher : public Person
{
public:
virtual void display()
{
cout << "Teacher" << endl;
}
};
int main()
{
Person *p = new Student();
p->display();
p = new Teacher();
p->display();
return 0;
}

View File

@ -1,39 +0,0 @@
// 创建一个基类 Animal其中包含一个纯虚函数 move() ,用于描述动物的移动方式。从 Animal 派生出 Bird 类和 Fish 类,分别实现不同类型动物的移动方式。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
public:
virtual void move() = 0;
};
class Bird : public Animal
{
public:
void move()
{
cout << "Bird" << endl;
}
};
class Fish : public Animal
{
public:
void move()
{
cout << "Fish" << endl;
}
};
int main()
{
Animal *p = new Bird;
p->move();
p = new Fish;
p->move();
return 0;
}

View File

@ -1,39 +0,0 @@
// 创建一个基类 Shape其中包含一个纯虚函数 draw() ,用于绘制形状。从 Shape 派生出 Rectangle 类和 Circle 类,分别实现不同形状的绘制方法
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw()
{
cout << "Rectangle" << endl;
}
};
class Circle : public Shape
{
public:
void draw()
{
cout << "Circle" << endl;
}
};
int main()
{
Shape *p = new Rectangle;
p->draw();
p = new Circle;
p->draw();
return 0;
}

View File

@ -1,55 +0,0 @@
// 创建一个基类 Shape其中包含一个纯虚函数 perimeter() ,用于计算形状的周长。从 Shape 派生出 Triangle 类和 Square 类,分别实现计算三角形和正方形的周长的函数。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
// 用于计算形状的周长
virtual double perimeter() = 0;
};
class Triangle : public Shape
{
private:
double a, b, c;
public:
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
public:
double perimeter()
{
return a + b + c;
}
};
class Square : public Shape
{
private:
double a;
public:
Square(double a) : a(a) {}
public:
double perimeter()
{
return 4 * a;
}
};
int main()
{
Shape *p = new Triangle(3, 4, 5);
cout << "三角形的周长是: " << p->perimeter() << endl;
p = new Square(5);
cout << "正方形的周长是: " << p->perimeter() << endl;
return 0;
}

View File

@ -1,92 +0,0 @@
// 继第6题 基于多态方式定义一个全局函数排序函数接收Shape类的数组并按形状的周长进行从大到少排序。
// 【提示】全局函数排序函数 void sort(Shape *arr, int size)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
// 用于计算形状的周长
virtual double perimeter() = 0;
};
class Triangle : public Shape
{
private:
double a, b, c;
public:
Triangle(double a, double b, double c) : a(a), b(b), c(c) {}
public:
double perimeter()
{
return a + b + c;
}
};
class Square : public Shape
{
private:
double a;
public:
Square(double a) : a(a) {}
public:
double perimeter()
{
return 4 * a;
}
};
void sort(Shape *arr[], int size) // 这里为什么传指针数组?因为 Shape 是抽象类,不能实例化对象,只能通过指针来操作
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
// 比较形状的周长
if (arr[j]->perimeter() < arr[j + 1]->perimeter())
{
// 交换位置
swap(arr[j], arr[j + 1]);
}
}
}
}
int main()
{
Shape *p = new Triangle(3, 4, 5);
Shape *q = new Square(5);
Shape *r = new Triangle(6, 8, 10);
Shape *s = new Square(10);
Shape *t = new Triangle(5, 12, 13);
Shape *u = new Square(13);
// Shape *arr[6];
// arr[0] = p;
// arr[1] = q;
Shape *arr[] = {p, q, r, s, t, u}; // arr作为一个指针数组存了6个指向 Shape 类对象的指针
int len = sizeof(arr) / sizeof(arr[0]);
cout << "排序前:" << endl;
for (int i = 0; i < len; i++)
{
cout << arr[i]->perimeter() << endl;
}
sort(arr, len);
cout << "排序后:" << endl;
for (int i = 0; i < len; i++)
{
cout << arr[i]->perimeter() << endl;
}
return 0;
}

View File

@ -1,44 +0,0 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T &maxVal(T &a, T &b)
{
return a > b ? a : b;
}
class A
{
friend A &maxVal<A>(A &a, A &b); // 模板特化的友元函数,用于访问私有成员
private:
int x;
public:
A(int x) : x(x) {}
void show()
{
cout << "x = " << x << endl;
}
};
// 具体化函数模版的重载
template <>
A &maxVal<A>(A &a, A &b) // 模板特化
{
if (a.x > b.x)
return a;
return b;
}
int main()
{
A a1(20), a2(50);
// A &b = maxVal<A>(a1, a2);
A &b = maxVal(a1, a2);
b.show();
return 0;
}

View File

@ -1,35 +0,0 @@
// 模版类与友元
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class A
{
// 内部实现的友元函数(全局性的友元函数,类的外部可以直接访问 showIn(x) ,不需要类对象调用 a.showIn()
// 接收当前模版类的引用时,可以指定当前模版类的类型
friend void showIn(A<T> &a)
{
cout << a.item << endl;
}
private:
T item;
public:
A(T item)
{
this->item = item;
}
// public:
};
int main()
{
A<int> a(20);
showIn(a);
return 0;
}

View File

@ -1,45 +0,0 @@
// 模版类与友元
// 外部实现的友元函数
// 是函数模板时,必须在类定义之前声明。在类中声明友元全局函数时,必须使用<> 空泛型表示此函数是函数模板。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class A;
template <typename T>
void showOut(A<T> &a);
template <typename T>
class A
{
// 外部实现的友元函数,它有自己的模版类型,不需要指定当前模版类的类型
// <>空泛型表示外部是函数模版,模版的泛型同当前类的泛型
// 要求: 必须之前先声明此函数为构造函数
friend void showOut<>(A<T> &a);
private:
T item;
public:
A(T item)
{
this->item = item;
}
};
template <typename T>
void showOut(A<T> &a) // 全局友元函数
{
cout << "out item is: " << a.item << endl;
}
int main()
{
A<int> a(30);
showOut(a);
return 0;
}

View File

@ -1,42 +0,0 @@
// 友元函数模板
/*
:
template <typename T>
friend (T &);
*/
#include <iostream>
using namespace std;
template <typename T>
class A
{
// 友元函数模板,声明的友元函数名不需要加<>空泛型
template <typename U>
friend void show(A<U> &a);
private:
T item;
public:
A(T item)
{
this->item = item;
}
};
template <typename U>
void show(A<U> &a)
{
cout << "template friend item is: " << a.item << endl;
}
int main()
{
A<int> a(15);
show(a);
A<float> b(15.5);
show(b);
return 0;
}

View File

@ -1,92 +0,0 @@
// 类模板的应用
// 设计一个数组模板类(MyArray),完成对不同类型元素的管理
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class MyArray
{
private:
int index; // 当前元素的位置
T *arr; // 指向数组的指针
int capacity; // 最大容量
int size; // 当前容量
public:
MyArray(int capacity)
{
this->capacity = capacity; // 最大容量
this->size = 0; // 当前容量为0
this->index = 0; // 当前元素位置为0
this->arr = new T[capacity]; // 申请内存
}
MyArray(const MyArray<T> &other)
{
this->index = other.index; // 当前元素的位置
this->capacity = other.capacity; // 最大容量
this->arr = new T[this->capacity]; // 申请内存
for (int i = 0; i <= this->capacity; i++)
{
this->arr[i] = other.arr[i]; // 拷贝数据
}
this->size = other.size; // 当前容量
}
~MyArray()
{
delete[] this->arr; // 释放内存
}
public:
T &get(int index)
{
return arr[index];
}
T &operator[](int index)
{
return arr[index];
}
// & 是为了链式编程
MyArray<T> &push(T item)
{
if (index < capacity)
{
arr[index++] = item; // 将元素放入数组
size++; // 当前容量加1
}
return *this;
}
T pop()
{
if (index > 0)
{
size--; // 当前容量减1
return arr[--index];
}
else
return NULL;
}
int getSize()
{
return size;
}
};
int main()
{
MyArray<int> a1 = MyArray<int>(20);
a1[0] = 10;
a1[1] = 20;
MyArray<int> a2 = a1;
cout << "a2[0] = " << a2[0] << ", a2[1] = " << a2[1] << endl;
a1.push(30).push(40).push(50);
for (int i = 0; i <= 3; i++)
{
cout << a1.pop() << endl;
}
return 0;
}

View File

@ -1,31 +0,0 @@
// 类型转换
#include <iostream>
using namespace std;
int test1()
{
float f = 97.5;
char c = static_cast<char>(f);
cout << "c = " << (int)c << endl;
}
int test2()
{
float f = 97.5;
// int *p = &f; // 指针转换不允许跨类型
// int *p = static_cast<int *>(&f);
// int *p = (int *)&f;
// int *p = dynamic_cast<int *>(&f);
float *pf = &f;
// double *p = const_cast<double *>(pf);
int *p = reinterpret_cast<int *>(pf); // 重新解释类型转换,不安全
cout << "*p = " << *p << endl;
}
int main()
{
test2();
return 0;
}

View File

@ -1,42 +0,0 @@
// 静态类型转换
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class A
{
public:
int x;
A(int x) : x(x) {}
};
class B : public A
{
public:
int y;
B(int x, int y) : A(x), y(y) {}
};
int main()
{
A *a = new A(5);
B *b = new B(10, 20);
A *c = static_cast<A *>(b); // 静态上行转换,子类指针向父类指针转换
// cout << "a.x = " << a->x << endl;
// cout << "b.x = " << b->x << ", b.y = " << b->y << endl;
// cout << "b = " << b << endl;
// cout << "c = " << c << endl;
// cout << "c.x = " << c->x << endl;
// B *b2 = static_cast<B *>(a); // 下行转换,父类指针向子类指针转换
// B *b2 = (B *)a; // 强制下行转换,不安全
// cout << "b2.x = " << b2->x << ", b2.y = " << b2->y << endl;
delete a;
delete b;
return 0;
}

View File

@ -1,71 +0,0 @@
// 动态类型转换
#include <iostream>
using namespace std;
class A
{
protected:
int x;
public:
A(int x) : x(x) {}
void show()
{
cout << "A x = " << x << endl;
}
};
class B : public A
{
public:
B(int x) : A(x) {}
void print()
{
cout << "B print x = " << x << endl;
}
};
class C
{
};
void test1()
{
int a = 65;
// 动态转化不支持基本数据类型之间的转换
// char c = dynamic_cast<char>(a);
// char *c = dynamic_cast<char *>(&a);
// cout << "c = " << c << endl;
}
void test2()
{
A a1 = A(10);
B b1 = B(20);
// 上行转换
A &a2 = dynamic_cast<A &>(b1);
a2.show();
// a2.print(); // 不存在 print 成员函数
b1.print();
// 下行转换: 动态转换不支持(安全)
// B &b2 = dynamic_cast<B &>(a2);
// b2.print();
B &b3 = static_cast<B &>(a2);
b3.print();
}
// 不相关类型的转换: 不可动态转换
void test3()
{
A *a = new A(20);
// C *p = dynamic_cast<C *>(a);
}
int main()
{
test3();
return 0;
}

View File

@ -1,42 +0,0 @@
// const 转换
#include <iostream>
using namespace std;
void change(const int *p, int value)
{
int *q = const_cast<int *>(p); // 转换为非 const 指针
*q = value;
}
int main()
{
int a = 10;
const int b = 30; // 存储在符号表,在栈中没有空间
// 1. 将变量转化为 const 引用变量【可以】
const int &a1 = const_cast<const int &>(a);
// 2. 将变量地址转化为 const 指针变量【可以】
const int *a2 = const_cast<const int *>(&a);
// 3. 去掉 b 的 const 修饰
// b 在取地址时在占中开辟空间b1 指向空间
// int b1 = const_cast<int>(b);【不可以】
int &b1 = const_cast<int &>(b);
b1 = 100;
cout << "b = " << b << endl;
cout << "b1 = " << b1 << endl;
// 4. 去掉 a2 的 const 修饰
int *a3 = const_cast<int *>(a2);
*a3 = 200;
cout << "*a2 = " << *a2 << endl;
cout << "*a3 = " << *a3 << endl;
// 5. 将 a3 转化为 const 指针变量【可以】
const int *a4 = const_cast<const int *>(a3);
// *a4 = 100;
cout << "*a4 = " << *a4 << endl;
cout << "*a3 = " << *a3 << endl;
return 0;
}

View File

@ -1,39 +0,0 @@
#include <iostream>
using namespace std;
class A
{
public:
int x;
void show()
{
cout << "A show() x = " << x << endl;
}
};
class B
{
public:
int x;
void show()
{
cout << "B show() x = " << x << endl;
}
};
int main()
{
A *a = new A();
a->x = 100;
B *b = new B();
b->x = 200;
// B *b2 = static_cast<B *>(a); // error
// B *b3 = dynamic_cast<B *>(a); // error
B *b4 = reinterpret_cast<B *>(a); // 输出: B show() x = 100
b4->show();
return 0;
}

View File

@ -1,40 +0,0 @@
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
T &maxVal(T &a, T &b)
{
return a > b ? a : b;
}
class A
{
private:
int x;
public:
A(int x) : x(x) {}
void show()
{
cout << "x = " << x << endl;
}
public:
// 重载运算符>,解决函数模版的局限性,不用具体化函数模版
bool operator>(const A &a) const
{
return this->x > a.x;
}
};
int main()
{
A a1(20), a2(50);
// A &b = maxVal<A>(a1, a2);
A &b = maxVal(a1, a2);
b.show();
return 0;
}

View File

@ -1,53 +0,0 @@
// 类模版
// 类模版是成员变量的数据类型泛化
// 类模版的声明作用于整个类,而不是单个成员,即类的内部的任何位置都可以使用
// 如: 定义长方形的图形类
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// 类模版
template <typename T>
class Rectangle
{
private:
T width, height; //矩形的宽和高
public:
Rectangle(T w, T h) : width(w), height(h) {}
public:
void draw()
{
cout << "绘制矩形"
<< "宽: " << width << "高: " << height << endl;
}
void length()
{
cout << "矩形周长: " << 2 * (width + height) << endl;
}
};
int main()
{
cout << "r1: " << endl;
Rectangle<int> r1(10, 20);
r1.draw();
r1.length();
cout << "r2: " << endl;
Rectangle<double> r2(10.53, 20.25);
r2.draw();
r2.length();
cout << "r3: " << endl;
Rectangle<float> r3(10.2f, 20.2f);
r3.draw();
r3.length();
return 0;
}

View File

@ -1,55 +0,0 @@
// 类模版作为函数参数
// 函数模版的参数可以是类模版
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// 类模版
template <typename T>
class Rectangle
{
private:
T width, height; //矩形的宽和高
public:
Rectangle(T w, T h) : width(w), height(h) {}
public:
void draw()
{
cout << "绘制矩形"
<< "宽: " << width << "高: " << height << endl;
}
void length()
{
cout << "矩形周长: " << 2 * (width + height) << endl;
}
};
// 类模版作为函数参数
// 将类模版作为函数参数,可以将类模版的数据类型泛化
template <typename T>
void drawShape(Rectangle<T> &r)
{
r.draw();
r.length();
}
int main()
{
cout << "r1: " << endl;
Rectangle<int> r1(10, 20);
drawShape<int>(r1); // 显式指定模版参数
cout << "r2: " << endl;
Rectangle<double> r2(10.53, 20.25);
drawShape<>(r2); // 空<>表示自动推导
cout << "r3: " << endl;
Rectangle<float> r3(10.2f, 20.2f);
drawShape(r3); // 表示自动推导
return 0;
}

View File

@ -1,46 +0,0 @@
// 类模版派生普通类
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class Shape
{
protected:
T iPerimeter;
public:
// virtual double area() = 0;
virtual T perimeter() { return iPerimeter; } // 周长
};
class Rectangle : public Shape<int>
{
private:
int width, height;
public:
Rectangle(int w, int h) : width(w), height(h) { iPerimeter = 2 * (w + h); }
};
class Triangle : public Shape<float>
{
private:
float a, b, c;
public:
Triangle(float a, float b, float c) : a(a), b(b), c(c) { iPerimeter = a + b + c; }
};
int main()
{
Rectangle r1(10, 20);
cout << "r1周长: " << r1.perimeter() << endl;
Triangle t1(3.0f, 4.0f, 5.0f);
cout << "t1周长: " << t1.perimeter() << endl;
return 0;
}

View File

@ -1,37 +0,0 @@
// 类模版派生普通类
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class Shape
{
public:
virtual T perimeter() = 0; // 周长
};
// 类模版派生类模版
// 派生的子类模版不能使用父类模版的成员变量,因为父类模版的成员变量是在编译时期确定的,而子类模版是在运行时期确定的
template <typename T>
class Rectangle : public Shape<T>
{
private:
T width, height;
public:
Rectangle(T w, T h) : width(w), height(h) {}
virtual T perimeter() { return 2 * (width + height); } // 周长
};
int main()
{
// 必须使用 int 来指定类型
Rectangle<int> r1(10, 20);
cout << "r1周长: " << r1.perimeter() << endl;
Rectangle<float> r2(10.2f, 20.2f);
cout << "r2周长: " << r2.perimeter() << endl;
return 0;
}

View File

@ -1,47 +0,0 @@
// 类模版类内部实现
// 在类模版内部的成员函数中,可以使用泛型成员变量
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath> // sqrt
using namespace std;
template <typename T1, typename T2>
class Point
{
private:
T1 x;
T2 y;
public:
Point(T1 x, T2 y) : x(x), y(y) {}
public:
// ? T1, T2 是哪一个对象的泛型的具体化?是当前调用的对象的泛型
// 此函数要求两个对象的泛型必须一致
float distance(Point<T1, T2> &other)
{
return sqrt((this->x - other.x) * (this->x - other.x) + (this->y - other.y) * (this->y - other.y));
}
};
int main()
{
// 类的泛型必须指定类型
Point<float, float> p1(2, 3);
Point<float, float> p2(4, 5.5f);
cout << "p1和p2的距离: " << p1.distance(p2) << endl;
Point<int, int> p3(2, 3);
Point<int, int> p4(4, 5.5f);
cout << "p3和p4的距离: " << p3.distance(p4) << endl;
// 只有同类型的对象才能调用成员函数
// 调用成员函数时,泛型的具体化是当前调用的对象的泛型
Point<int, float> p5(2, 3);
Point<int, float> p6(4, 5.5f);
cout << "p5和p6的距离: " << p5.distance(p6) << endl;
return 0;
}

View File

@ -1,44 +0,0 @@
// 类模版类的外部实现
// 类外部实现成员函数时,需要指定类泛型的类型
// 再次声明泛型类型(转成函数模版)
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
template <typename T>
class Point
{
private:
T x, y;
public:
Point(T x, T y);
void show();
};
template <typename T>
Point<T>::Point(T x, T y) : x(x), y(y)
{
}
template <typename T>
void Point<T>::show()
{
cout << "x = " << x << ", y = " << y << endl;
}
int main()
{
cout << "p1: ";
Point<int> p1(10, 20);
p1.show();
cout << "p2: ";
Point<float> p2(10.2, 14.5);
p2.show();
return 0;
}

View File

@ -1,30 +0,0 @@
#ifndef __MYPOINT_H__
#define __MYPOINT_H__
#include <iostream>
using namespace std;
template <typename T>
class Point
{
private:
T x, y;
public:
Point(T x, T y);
void show();
};
template <typename T>
Point<T>::Point(T x, T y) : x(x), y(y)
{
}
template <typename T>
void Point<T>::show()
{
cout << "x = " << x << ", y = " << y << endl;
}
#endif // __MYPOINT_H__

View File

@ -1,14 +0,0 @@
#include "MyPoint.h"
int main()
{
cout << "p1: ";
Point<int> p1(10, 20);
p1.show();
cout << "p2: ";
Point<float> p2(10.2, 14.5);
p2.show();
return 0;
}