其他未完成

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
+85
View File
@@ -0,0 +1,85 @@
// 编写一个名为 String 的类,表示字符串。重载赋值运算符 = ,使其能够执行字符串的赋值操作。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class String
{
private:
char *str;
public:
String(const char *s = NULL) // 构造函数: 默认参数为NULL,表示构造一个空字符串
{
if (s == NULL)
str = NULL;
else
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
}
String(const String &s)
{
if (s.str == NULL)
str = NULL;
else
{
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
}
~String()
{
if (str != NULL) // 如果str不为空,删除str
delete[] str;
}
public:
String &operator=(const String &s)
{
if (str == s.str)
return *this;
if (str != NULL) // 如果str不为空,则先删除str
delete[] str; // 删除的目的是为了防止内存泄漏
if (s.str == NULL) // 如果s.str为空,则str也为空
str = NULL;
else
str = new char[strlen(s.str) + 1]; // +1 是为了存放'\0'
strcpy(str, s.str);
return *this; // 返回当前对象的引用
}
void print()
{
if (str != NULL)
cout << str << endl;
else
cout << "NULL" << endl;
}
};
int main()
{
String s1("hello");
s1.print();
String *s2 = new String("world");
s2->print();
s1 = "xi'an";
s2 = &s1;
s2->print();
return 0;
}
+40
View File
@@ -0,0 +1,40 @@
// 编写一个名为 Date 的类,表示日期。重载相等运算符 == ,使其能够比较两个日期是否相等。
// 【提示】类中包含year,
// month, day三个变量。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Date
{
private:
int year, month, day;
public:
Date(int year, int month, int day) : year(year), month(month), day(day) {}
Date(const Date &d) : year(d.year), month(d.month), day(d.day) {}
~Date() {}
public:
bool operator==(const Date &d)
{
if (year == d.year && month == d.month && day == d.day)
{
return true;
}
return false;
}
};
int main()
{
Date d1(2020, 10, 1);
Date d2(2020, 10, 1);
Date d3(2020, 10, 2);
cout << (d1 == d2) << endl; // 1 表示 true
cout << (d1 == d3) << endl; // 0 表示 false
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
// 编写一个名为 Matrix 的类,表示矩阵。重载乘法运算符 *,使其能够执行两个矩阵的乘法操作。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Matrix
{
private:
int row, col;
int **matrix;
public:
Matrix(int row, int col) : row(row), col(col)
{
matrix = new int *[row];
for (int i = 0; i < row; i++)
matrix[i] = new int[col];
}
~Matrix()
{
for (int i = 0; i < row; i++)
delete[] matrix[i];
delete[] matrix;
}
public:
Matrix operator*(const Matrix &m)
{
Matrix result(row, m.col);
for (int i = 0; i < row; i++)
for (int j = 0; j < m.col; j++)
for (int k = 0; k < col; k++)
result.matrix[i][j] += matrix[i][k] * m.matrix[k][j];
return result;
}
friend ostream &operator<<(ostream &out, const Matrix &m)
{
for (int i = 0; i < m.row; i++)
{
for (int j = 0; j < m.col; j++)
out << m.matrix[i][j] << " ";
out << endl;
}
return out;
}
friend istream &operator>>(istream &in, Matrix &m)
{
for (int i = 0; i < m.row; i++)
for (int j = 0; j < m.col; j++)
in >> m.matrix[i][j];
return in;
}
};
int main()
{
}
+36
View File
@@ -0,0 +1,36 @@
// 编写一个名为 Complex 的类,表示复数。重载加法运算符 + ,使其能够执行两个复数的加法操作。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Complex
{
private:
double real, imag; // 实部和虚部
public:
Complex(double real, double imag) : real(real), imag(imag) {}
Complex(const Complex &other) : real(other.real), imag(other.imag) {}
~Complex() {}
public:
Complex operator+(const Complex &other)
{
return Complex(real + other.real, imag + other.imag);
}
void show()
{
cout << real << " + " << imag << "i" << endl;
}
};
int main()
{
Complex c1(1, 2);
Complex c2(3, 4);
Complex c3 = c1 + c2;
c3.show(); // 4 + 6i
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
// 假设有三个类,Animal(动物),Mammal(哺乳动物)和 Dog(狗)。Animal 类具有一个成员函数 eat() ,用于输出动物吃的食物。Mammal 类继承自 Animal 类,并添加了一个成员函数 giveBirth() ,用于输出哺乳动物的生育方式。Dog 类继承自 Mammal 类,并添加了一个成员函数 bark() ,用于输出狗的叫声。请在给定的类定义中完成代码,并实现相应的成员函数。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal eat" << endl;
}
};
class Mammal : public Animal
{
public:
void giveBirth()
{
cout << "Mammal give birth" << endl;
}
};
class Dog : public Mammal
{
public:
void bark()
{
cout << "Dog bark" << endl;
}
};
int main()
{
Dog dog;
dog.eat(); // Animal eat
dog.giveBirth(); // Mammal give birth
dog.bark(); // Dog bark
return 0;
}
+20
View File
@@ -0,0 +1,20 @@
// 编写一个名为 MatrixArray 的类,表示矩阵数组。重载乘法运算符 *,使其能够执行两个矩阵积操作。重载[] 运算符,实现返回某一个行的一维数组并对数组进行设置值或访问值。
// 【提示】构造函数提供行数与列数的参数,初始值为0。
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class MatrixArray
{
private:
int row, col;
int **matrix;
};
int main()
{
return 0;
}
+42
View File
@@ -0,0 +1,42 @@
// 假设有两个类,Shape(形状)和 Rectangle(矩形)。Shape 类具有一个成员函数 getArea() ,用于计算形状的面积。Rectangle 类继承自 Shape 类,并添加了两个成员变量 width(宽度)和 height(高度)。请在给定的类定义中完成代码,并实现 Rectangle 类的 getArea() 函数来计算矩形的面积。
// 【提示】Shape的类设计如下,表示为抽象类(getArea() 是纯虚函数):
// class Shape
// {
// public:
// virtual double getArea() const = 0;
// };
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Shape
{
public:
virtual double getArea() const = 0;
};
class Rectangle : public Shape
{
private:
double width, height;
public:
Rectangle(double width, double height) : width(width), height(height) {}
~Rectangle() {}
public:
double getArea() const // 重写父类的纯虚函数,const 可以保证不会修改成员变量的值
{
return width * height;
}
};
int main()
{
Shape *shape = new Rectangle(3, 4);
cout << shape->getArea() << endl; // 12
return 0;
}