Commit fb82929b authored by Clark Lin's avatar Clark Lin
Browse files

Merge branch 'learning' into 'main'

Merge Basic and OOP

See merge request !2
parents 2b9afd97 70a987cf
#include <iostream>
using namespace std;
// 基类
// 如果类中至少有一个函数被声明为纯虚函数,则这个类就是抽象类。
// 设计抽象类(通常称为 ABC)的目的,是为了给其他类提供一个可以继承的适当的基类。
// 抽象类不能被用于实例化对象,它只能作为接口使用。
// 如果试图实例化一个抽象类的对象,会导致编译错误。
class Shape {
public:
// 提供接口框架的纯虚函数
virtual int getArea() = 0;
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// 派生类 矩形
class Rectangle: public Shape {
public:
// 基类的纯虚函数,在派生类中实现了具体逻辑
int getArea() {
return (width * height);
}
};
// 派生类 三角形
class Triangle: public Shape {
public:
// 基类的纯虚函数,在派生类中实现了具体逻辑
int getArea() {
return (width * height)/2;
}
};
int main(void) {
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// 输出对象的面积
cout << "Total Rectangle area: " << Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// 输出对象的面积
cout << "Total Triangle area: " << Tri.getArea() << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class printData {
public:
// 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明
// 但是它们的参数列表和定义(实现)不相同。
// 当您调用一个重载函数或重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。
// 选择最合适的重载函数或重载运算符的过程,称为重载决策。
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(string c) {
cout << "Printing character: " << c << endl;
}
};
int main(void) {
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
public:
double getVolume(void) {
return length * breadth * height;
}
void setLength(double len) {
length = len;
}
void setBreadth(double bre) {
breadth = bre;
}
void setHeight(double hei) {
height = hei;
}
// 重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。
// 与其他函数一样,重载运算符有一个返回类型和一个参数列表。
// 重载 + 运算符,用于把两个 Box 对象相加
Box operator+(const Box& b){
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 程序的主函数
int main() {
Box Box1; // 声明 Box1,类型为 Box
Box Box2; // 声明 Box2,类型为 Box
Box Box3; // 声明 Box3,类型为 Box
double volume = 0.0; // 把体积存储在该变量中
// Box1 详述
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 把两个对象相加,得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape(int a, int b) {
width = a;
height = b;
}
int area() {
cout << "Parent class area :" <<endl;
return 0;
}
// 虚函数 是在基类中使用关键字 virtual 声明的函数。
virtual int virtualArea () {
cout << "Triangle class area :" << endl;
return (width * height / 2);
}
// 您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象
// 但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。
virtual int pureVirtualArea () = 0;
};
class Rectangle: public Shape {
public:
Rectangle(int a, int b): Shape(a, b) {
}
int area () {
cout << "Rectangle class area :" << endl;
return (width * height);
}
// 在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
int virtualArea () {
cout << "Rectangle class area :" << endl;
return (width * height);
}
// 纯虚函数在派生类中的具体实现。
int pureVirtualArea () {
cout << "Rectangle class area :" << endl;
return (width * height);
}
};
class Triangle: public Shape {
public:
Triangle( int a, int b): Shape(a, b) {
}
int area () {
cout << "Triangle class area :" << endl;
return (width * height / 2);
}
// 在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
int virtualArea () {
cout << "Triangle class area :" << endl;
return (width * height / 2);
}
// 纯虚函数在派生类中的具体实现。
int pureVirtualArea () {
cout << "Triangle class area :" << endl;
return (width * height / 2);
}
};
// 程序的主函数
int main() {
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// 存储矩形的地址
shape = &rec;
// 调用矩形的求面积函数 area
// shape是基类的指针,使用基类指针调用函数 area() ,执行的是基类Shape的方法。
// 这就是所谓的静态多态,或静态链接调用函数。
cout << "----- shape->area() -----" << endl;
cout << shape->area() << endl;
// 调用矩形的求面积函数 area
// 如果使用变量,执行的是派生类Rectangle的方法。
cout << "----- rec.area() -----" << endl;
cout << rec.area() << endl;
// 可以在通过派生类的变量rec,调用基类的方法
// 调用方法是<派生类变量>.<基类类名>::<基类方法>
cout << "----- rec.Shape::area() -----" << endl;
cout << rec.Shape::area() << endl;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。
cout << "----- shape->virtualArea() -----" << endl;
cout << shape->virtualArea() << endl;
// 通过纯虚函数的实现,来执行派生类的方法
cout << "----- shape->pureVirtualArea() -----" << endl;
cout << shape->pureVirtualArea() << endl;
// 存储三角形的地址
shape = &tri;
// 调用三角形的求面积函数 area
// shape是基类的指针,使用基类指针调用函数 area() ,执行的是基类Shape的方法。
// 这就是所谓的静态多态,或静态链接调用函数。
cout << "----- shape->area() -----" << endl;
cout << shape->area() << endl;
// 调用三角形的求面积函数 area
// 如果使用变量,执行的是派生类Triangle的方法。
cout << "----- tri.area() -----" << endl;
cout << tri.area() << endl;
// 可以在通过派生类的变量tri,调用基类的方法
// 调用方法是<派生类变量>.<基类类名>::<基类方法>
cout << "----- tri.Shape::area() -----" << endl;
cout << tri.Shape::area() << endl;
// 我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数
// 这种操作被称为动态链接,或后期绑定。
cout << "----- shape->virtualArea() -----" << endl;
cout << shape->virtualArea() << endl;
// 通过纯虚函数的实现,来执行派生类的方法
cout << "----- shape->pureVirtualArea() -----" << endl;
cout << shape->pureVirtualArea() << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
public:
// 构造函数定义
Box(double l, double b, double h) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// 每次创建对象时增加 1
objectCount++;
}
double Volume() {
return length * breadth * height;
}
// 静态成员函数即使在类对象不存在的情况下也能被调用
// 静态函数只要使用类名加范围解析运算符 :: 就可以访问。
// 静态成员函数只能访问静态数据成员,不能访问其他静态成员函数和类外部的其他函数。
// 静态成员函数有一个类范围,他们不能访问类的 this 指针。
// 可以使用静态成员函数来判断类的某些对象是否已被创建。
static int getCount() {
return objectCount;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
static int objectCount;
};
// 初始化类 Box 的静态成员
int Box::objectCount = 0;
int main(void) {
Box Box1(3.3, 1.2, 1.5); // 声明 box1
Box Box2(8.5, 6.0, 2.0); // 声明 box2
// 输出对象的总数
cout << "Total objects: " << Box::getCount() << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
public:
// 成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本
// 静态成员在类的所有对象中是共享的。
// 如果不存在其他的初始化语句,在创建第一个对象时,所有的静态数据都会被初始化为零。
// 不能把静态成员放置在类的定义中
// static int objectCount = 0;
static int objectCount;
// 构造函数定义
Box(double l, double b, double h) {
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
// 每次创建对象时增加 1
objectCount++;
}
double Volume() {
return length * breadth * height;
}
private:
double length; // 长度
double breadth; // 宽度
double height; // 高度
};
// 初始化类 Box 的静态成员
int Box::objectCount = 0;
int main(void) {
Box Box1(3.3, 1.2, 1.5); // 声明 box1
Box Box2(8.5, 6.0, 2.0); // 声明 box2
// 输出对象的总数
cout << "Total objects: " << Box::objectCount << endl;
return 0;
}
\ No newline at end of file
#include <iostream>
using namespace std;
class Box {
public:
// 构造函数定义
Box(double l, double b, double h) {
cout << "Constructor called." << endl;
// 每一个对象都能通过 this 指针来访问自己的地址。
// this 指针是所有成员函数的隐含参数。
// 因此,在成员函数内部,它可以用来指向调用对象。
// 只有成员函数或者成员变量才有 this 指针。
this->length = l;
this->breadth = b;
this->height = h;
}
double Volume() {
return length * breadth * height;
}
int compare(Box box) {
// 每一个对象都能通过 this 指针来访问自己的地址。
// this 指针是所有成员函数的隐含参数。
// 因此,在成员函数内部,它可以用来指向调用对象。
// 只有成员函数或者成员变量才有 this 指针。
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if (Box1.compare(Box2)) {
cout << "Box2 is smaller than Box1" <<endl;
} else {
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment