其他未完成
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
// = 赋值重载
|
||||
// 注意 = 重载时,可能会调用类本身的拷贝构造函数。如果左值是没有创建的对象时,
|
||||
// 会调用类的拷贝构造函数
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
private:
|
||||
int x;
|
||||
|
||||
public:
|
||||
A(int x) : x(x)
|
||||
{
|
||||
cout << "A(int x)" << endl;
|
||||
}
|
||||
A(const A &obj)
|
||||
{
|
||||
cout << "A(A &obj)" << endl;
|
||||
this->x = obj.x;
|
||||
}
|
||||
A &operator=(const A &other)
|
||||
{
|
||||
cout << "A &operator=(A&other)" << endl;
|
||||
this->x = other.x;
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
void show()
|
||||
{
|
||||
cout << "x = " << x << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
A a1(100), a2(200);
|
||||
A a4 = 300; // A(300)
|
||||
a4 = 400; // operator=()
|
||||
|
||||
A a3 = a1; // A(a1)
|
||||
a3.show();
|
||||
|
||||
a3 = a2; // operator=() 调用赋值重载函数
|
||||
a3.show();
|
||||
|
||||
a3 = a4;
|
||||
a3.show();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
// () 函数调用重载
|
||||
// 当类对象作为函数调用时,会执行 operator()(参数列表) 函数
|
||||
//
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
private:
|
||||
int x, y;
|
||||
|
||||
public:
|
||||
A(int x, int y) : x(x), y(y) {}
|
||||
|
||||
public:
|
||||
void operator()(int a, int b)
|
||||
{
|
||||
this->x += a;
|
||||
this->y += b;
|
||||
show();
|
||||
}
|
||||
|
||||
void show()
|
||||
{
|
||||
cout << x << ", " << y << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
A a1(10, 9);
|
||||
a1.show();
|
||||
|
||||
a1(2, 3); // 类对象作为函数使用,实现 2+x,3+y
|
||||
// a1.show();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
# 定义路径变量
|
||||
SRC_DIR = ./
|
||||
OBJ_DIR = ./
|
||||
BIN_DIR = ./
|
||||
|
||||
# 定义编译器
|
||||
CC = g++
|
||||
STD = -std=c++11
|
||||
# 定义目标文件
|
||||
TARGET = $(BIN_DIR)/a.out
|
||||
# 定义源文件
|
||||
SRCS = $(wildcard $(SRC_DIR)/*.cpp)
|
||||
# OBJS = $(OBJ_DIR)/lrc.o $(OBJ_DIR)/console.o $(OBJ_DIR)/start_mplayer.o $(OBJ_DIR)/time_delay.o $(OBJ_DIR)/main.o
|
||||
OBJS = $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
|
||||
|
||||
# 编译规则
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
|
||||
$(CC) -c $< -o $@ $(STD)
|
||||
|
||||
# 链接规则
|
||||
$(TARGET): $(OBJS)
|
||||
$(CC) $^ -o $@ $(STD)
|
||||
|
||||
# 默认构建目标
|
||||
all: $(TARGET)
|
||||
|
||||
# 创建目录
|
||||
$(shell mkdir -p $(OBJ_DIR) $(BIN_DIR))
|
||||
|
||||
# 运行测试
|
||||
run: $(TARGET)
|
||||
$(TARGET)
|
||||
|
||||
# 清理规则
|
||||
clean:
|
||||
rm -rf $(OBJ_DIR)/*.o $(TARGET)
|
||||
|
||||
# 伪目标
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,12 @@
|
||||
#include "mystring.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main()
|
||||
{
|
||||
MyString s1("disen");
|
||||
cout << s1 + ",lucy" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "mystring.h"
|
||||
using namespace std;
|
||||
|
||||
ostream &operator<<(ostream &cout, const MyString &str)
|
||||
{
|
||||
cout << str.mStr; // 输出字符串 mStr
|
||||
return cout; // 返回 cout 输出流对象
|
||||
}
|
||||
istream &operator<<(istream &cin, MyString &str)
|
||||
{
|
||||
cin >> str.mStr; // 输入字符串 mStr
|
||||
return cin; // 返回 cin 输入流对象
|
||||
}
|
||||
|
||||
MyString::MyString(const char *str)
|
||||
{
|
||||
mSize = strlen(str); // 计算字符串长度
|
||||
mStr = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(mStr, str); // 拷贝字符串
|
||||
}
|
||||
|
||||
MyString::MyString(const MyString &str)
|
||||
{
|
||||
mSize = str.mSize; // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, str.mStr); // 拷贝字符串
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString::~MyString()
|
||||
{
|
||||
delete[] mStr; // 释放空间
|
||||
}
|
||||
|
||||
// 字符串拼接
|
||||
MyString &MyString::operator+(MyString &other)
|
||||
{
|
||||
mSize += other.mSize; // 计算新的字符串长度, 用原有长度加上 other 的长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, mStr); // 拷贝 mStr
|
||||
strcat(p, other.mStr); // 拼接 other.mStr
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString &MyString::operator+(const char *other)
|
||||
{
|
||||
mSize += strlen(other); // 计算新的字符串长度, 用原有长度加上 other 的长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, mStr); // 拷贝 mStr
|
||||
strcat(p, other); // 拼接 other
|
||||
delete[] this->mStr; // 删除之前的空间
|
||||
this->mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
// 字符串赋值
|
||||
MyString &MyString::operator=(MyString &other)
|
||||
{
|
||||
mSize = other.mSize; // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, other.mStr); // 拷贝字符串
|
||||
delete[] mStr; // 删除之前的空间
|
||||
mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
MyString &MyString::operator=(const char *other)
|
||||
{
|
||||
mSize = strlen(other); // 拷贝字符串长度
|
||||
char *p = new char[mSize + 1]; // 为 '\0' 留一个位置
|
||||
strcpy(p, other); // 拷贝字符串
|
||||
delete[] mStr; // 删除之前的空间
|
||||
mStr = p; // 指向新的空间
|
||||
}
|
||||
|
||||
// 字符串比较
|
||||
MyString &operator==(MyString &other)
|
||||
{
|
||||
return strcmp(mStr, other.mStr) == 0; // 比较字符串
|
||||
}
|
||||
|
||||
MyString &operator==(const char *other)
|
||||
{
|
||||
return strcmp(mStr, other) == 0; // 比较字符串
|
||||
}
|
||||
|
||||
// 读取字符串中 index 位置的字符
|
||||
char MyString::operator[](int index)
|
||||
{
|
||||
cout << "长度: " << mSize << endl;
|
||||
// index 支持负数, -1 表示倒数第一个字符
|
||||
// index 是负数,计算机存储是补码,如果直接位运算,以补码的方式 & 位运算符
|
||||
// 需要手动取反补码(~), 然后 +1
|
||||
int absIndex = (~index + 1) & 0x7fffffff; // 0x7fffffff 是有符号正整数 int 的最大值
|
||||
// 0x7fffffff = 0111 1111 1111 1111 1111 1111 1111 1111
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef __MYSTRING_H__
|
||||
#define __MYSTRING_H__
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
// 重载: 实现自定义字符串类
|
||||
class MyString
|
||||
{
|
||||
friend ostream &operator<<(ostream &cout, const MyString &str) {}
|
||||
friend istream &operator<<(istream &cout, MyString &str) {}
|
||||
|
||||
public:
|
||||
MyString(const char *str); // 有参构造函数
|
||||
MyString(const MyString &str); // 拷贝构造函数
|
||||
~MyString(); // 析构函数
|
||||
|
||||
public:
|
||||
// 字符串拼接
|
||||
MyString &operator+(MyString &other); // 重载 + 运算符
|
||||
MyString &operator+(const char *other); // 重载 + 运算符
|
||||
|
||||
// 字符串赋值
|
||||
MyString &operator=(MyString &other); // 重载 = 运算符
|
||||
MyString &operator=(const char *other); // 重载 = 运算符
|
||||
|
||||
// 字符串比较
|
||||
MyString &operator==(MyString &other); // 重载 == 运算符
|
||||
MyString &operator==(const char *other); // 重载 == 运算符
|
||||
|
||||
// 读取字符串中 index 位置的字符
|
||||
char operator[](int index); // 重载 [] 运算符
|
||||
|
||||
private:
|
||||
char *mStr; // 字符串
|
||||
int mSize; // 字符串长度
|
||||
};
|
||||
|
||||
ostream &operator<<(ostream &cout, const MyString &str);
|
||||
istream &operator<<(istream &cout, MyString &str);
|
||||
|
||||
#endif
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// 构造函数
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Person
|
||||
{
|
||||
private:
|
||||
string name;
|
||||
int age;
|
||||
|
||||
public:
|
||||
Person(const string &name, int age) : name(name), age(age) {}
|
||||
|
||||
public:
|
||||
void hi()
|
||||
{
|
||||
cout << this->name << ", " << this->age << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class Worker : public Person
|
||||
{
|
||||
private:
|
||||
int salary; // 工资
|
||||
int year; // 工作年限
|
||||
|
||||
public:
|
||||
// 子类的构造函数定义时,可以调用父类的构造函数进行父类成员的初始化
|
||||
// 使用初始化列表实现(调用父类的构造函数,传入对应参数)
|
||||
Worker(const string &name, int age, int salary, int year) : Person(name, age), salary(salary), year(year)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
void hi() // 重写了父类的函数,覆盖了父类的 hi()
|
||||
{
|
||||
// 先调用父类的 hi()
|
||||
Person::hi();
|
||||
cout << salary << ", " << year << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Worker w1 = Worker("liming", 18, 2100, 2);
|
||||
w1.hi();
|
||||
return 0;
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
int a;
|
||||
|
||||
protected:
|
||||
int b;
|
||||
|
||||
private:
|
||||
int c;
|
||||
|
||||
public:
|
||||
A(int a, int b, int c) : a(a), b(b), c(c) {}
|
||||
|
||||
public:
|
||||
void showA()
|
||||
{
|
||||
cout << "A: " << a << ", " << b << ", " << c << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
public:
|
||||
B() : A(1, 3, 2) {}
|
||||
|
||||
public:
|
||||
void showB()
|
||||
{
|
||||
showA();
|
||||
// B 类的成员函数内,可以访问父类的 public 、protected 成员
|
||||
// a, b 是父类的 public/protected 的成员变量
|
||||
cout << "B: " << a << ", " << b << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class C : protected B
|
||||
{
|
||||
public:
|
||||
C() : B() {}
|
||||
|
||||
public:
|
||||
void showC()
|
||||
{
|
||||
showA();
|
||||
showB();
|
||||
cout << "C: " << a << ", " << b << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class D : private C
|
||||
{
|
||||
public:
|
||||
D() : C() {}
|
||||
|
||||
public:
|
||||
void showD()
|
||||
{
|
||||
showA();
|
||||
cout << "D: " << a << ", " << b << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class E : public D
|
||||
{
|
||||
public:
|
||||
E() : D() {}
|
||||
|
||||
public:
|
||||
void showE()
|
||||
{
|
||||
showA();
|
||||
// cout << "E: " << a << ", " << b << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
// B b1;
|
||||
// b1.showA();
|
||||
// b1.showB();
|
||||
|
||||
// C c1;
|
||||
// c1.showC();
|
||||
|
||||
// D d1;
|
||||
// d1.showD();
|
||||
|
||||
E e1;
|
||||
e1.showD();
|
||||
|
||||
return 0;
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
|
||||
private:
|
||||
int y;
|
||||
|
||||
public:
|
||||
A(int x, int y)
|
||||
// A()
|
||||
{
|
||||
cout << "A(int x,int y)" << endl;
|
||||
// cout << "A()" << endl;
|
||||
}
|
||||
~A()
|
||||
{
|
||||
cout << "~A()" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
private:
|
||||
int y;
|
||||
|
||||
public:
|
||||
B() : A(1, 2)
|
||||
{
|
||||
cout << "B()" << endl;
|
||||
}
|
||||
~B()
|
||||
{
|
||||
cout << "~B()" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "A size is " << sizeof(A) << endl;
|
||||
cout << "B size is " << sizeof(B) << endl;
|
||||
|
||||
B b1; // A() B() ~B() ~A()
|
||||
|
||||
// A a;
|
||||
|
||||
return 0;
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
// 继承中同名成员的处理方法
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class A
|
||||
{
|
||||
public:
|
||||
int x;
|
||||
A(int x) : x(x) {}
|
||||
void show()
|
||||
{
|
||||
cout << "x = " << x << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class B : public A
|
||||
{
|
||||
private:
|
||||
int x;
|
||||
|
||||
public:
|
||||
// B(int x) : A(x)
|
||||
// {
|
||||
// // 就近原则: this->x 是 B 类的,不是父类 A 的
|
||||
// this->x = x + 20;
|
||||
// }
|
||||
B(int x) : A(x), x(x + 20)
|
||||
{
|
||||
}
|
||||
|
||||
void showX()
|
||||
{
|
||||
cout << "x = " << x << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
B b1(10);
|
||||
b1.show(); // 从 A 类继承过来的方法,打印的是 A 类的 x
|
||||
b1.showX(); // B 类自己的,打印自己的 a
|
||||
return 0;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user