support bazel complie this project and format code.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day6/abstract_class:main`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "main",
|
||||
srcs = ["main.cpp"],
|
||||
)
|
@@ -0,0 +1,43 @@
|
||||
/* 抽象类.cpp */
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Figure {
|
||||
protected:
|
||||
double x, y;
|
||||
|
||||
public:
|
||||
void set(double i, double j) {
|
||||
x = i;
|
||||
y = j;
|
||||
}
|
||||
virtual void area() = 0;
|
||||
};
|
||||
|
||||
class Trianle : public Figure {
|
||||
public:
|
||||
void area() { cout << "三角形面积:" << x * y * 0.5 << endl; }
|
||||
};
|
||||
class Rectangle : public Figure {
|
||||
public:
|
||||
void area() { cout << "这是矩形,它的面积是:" << x * y << endl; }
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
//定义抽象类指针
|
||||
Figure *pF = NULL;
|
||||
// Figure f1; 抽象类不能被实例化
|
||||
Rectangle r;
|
||||
Trianle t;
|
||||
t.set(10, 20);
|
||||
pF = &t;
|
||||
pF->area();
|
||||
r.set(10, 20);
|
||||
pF = &r;
|
||||
pF->area();
|
||||
//定义抽象类引用
|
||||
Figure &rF = t;
|
||||
rF.set(20, 20);
|
||||
rF.area();
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user