Classes and OOP help C++ - c++

I'm having trouble with a simple code I have to for class, I'm trying to figure out how to add in a user input into a class, I've tried multiple things and could really use some assistance.
Code so Far:
#include <iostream>
#include <string>
using namespace std;
// Base class
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
// Derived class
class FamilyFued{
public:
points;
};
int main ()
{
GameShow TN;
TN.Name
return 0;
}

Classes are used to abstract the state and behavior of things.
State is in the form of class attributes.
The behaviour is in the form of class methods (functions).
This is a behaviour:
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
So you need to put it inside a function:
class GameShow{
public:
string Name;
GameShow(){
cout << "Enter name of contestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};

Based on what you have, you might want to wrap cout >> ...; cin <<...; part in a constructor:
class GameShow{
public:
string Name;
GameShow()
{
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
}
};
Other than that, please refer to: The Definitive C++ Book Guide and List

You code missing too many basics, you need start with a good c++ book.
using namespace std; // is bad idea, mentioned in stack overflow thousands of times.
bad
class GameShow{
public:
string Name;
cout << "Enter name of constestant: ";
cin >> Name;
cout << "Welcome " << Name <<"! Let's get ready to play the FEUD!!" << endl;
};
Your cout and cin function should be used inside a constructor
class GameShow{
public:
string Name;
GameShow()
{
std::cout << "Enter name of constestant: ";
std::cin >> Name;
std::cout << "Welcome "
<< Name
<< "! Let's get ready to play the FEUD!!" << std::endl;
}
};
TN.Name // what this line doing in your main function ??
Assuming you wanted to print Name of GameShow class member, so change your line to below.
std::cout << TN.Name;

Related

How do I add vectors into my structs to create an inventory system in which I can add multiple different wines to the system using only one struct?

I have a school assignment in which I have to create a Wine Inventory System where the user can add multiple different wines without a finite number I assume.
I need to create vectors but I'm not sure. I don't know what to try.
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
} wine;
void printwine(Wine1 wine);
int main()
{
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
cout << endl;
printwine2(wine2);
cout << endl;
printwine3(wine3);
}
void printwine(Wine1 wine)
{ //data the user typed as output
cout << "Wine1" << endl;
cout << "the name is: " << wine.name << endl;
cout << "it's year is: " << wine.year << endl;;
cout << "its country of creation is: " << wine.place << endl;;
cout << "it's price is: " << wine.price << endl;
}
It should output the name of the wine, the year, it's country, and it's price, for each wine which was added.
A good starting should be using vector of Wine1.
std::vector<Wine1> wineVec;
wineVec.reserve(/*size*/) // reserve the memory if you know the number of wines beforehand
The printwine function should now take std::vector<Wine1>(preferably const-reference as the data is read-only) and iterate through the vector to print the attributes of the Wine1.
Something like:
#include <string>
#include <iostream>
#include <vector>
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
// do printing each: wine.name, wine.year,... so on
}
}
int main()
{
std::vector<Wine1> vecWine;
int wineNumber = 2;
vecWine.reserve(wineNumber);
std::string name, year, place, price;
for (int i = 0; i < wineNumber; ++i)
{
// get the user input for name, year, place, and price
std::cin >> name >> year >> place >> price;
vecWine.emplace_back(Wine1{ name, year, place, price });
}
printwine(vecWine);
}
That said, you should read more about std::vector
to get to know more, how it works.
Also, good to read about, how to overload operator>> and operator<<, so that you could even write code, much simpler.
Following is an incomplete code, which I leave you to complete after covering the topics which I mentioned.
void printwine(const std::vector<Wine1>& vecWine)
{
for (const auto& wine : vecWine)
{
std::cout << wine << '\n';
}
}
int main()
{
std::vector<Wine1> vecWine(wineNumber);
for (Wine1& wine : vecWine)
{
std::cin >> wine;
}
printwine(vecWine);
}
I believe there is a misunderstanding: you do not intend your struct Wine1 to contain a vector but instead, you want a vector of Wine1's.
I suggest a data structure similar to the following:
struct Wine {
string name;
string year;
string place;
string price;
};
void printwinelist(vector<Wine>& list){
for(Wine& w : list){
printwine(w);
}
}
vector<Wine> winelist;
The main method has to be rewritten accordingly, to append additional objects to the vector.
While I could rewrite your code accordingly I suspect, that a better next step for you would be to read up on some of the concepts used, such as vectors.
You probably want something like this:
#include <string>
#include <iostream>
#include <vector>
using namespace std;
struct Wine1
{ //struct for Wine pssibly needs Vector
string name;
string year;
string place;
string price;
};
void printwine(Wine1 wine);
int main()
{
vector<Wine1> wineinventory;
// read 3 wines
for (int i = 3; i < 10; i++)
{
Wine1 wine;
string str; //input for data
cout << "Please enter the data of the First wine: " << endl;
cout << "Enter name: ";
getline(cin, wine.name);
cout << endl << "Enter year: ";
getline(cin, wine.year);
cout << endl << "enter country of creation: ";
getline(cin, wine.place);
cout << endl << "enter price: ";
getline(cin, wine.price);
cout << endl;
cout << "your entered data: " << endl;
printwine(wine);
wineinventory.push_back(wine); // store in vectore
}
// print all wines in the vector
for (int i = 0; i < wineinventory.size(); i++)
{
cout << "Wine " << i << ":" endl;
printwine(wineinventory[i]);
}
}
Disclaimer: this is untested code, I'm not even sure if it compiles, but you should get the idea.
There is still much room for improvement.

