重写方法优先 C++
overriden method first c++
我正在读一本关于 c++ 编程的书,有一个练习应该用虚拟来解决:
//Main.cpp
#include "stdafx.h"
#include "Employee.h"
#include "Manager.h"
#include <vector>
#include <iostream>
#include <stdlib.h>
using namespace std;
//Generates the choice of which type of employee we are working on.
int generate_type_choice() {
cout << "1.Manager" << endl;
cout << "2.Enginner" << endl;
cout << "3.Researcher" << endl;
int choice=0;
cin >> choice;
return choice;
}
void addEmployee(vector<Employee*>* v) {
int choice = generate_type_choice();
cout << "first name: ";
string Fname;
cin.ignore();
getline(cin, Fname);
string Lname;
cout << "Last Name: ";
getline(cin, Lname);
cout << "Salary: ";
float s;
cin >> s;
switch (choice) {
case 1: {
cout << "Number of Meetings per week: ";
int m,vac;
cin >> m;
cout << "Number of vacation days per year: ";
cin >> vac;
Employee* e = new Manager(Fname, Lname, s, m, vac);
(*v).push_back(e);
break;
}
}
(*v).push_back(new Employee(Fname, Lname, s));
}
void printVector(vector<Employee*> v) {
for each (Employee* e in v)
{
(*e).printData();
}
}
int main()
{
vector<Employee*> v;
int choice = 0;
cout << "1.Add Employee" << endl;
cin >> choice;
switch (choice) {
case 1: {
addEmployee(&v);
}
}
printVector(v);
system("pause");
return 0;
}
//Employee.cpp
#include "stdafx.h"
#include "Employee.h"
#include <string>
#include <iostream>
using namespace std;
Employee::Employee()
{
Fname = "NoName";
Lname = "NoName";
salary = 0;
}
Employee::Employee(string f, string l, float s) {
Fname = f;
Lname = l;
salary = s;
}
void Employee::printData() {
cout << "First Name: " << Fname << endl;
cout << "Last Name: " << Lname << endl;
cout << "salary: " << salary << endl;
}
//Manage.cpp
#include "stdafx.h"
#include "Manager.h"
#include <string>
#include <iostream>
using namespace std;
Manager::Manager()
{
NumMeetings=0;
NumVacations=0;
}
void Manager::printData() {
cout << "Number of meetings per week: " << NumMeetings << endl;
cout << "Number of vacation days per year: " << NumVacations << endl;
}
我在这里想要的是打电话给员工::p rintData,然后打电话给经理::p rintData...(员工是经理的父类(我没有放 Getters 和 Setters 来减少代码,它不是一个完成的代码,所以 switch 不只有一种情况
您可以使用 ::
调用超类的运算符:
void Manager::printData() {
Employee::printData();
cout << "Number of meetings per week: " << NumMeetings << endl;
cout << "Number of vacation days per year: " << NumVacations << endl;
}
您可以从派生函数调用基函数。
void Manager::printData() {
cout << "Number of meetings per week: " << NumMeetings << endl;
cout << "Number of vacation days per year: " << NumVacations << endl;
Employee::printData();
}
将打印Manager
部分,然后Employee::printData();
调用printData
仅使用对象的Employee
部分来打印其余部分。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- C++从另一个类访问公共静态向量的正确方法是什么
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 没有为自己的结构调用列表推回方法
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 枚举环境变量的惯用C++14/C++17方法
- 初始化具有非默认构造函数的std::数组项的更好方法