support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,28 @@
# please run `bazel run //practical_exercises/10_day_practice/day6/virtual_func:example`
# please run `bazel run //practical_exercises/10_day_practice/day6/virtual_func:virtual_feature`
# please run `bazel run //practical_exercises/10_day_practice/day6/virtual_func:virtual_dtor`
# please run `bazel run //practical_exercises/10_day_practice/day6/virtual_func:virtual`
# please run `bazel run //practical_exercises/10_day_practice/day6/virtual_func:vis`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "example",
srcs = ["example.cpp"],
)
cc_binary(
name = "virtual_feature",
srcs = ["virtual_feature.cpp"],
)
cc_binary(
name = "virtual_dtor",
srcs = ["virtual_dtor.cpp"],
)
cc_binary(
name = "virtual",
srcs = ["virtual.cpp"],
)
cc_binary(
name = "vis",
srcs = ["vis.cpp"],
)

View File

@@ -0,0 +1,93 @@
/* 编程实例.cpp */
/*
某公司有经理、销售员、小时工等多类人员。经理按周计算薪金销售员每月底薪800元
然后加销售提成每销售1件产品提取销售利润的5%;小时工按小时计算薪金。每类人员都有姓名和身份证号等数据。
为简化问题把各类人员的共有信息抽象成基类Employee其他人员则继承该类的功能。
*/
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee(string Name, string id) : name(Name), Id(id) {}
string getName() const { return name; } //返回姓名
string getID() const { return Id; } //返回身份证号
virtual float getSalary() const { return 0.0; } //返回薪水
virtual void print() const { //输出姓名和身份证号
cout << "姓名: " << name << "\t\t 编号: " << Id << endl;
}
private:
string name;
string Id;
};
class Manager : public Employee {
public:
Manager(string Name, string id, int week) : Employee(Name, id) {
WeeklySalary = week * 1000;
}
float getSalary() const { return WeeklySalary; } //获取经理的周薪
void print() const { //打印经理姓名、身份证、周薪
cout << "经理:" << getName() << "\t\t 编号: " << getID()
<< "\t\t 总工资: " << getSalary() << endl;
}
private:
float WeeklySalary; //周薪
};
class SaleWorker : public Employee {
public:
SaleWorker(string name, string id, int profit, int x) : Employee(name, id) {
workerMoney = baseMoney + x * 0.05 * profit;
}
float getSalary() const { return workerMoney; }
void print() const {
cout << "销售员:" << getName() << "\t\t 编号: " << getID()
<< "\t\t 总工资: " << getSalary() << endl;
}
private:
float baseMoney = 800.0;
float workerMoney;
};
class HourWorker : public Employee {
public:
HourWorker(string name, string id, int h) : Employee(name, id) {
TotalMoney = h * hourMoney;
}
float getSalary() const { return TotalMoney; }
void print() const {
cout << "小时工:" << getName() << "\t\t 编号: " << getID()
<< "\t\t 总工资: " << getSalary() << endl;
}
private:
float hourMoney = 100.0;
float TotalMoney;
};
int main() {
cout << "请输入工作周:";
int week;
cin >> week;
Manager m("小王", "11111111", week);
m.print();
cout << "请输入销售利润:";
int profit;
cin >> profit;
cout << "请输入销售件数:";
int x;
cin >> x;
SaleWorker s("小李", "222222", profit, x);
s.print();
cout << "请输入工作小时:";
int hour;
cin >> hour;
HourWorker h("小何", "333333", hour);
h.print();
return 0;
}

View File

@@ -0,0 +1,64 @@
/* 虚函数例子.cpp */
// Eg7-1.cpp
//基类指针或引用指向派生类对象时,虚函数与非虚函数区别:
//声明Employee的print为虚函数则可访问到Manager的print函数非虚函数则只能访问到Employee的print
#include <iostream>
#include <string>
using namespace std;
class Employee {
public:
Employee(string name, string id);
string getName();
string getId();
float getSalary();
virtual void print();
private:
string Name;
string Id;
};
Employee::Employee(string name, string id) {
Name = name;
Id = id;
}
string Employee::getName() { return Name; }
string Employee::getId() { return Id; }
float Employee::getSalary() { return 0.0; }
void Employee::print() {
cout << "姓名:" << Name << "\t"
<< "编号:" << Id << endl;
}
class Manager : public Employee {
public:
Manager(string name, string id, float s = 0.0) : Employee(name, id) {
weeklySalary = s;
}
void setSalary(float s) { weeklySalary = s; } //设置经理的周薪
float getSalary() { return weeklySalary; } //获取经理的周薪
void print() { //打印经理姓名、身份证、周薪
cout << "经理:" << getName() << "\t\t 编号: " << getId()
<< "\t\t 周工资: " << getSalary() << endl;
}
private:
float weeklySalary; //周薪
};
/*
不论哪种赋值方式,都只能通过基类对象(或基类对象的指针或引用)访问到派生类对象从基类中继承到的成员,
不能借此访问派生类定义的成员。而虚函数使得可以通过基类对象的指针或引用访问派生类定义的成员。
*/
int main() {
Employee e("小米", "NO0001"), *pM;
Manager m("小汪", "NO0002", 128);
m.print();
pM = &m;
pM->print();
Employee &rM = m;
rM.print();
return 0;
}
// Virtual关键字其实质是告知编译系统被指定为virtual的函数采用动态联编的形式编译。

View File

@@ -0,0 +1,24 @@
/* 虚析构函数.cpp */
#include <iostream>
using namespace std;
class A {
public:
virtual ~A() { cout << "call A::~A()" << endl; }
};
class B : public A {
char *buf;
public:
B(int i) { buf = new char[i]; }
~B() {
delete[] buf;
cout << "call B::~()" << endl;
}
};
int main() {
A *a = new B(10);
delete a;
return 0;
}

View File

@@ -0,0 +1,37 @@
/* 虚函数特性.cpp */
#include <iostream>
#include <string>
using namespace std;
class A {
public:
void f(int i) { cout << "A::f()" << endl; };
};
class B : public A {
public:
virtual void f(int i) { cout << "B::f()" << endl; }
};
class C : public B {
public:
void f(int i) { cout << "C::f()" << endl; }
};
//一旦将某个成员函数声明为虚函数后,它在继承体系中就永远为虚函数了
class D : public C {
public:
void f(int) { cout << "D::f()" << endl; }
};
int main() {
A *pA, a;
B *pB, b;
C c;
D d;
pA = &a;
pA->f(1); //调用A::f
pB = &b;
pB->f(1); //调用B::f
pB = &c;
pB->f(1); //调用C::f
pB = &d;
pB->f(1); //调用D::f
return 0;
}

View File

@@ -0,0 +1,18 @@
/* 从基类继承的成员将访问到派生类版本.cpp */
#include <iostream>
using namespace std;
class B {
public:
void f() { g(); }
virtual void g() { cout << "B::g"; }
};
class D : public B {
public:
void g() { cout << "D::g\n"; }
};
int main() {
D d;
d.f();
return 0;
}