How to modify a nested struct on a vector?

I am working on a program that keeps inventory of vehicles, so I created a struct for it. It also needs to keep a list of the drivers, so I created a nested struct for that. Here's the code:
struct Vehicle {
string License;
string Place;
int Capacity;
struct Driver {
string Name;
int Code;
int Id;
} dude;
};
I ask for user input and then put the structs in a vector using this function:
void AddVehicle(vector<Vehicle> &vtnewV) {
Vehicle newV;
Vehicle::Driver dude;
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> dude.Name;
cout << "Enter the driver's code: " << endl;
cin >> dude.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> dude.Id;
vtnewV.push_back(newV);
};
Now, I need to know if there's a way to add the driver on another function, like, you ask for the vehicle info on one function and then ask for the driver's info on another. The user enters the license plate, for example, and the driver is added in the struct that has that license plate. I don't know if I'm explaining myself. So, that's it, if you can help me, I would really appreciate it.
Example of your own code:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
struct Vehicle {
std::string License;
std::string Place;
int Capacity;
struct Driver {
std::string Name;
int Code;
int Id;
}dude;
};
void AddVehicle(std::vector<Vehicle> &vtnewV)
{
Vehicle newV;
std::cout << "Enter license plate number: " << std::endl;
std::cin >> newV.License;
std::cout << "Enter the vehicle's ubication: " << std::endl;
std::cin >> newV.Place;
std::cout << "Enter the vehicle's capacity: " << std::endl;
std::cin >> newV.Capacity;
std::cout << "Enter the driver's name: " << std::endl;
std::cin >> newV.dude.Name;
std::cout << "Enter the driver's code: " << std::endl;
std::cin >> newV.dude.Code;
std::cout << "Enter the driver's identification number: " << std::endl;
std::cin >> newV.dude.Id;
vtnewV.push_back(newV);
};
int main()
{
std::vector<Vehicle> listVehicle;
AddVehicle(listVehicle);
AddVehicle(listVehicle);
for (auto& i : listVehicle)
{
std::cout << i.dude.Name << " got crabs" << std::endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
Now, I need to know if there's a way to add the driver on another
function, like, you ask for the vehicle info on one function and then
ask for the driver's info on another.
I don't know if this is what you are after but there is a way better way to solve this than doing it this way, but without changing too much of your code, this will give you a hint:
#include <iostream>
#include <string>
#include <vector>
#include <limits>
struct Vehicle {
std::string License;
std::string Place;
int Capacity;
struct Driver {
std::string Name;
int Code;
int Id;
}driver;
};
Vehicle CreateVehicle()
{
Vehicle vehicle;
std::cout << "Enter license plate number: " << std::endl;
std::cin >> vehicle.License;
std::cout << "Enter the vehicle's ubication: " << std::endl;
std::cin >> vehicle.Place;
std::cout << "Enter the vehicle's capacity: " << std::endl;
std::cin >> vehicle.Capacity;
return vehicle;
};
Vehicle::Driver CreateDriver()
{
Vehicle::Driver driver;
std::cout << "Enter the driver's name: " << std::endl;
std::cin >> driver.Name;
std::cout << "Enter the driver's code: " << std::endl;
std::cin >> driver.Code;
std::cout << "Enter the driver's identification number: " << std::endl;
std::cin >> driver.Id;
return driver;
}
int main()
{
std::vector<Vehicle> listVehicle;
auto vehicle = CreateVehicle();
auto driver = CreateDriver();
vehicle.driver = driver;
listVehicle.push_back(vehicle);
for (auto& i : listVehicle)
{
std::cout << i.driver.Name << " got crabs" << std::endl;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
How to modify a nested struct on a vector?
To set the driver of the N-th item from the vector of Vehicles, you'd use:
Vehicle::Driver driver_dude;
...
...
vtnewV[N-1].dude = driver_dude;
#include <iostream>
#include <vector>
using namespace std;
struct Driver
{
string Name;
int Code;
int Id;
};
struct Vehicle
{
string License;
string Place;
int Capacity;
///from my opinion driver should be declare like this
Driver driver;
};
vector <Vehicle> vtnewV ;
/// the vector need to declare outside the void
/// else it will keep recreate a new vector
void AddVehicle()
{
Vehicle newV;
cout << "Enter license plate number: " << endl;
cin >> newV.License;
cout << "Enter the vehicle's ubication: " << endl;
cin >> newV.Place;
cout << "Enter the vehicle's capacity: " << endl;
cin >> newV.Capacity;
cout << "Enter the driver's name: " << endl;
cin >> newV.driver.Name;
cout << "Enter the driver's code: " << endl;
cin >> newV.driver.Code;
cout << "Enter the driver's identification number: " << endl;
cin >> newV.driver.Id;
vtnewV.push_back(newV);
};
void ShowInfo()
{
for(int i= 0; i<vtnewV.size();i++)
{
cout<<vtnewV[].License<<newV.driver.Id;
///you need to use for loop to cout all that exist in vector info
}
}
int main()
{
AddVehicle();
ShowInfo();
return 0;
}
I have modify and added some comment to your code base by my own opinion
hope that will solve you problem

error C2601: "Name": local function definitions are illegal

I'm new to C++ and learning about Inheritance and Polymorphism. We require to write an employee project that have 4 types of employee (BasePlusCommission, CommisisonEmployee, Salaried and TipWorker). My project has one main() class that I used switch method for each type of employee. I got stuck on TipWorker where we have to do Polymorphism. Here what I got so far.
int main()
{
void virtualViaPointer(const Employee * const);
{
cout << "Enter First Name: " << endl;
cin >> firstName;
cout << "Enter Last Name: " << endl;
cin >> lastName;
cout << "Enter SSN: " << endl;
cin >> SSN;
if (SSN.length() == 9)
{
SSN = true;
}
else
{
cout << "Please enter SSN again with 9 digits only:" << endl;
cin >> SSN;
}
cout << "Enter wages: " << endl;
cin >> wage;
cout << "Enter hours: " << endl;
cin >> hours;
cout << "Enter tips: " << endl;
cin >> tips;
TipWorker employee4(firstName, lastName, SSN, wage, hours, tips);
employee4.print();
cout << fixed << setprecision(2);
vector < Employee * > employees(1);
employees[0] = &employee4;
cout << "Employee processed polymorphically via dynamic binding: \n\n";
cout << "Virtual function calls made off base-class pointers:\n\n";
for (const Employee *employeePtr : employees)
virtualViaPointer(employeePtr);
void virtualViaPointer(const Employee * const baseClassPtr)
{
baseClassPtr->print();
cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n";
}
break;
}
}
When I run the project, I came up with this error:
error C2601: "virtualViaPointer": local function definitions are
illegal
void virtualViaPointer(const Employee * const baseClassPtr)
{
baseClassPtr->print();
cout << "\nEarned $" << baseClassPtr->earnings() << "\n\n";
}
Can anyone please help me? Thank you so much!
You may not define one function inside an other function. Any function definition shall be outside any other function definition.
Place the definition of virtualViaPointer outside the body of main.
You can have a declaration of a function inside a function, but no definition(!) of that function. However, you can have a local struct/class or lambda in the function:
#include <iostream>
void f()
{
void print(); // mostly useless - maybe a most vexing parse error
struct Print {
static void apply(const char* s) { std::cout << s << '\n'; }
}
Print::apply("Hello");
auto lambda = [] (const char* s) { std::cout << s << '\n'; };
lambda("World");
}
Note: A local struct does not require C++11 (Also, it may look nicer while debugging)

C++ Address Book

I am trying to make it so if userselection = 1, then the user is asked questions to create their contact for an address book. It saves all of the contact info to a struct and then saves to a .txt file. I am very new to C++. This is what I have so far... I keep getting [Error] expected primary-expression before '.' token. <---- HOW CAN I FIX THIS
Also, can anyone offer guidance for how to save the struct to a file?
Thanks.
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main(){
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == '1');
person newPerson;
cout << "What is your Name?" << endl;
cin >> person.Name;
cout << "What is your Address?" << endl;
cin >> person.Address;
cout << "What is your Phone Number?" << endl;
cin >> person.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> person.Email;
}
For the error you describe, you need to access members in the class instance, not the class definition ..
newPerson.Name
rather than
person.Name
Your mistakes were simply associated with syntax. Please read your compiler's error messages in the future.
#include <iostream>
#include <string> // added
using namespace std;
struct person {
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
int userselection = 0;
cout << "What do you want to do? Press 1 to Add Contact -- Press 2 to Search for Contact"<<endl;
cin >> userselection;
if(userselection == 1) { // userselection is int so why compare it to char
person newPerson;
cout << "What is your Name?" << endl;
cin >> newPerson.Name; // assign to object's member not a static member
cout << "What is your Address?" << endl;
cin >> newPerson.Address;
cout << "What is your Phone Number?" << endl;
cin >> newPerson.PhoneNumber;
cout << "What is your Email?" << endl;
cin >> newPerson.Email;
}
}

Create a vector of base class objects and store derived class objects within

I am trying to create an employee database (Vector of Employees). There are 3 types of employees ie. employees is the base class and Manager, Engg and Scientist are derived class.
Every employee has first name and last name. In addition to the name, each of the 3 types of employees have unique stats ie. Manager has number of meetings/week whereas the Engg has work experience and so on.
I have a couple of questions
1. Should I upcast the derived objects to the base class or downcast the base class to the derived class?
2. How do I use polymorphism to override methods, since I want the user to add an employee type and based on the type selected the respective entry fields should appear ie. in case of a Manager, in addition to the first and last names, the program should also ask for meetings/week?
Here is my Class file
class Employee{
public:
Employee();
Employee(string fName, string lName, int sal);
virtual void printEmp();
string getFirstName();
string getLastName();
protected:
string m_fName;
string m_lName;
int m_sal;
};
class Manager : public Employee{
public:
Manager();
Manager(string fName, string lName, int sal, int meets, int hols);
void printEmp();
protected:
int m_meets;
int m_hols;
};
Here is the implementation
Employee::Employee(){
m_fName = "Default";
m_lName = "Default";
m_sal = 0;
}
Employee::Employee(string fName, string lName, int sal){
m_fName = fName;
m_lName = lName;
m_sal = sal;
}
void Employee::printEmp(){
cout << "First Name: " << m_fName << endl
<< "Last Name: " << m_lName << endl
<< "Salary: " << m_sal << endl;
}
string Employee::getLastName(){
return m_lName;
}
string Employee::getFirstName(){
return m_fName;
}
Manager::Manager(string fName, string lName, int sal, int meets, int hols) : Employee(fName, lName, sal), m_meets(meets), m_hols(hols)
{
//empty
}
void Manager::printEmp(){
Employee::printEmp();
cout << "Meets/Week: " << m_meets << endl
<< "Holidays/Year: " << m_hols << endl << endl;
Here is the main
int main(){
bool exit = false;
vector<Employee*> dBVector;
while (!exit){
cout << "Welcome to Employee Database, Enter an option to continue..." << endl;
cout << "1) Add an Employee, 2) Delete an Employee, 3) Save Database, 4) Exit" << endl;
int input;
cin >> input;
string fNameInp;
string lNameInp;
int salInp;
string lNameSearch;
int i; // for loop in Delete employee case
bool deleted = false;
switch (input){
case 1: //Add
cout << "1) Add a Manager, 2) Add an Engg, 3) Add a Researcher" << endl;
int empInput;
cin >> empInput;
if (empInput == 1){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "Number of meetings/week: ";
int meetsInp;
cin >> meetsInp;
cout << "Number of holidays/year: ";
int holsInp;
cin >> holsInp;
Manager mEmp(fNameInp, lNameInp, salInp, meetsInp, holsInp);
Employee &emp = mEmp;
dBVector.push_back(&mEmp);
dBVector[dBVector.size()-1]->printEmp();
}
else if (empInput == 2){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "Cpp Experience (Y/N): ";
string cppInp;
cin >> cppInp;
cout << "Years of experience: ";
float expInp;
cin >> expInp;
cout << "Engg Type (Chem, Mech, IT): ";
string typInp;
cin >> typInp;
Engg eEmp(fNameInp, lNameInp, salInp, cppInp, expInp, typInp);
Employee &emp = eEmp;
dBVector.push_back(&eEmp);
dBVector[dBVector.size() - 1]->printEmp();
}
else if (empInput == 3){
cout << "Enter First Name: ";
cin >> fNameInp;
cout << "Enter Last Name: ";
cin >> lNameInp;
cout << "Enter Salary: ";
cin >> salInp;
cout << "School of PhD: ";
string schoolInp;
cin >> schoolInp;
cout << "Topic of PhD: ";
string topImp;
cin >> topImp;
Researcher rEmp(fNameInp, lNameInp, salInp, schoolInp, topImp);
Employee &emp = rEmp;
dBVector.push_back(&rEmp);
dBVector[dBVector.size() - 1]->printEmp();
}
break;
case 2: // Delete Emp
for (int x = 0; x < dBVector.size(); x++){
dBVector[x]->getLastName();
cout << endl;
}
cout << "Input Last name of the employee to delete: " << endl;
cin >> lNameSearch;
for (i = 0; i < dBVector.size(); i++){
if (dBVector[i]->getLastName() == lNameSearch){
dBVector.erase(dBVector.begin() + i);
cout << dBVector[i]->getFirstName() << "has been deleted from database";
deleted = true;
break;
}
}
if (deleted == false && i == dBVector.size()){
cout << "No Employee with Last Name - " << lNameSearch << " exists in Database." << endl;
}
else
break;
case 3: //save
cout << "saving..." << endl;
break;
case 4: //exit
exit = true;
break;
}
}
}
Please Help!
Firstly, if you want to use polymorphism you need to store pointers in your vector. As the vector is the sole owner of the employees something like std::vector<std::unique_ptr<Employee>> would be suitable.
Edit: I see you have updated the vector to use pointers. But you are storing a pointer to a local stack allocated object, e.g mEmp. This will not work, when the mEmp variable goes out-of-scope at the closing brace the object will be deleted and you will be left with a dangling pointer in your vector that points to a deleted object. Using this dangling pointer is undefined behaviour. You need to allocate the Manager on the heap using new. Then the object will not be deleted when the variable goes out-of-scope but you do need to remember to delete the object when you are done. Something like unique_ptr makes this easy.
Regarding your questions:
Try to minimize explicit casting, especially downcasting. At the point that you store the Employee in the vector it will be implicitly upcast from the derived class to Employee but other than that there is not much need for casting.
You have roughly the right idea when it comes to overriding methods, if you call the virtual printEmp method on an Employee pointer it will call the override in the derived class.
If you are happy for the user input to be the responsibility of the Employee classes you could simply add a virtual method that initializes the employee using suitable input from the user. But I would be tempted to keep that separate from your domain objects. You need a switch statement on the user choice of employee type anyway so polymorphism doesn't gain you much there.
If you really want to use polymorphism for the employee creation I would suggest using something like the Abstract Factory pattern.
Here is my suggestion anyway:
#include <vector>
#include <string>
#include <iostream>
#include <memory>
class Employee {
public:
Employee(std::string fName, std::string lName, int sal);
virtual ~Employee();
virtual void printEmp();
protected:
std::string m_fName;
std::string m_lName;
int m_sal;
};
class Manager : public Employee {
public:
Manager(std::string fName, std::string lName, int sal, int meets, int hols);
void printEmp() override;
protected:
int m_meets;
int m_hols;
};
Employee::Employee(std::string fName, std::string lName, int sal)
: m_fName(fName), m_lName(lName), m_sal(sal) {
}
Employee::~Employee() {
}
void Employee::printEmp(){
std::cout << "First Name: " << m_fName << "\n"
<< "Last Name: " << m_lName << "\n"
<< "Salary: " << m_sal << "\n";
}
Manager::Manager(std::string fName, std::string lName, int sal, int meets, int hols)
: Employee(fName, lName, sal), m_meets(meets), m_hols(hols){
}
void Manager::printEmp(){
Employee::printEmp();
std::cout << "Meets/Week: " << m_meets << "\n"
<< "Holidays/Year: " << m_hols << "\n";
}
std::unique_ptr<Manager> createManager() {
std::cout << "Enter First Name: ";
std::string fNameInp;
std::cin >> fNameInp;
std::cout << "Enter Last Name: ";
std::string lNameInp;
std::cin >> lNameInp;
std::cout << "Enter Salary: ";
int salInp;
std::cin >> salInp;
std::cout << "Number of meetings/week: ";
int meetsInp;
std::cin >> meetsInp;
std::cout << "Number of holidays/year: ";
int holsInp;
std::cin >> holsInp;
std::cout << "\n";
return std::make_unique<Manager>(fNameInp, lNameInp, salInp, meetsInp, holsInp);
}
std::unique_ptr<Employee> createEmployee() {
int input;
std::cout << "1) Add a Manager, 2) Add an Engg, 3) Add a Researcher\n";
std::cin >> input;
switch (input){
case 1:
return createManager();
default:
return nullptr;
}
}
int main() {
std::vector<std::unique_ptr<Employee>> dBVector;
std::cout << "Welcome to Employee Database, Enter an option to continue...\n";
std::cout << "1) Add an Employee"
<< ", 2) Delete an Employee"
<< ", 3) Save Database"
<< ", 4) Exit\n";
int input;
std::cin >> input;
switch (input){
case 1:
dBVector.push_back(createEmployee());
break;
default:
break; // Do nothing
}
dBVector.at(0)->printEmp();
}
Live demo
You may want to store pointers in the vector to avoid slicing, as others mentioned. Then each employee could have their own input method and ask the right questions to initialize themselves (main would not implement that but only call the respective employee's virtual input function). Input and output are conceptually sysmmetrical operations which hints at implementing them symmetrically, i.e. in this case both as member functions.