update
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
#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();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
92
practical_exercises/10_day_practice/day6/虚函数/编程实例.cpp
Normal file
92
practical_exercises/10_day_practice/day6/虚函数/编程实例.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
某公司有经理、销售员、小时工等多类人员。经理按周计算薪金;销售员每月底薪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(){ return name; } //返回姓名
|
||||
string getID(){ return Id; } //返回身份证号
|
||||
float getSalary(){ return 0.0; } //返回薪水
|
||||
void print(){ //输出姓名和身份证号
|
||||
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(){ return WeeklySalary; } //获取经理的周薪
|
||||
void print(){ //打印经理姓名、身份证、周薪
|
||||
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(){
|
||||
return workerMoney;
|
||||
}
|
||||
void print(){
|
||||
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(){
|
||||
return TotalMoney;
|
||||
}
|
||||
void print(){
|
||||
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();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
66
practical_exercises/10_day_practice/day6/虚函数/虚函数例子.cpp
Normal file
66
practical_exercises/10_day_practice/day6/虚函数/虚函数例子.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
//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();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
//Virtual关键字其实质是告知编译系统,被指定为virtual的函数采用动态联编的形式编译。
|
||||
30
practical_exercises/10_day_practice/day6/虚函数/虚函数特性.cpp
Normal file
30
practical_exercises/10_day_practice/day6/虚函数/虚函数特性.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
class A {
|
||||
public:
|
||||
void f(int i){cout<<"…A"<<endl;};
|
||||
};
|
||||
class B: public A {
|
||||
public:
|
||||
virtual void f(int i){cout<<"…B"<<endl;}
|
||||
};
|
||||
class C: public B {
|
||||
public:
|
||||
void f(int i){cout<<"…C"<<endl;}
|
||||
};
|
||||
//一旦将某个成员函数声明为虚函数后,它在继承体系中就永远为虚函数了
|
||||
class D: public C{
|
||||
public:
|
||||
void f (int){cout<<"…D"<<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); //调用A::f
|
||||
pB=&c; pB->f(1); //调用A::f
|
||||
pB=&d; pB->f(1); //调用A::f
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
22
practical_exercises/10_day_practice/day6/虚函数/虚析构函数.cpp
Normal file
22
practical_exercises/10_day_practice/day6/虚函数/虚析构函数.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
#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;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user