I am reading a book about c++ programming and there is an exercice that should be solved with virtual:
//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;
}
what i want here is to call the employee::printData and after that call Manager::printData
...
(Employee is the parent Class of Manager)
i didn't put Getters and Setters to reduce the code and it is not a finished code so switch doesn't have only one case
You can use the :: to call the superclass' operator:
void Manager::printData() {
Employee::printData();
cout << "Number of meetings per week: " << NumMeetings << endl;
cout << "Number of vacation days per year: " << NumVacations << endl;
}
You can call the base function from the derived function.
void Manager::printData() {
cout << "Number of meetings per week: " << NumMeetings << endl;
cout << "Number of vacation days per year: " << NumVacations << endl;
Employee::printData();
}
Will print the Manager part and then Employee::printData(); will call printData just using the Employee part of the object to print out the rest.
Related
I'm creating a database for Customer in C++. The firt void works well but the second one returns me the following error: expected primary-expression before 'int'
Where is teh mistake? How can I figure out it?
I can't understand why I receive this error. I'm a beginner and this is my firt important project.
Many thanks!!!
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <cstdio>
using namespace std;
class DataCustomer{ //initialize a class
public:
DataCustomer(){
count = 0 ;
}
void AddCustomer();
void DisplayAll(int i);
void DeleteFile();
void EditFile();
void quit();
void search();
void searchName();
void searchSurname();
void searchCell();
void searchEmail();
int Menu();
//defining New Customer's details
struct NewCustomer
{
char Name[20];
char Surname[20];
char Birthday[10];
char PhoneNum [15];
char Email[20];
};
NewCustomer entries [10000];
int count;
};
void DataCustomer::AddCustomer()
{
cout<<"Enter a Name: ";
ofstream a_file ("database.txt", ios::app);
cin >> entries[count].Name;
a_file << entries [count].Name<<"\n";
cin.clear();
cin.sync();
cout << "Enter a Surname: ";
cin >> entries[count].Surname;
a_file << entries[count].Surname << "\n";
cin.clear();
cin.sync();
cout << "Enter Date of Birth: ";
cin >> entries[count].Birthday;
a_file << entries[count].Birthday << "\n";
cin.clear();
cin.sync();
cout << "Enter Phone Number: ";
cin >> entries[count].PhoneNum;
a_file << entries[count].PhoneNum << "\n";
cin.clear();
cin.sync();
cout << "Enter Email: ";
cin >> entries[count].Email;
a_file << entries[count].Email << "\n";
a_file.close();
++count;
}
void DataCustomer::DisplayAll(int i)
{
system ("cls");
cout<<"Entire Customers' database:"<<endl;
ifstream a_file ( "database.txt" );
a_file>> entries[i].Name;
a_file>> entries[i].Surname;
a_file>> entries[i].Birthday;
a_file>> entries[i].PhoneNum;
a_file>> entries[i].Email;
cout << "Name : " << entries[i].Name << endl;
cout << "Surname: " << entries[i].Surname << endl;
cout << "Date of birth : " << entries[i].Birthday << endl;
cout << "Phone number : " << entries[i].PhoneNum << endl;
cout << "Email: " << entries[i].Email << endl;
cout << "Number of Entries : " << count << endl;
for(int i = 0;i < count;++i)
{cout << endl;
DisplayAll(i); }
}
DataCustomer db;
int main ()
{
db.DisplayAll(int i);
return 0;
}
Okay, so I have a parent class called employee and 3 subclass called manager,researcher and engineer. I made a vector and want to list them. this is the how I process the making.
vector <Employee*,Manager*> EmployeeDB;
Employee *temp;
temp = new Manager(first, last, salary, meetings, vacations);
EmployeeDB.push_back(temp);
I have no problem in making the vector, my concern is listing the info. all 3 subclasses have firstname, lastname and salary but they're difference is that they have different data members which is unique, example the Manager has the int value vacation and the Engineer has the int value experience so on and so forth.
Employee.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef EMPLOYEE_h
#define EMPLOYEE_h
class Employee
{
public:
Employee();
Employee(string firstname, string lastname, int salary);
string getFname();
string getLname();
int getSalary();
virtual void getInfo();
private:
string mFirstName;
string mLastName;
int mSalary;
};
#endif
Employee.cpp:
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee()
{
mFirstName = "";
mLastName = "";
mSalary = 0;
}
Employee::Employee(string firstname, string lastname, int salary)
{
mFirstName = firstname;
mLastName = lastname;
mSalary = salary;
}
string Employee::getFname()
{
return mFirstName;
}
string Employee::getLname()
{
return mLastName;
}
int Employee::getSalary()
{
return mSalary;
}
void Employee::getInfo()
{
cout << "Employee First Name: " << mFirstName << endl;
cout << "Employee Last Name: " << mLastName << endl;
cout << "Employee Salary: " << mSalary << endl;
}
Main:
#include <vector>
#include <iostream>
#include <string>
#include "Employee.h"
#include "Engineer.h"
#include "Manager.h"
#include "Researcher.h"
using namespace std;
vector <Employee*> EmployeeDB;
Employee *temp;
void add()
{
int emp, salary, vacations, meetings, exp, c;
string first, last, type, school, topic;
bool skills;
do
{
system("cls");
cout << "===========================================" << endl;
cout << " Add Employee " << endl;
cout << "===========================================" << endl;
cout << "[1] Manager." << endl;
cout << "[2] Engineer." << endl;
cout << "[3] Researcher." << endl;
cout << "Input choice: ";
cin >> emp;
system("cls");
} while (emp <= 0 || emp > 3);
cout << "===========================================" << endl;
cout << " Employee Info " << endl;
cout << "===========================================" << endl;
cout << "Employee First name: ";
cin >> first;
cout << "Employee Last name: ";
cin >> last;
cout << "Employee Salary: ";
cin >> salary;
switch (emp)
{
case 1:
cout << "Employee numbers of meetings: ";
cin >> meetings;
cout << "Employee numbers of vacations: ";
cin >> vacations;
temp = new Manager(first, last, salary, meetings,vacations);
EmployeeDB.push_back(temp);
delete temp;
break;
case 2:
cout << endl;
cout << "[1]YES [2]NO" << endl;
cout << "Employee C++ Skills: ";
cin >> c;
if (c == 1)
{
skills = true;
}
else
{
skills = false;
}
cout << "Employee Years of exp: ";
cin >> exp;
cout << "(e.g., Mechanical, Electric, Software.)" << endl;
cout << "Employee Engineer type: ";
cin >> type;
temp = new Engineer(first, last, salary, skills, exp, type);
EmployeeDB.push_back(temp);
delete temp;
break;
case 3:
cout << "Employee School where he/she got his/her PhD: ";
cin >> school;
cout << "Employee Thesis Topic: ";
cin >> topic;
temp = new Researcher(first, last, salary, school, topic);
EmployeeDB.push_back(temp);
delete temp;
break;
}
}
void del()
{
}
void view()
{
for (int x = 0; x < (EmployeeDB.size()); x++)
{
cout << EmployeeDB[x]->getInfo();
}
}
void startup()
{
cout << "===========================================" << endl;
cout << " Employee Database " << endl;
cout << "===========================================" << endl;
cout << "[1] Add Employee." << endl;
cout << "[2] Delete Employee." << endl;
cout << "[3] List Employees." << endl;
cout << "[4] Exit." << endl;
cout << "Please Enter Your Choice: ";
}
int main(int argc, char** argv)
{
bool flag = true;
int choice;
do {
do
{
system("cls");
system("pause>nul");
startup();
cin >> choice;
} while (choice < 0 || choice >4);
switch (choice)
{
case 1:
add();
break;
case 2:
del();
break;
case 3:
view();
break;
case 4:
flag = false;
system("EXIT");
break;
}
} while (flag == true);
return 0;
system("pause>nul");
}
I am getting error on the view() function.
It says no operator<< matches these operands
binary '<<': no operator found which takes a right hand operand of type void etc etc.
The problem is that the getInfo has return type void and you are trying to put that return value into cout.
It's important to understand that the code std::cout << val actually calls the function operator<<(ostream& out, const objectType& val) where objectType is the type of 'val'.
In your case the type is void, and there is simply no implementation of operator<< that takes void as a type. hence the error "no operator found which takes a right hand operand of type void...".
In order to fix your issue you have a few options:
Change view() to be
for (...)
{
EmployeeDB[x]->getInfo();
}
Change getInfo() to return a string the info as you'd like:
std::string getInfo()
{
std::string info;
info =...
return info;
}
Create an operator<< for Employee and change view to call it:
view()
{
for (...)
{
std::cout << EmployeeDB[x];
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For my homework assignment, I was supposed to code a struct that collected information about music albums. I was able to do this easily. The second part of my assignment was to turn my struct into a class, and I'm having trouble getting my code to compile. Here are my two codes.
Struct code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
struct Album
{
string name;
string artist;
vector <string> songs;
Date release;
};
void initializeAlbum(Album& a);
void initializeSongs(vector <string> & songs);
void printAlbum(Album a);
int main()
{
Album my_album;
initializeAlbum(my_album);
printAlbum(my_album);
return 0;
}
void initializeAlbum(Album& a)
{
cout << "Enter album name:" << endl;
getline(cin, a.name);
cout << "Enter artist name:" << endl;
getline(cin, a.artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> a.release.month;
cout << "Enter the release day:" << endl;
cin >> a.release.day;
cout << "Enter the release year:" << endl;
cin >> a.release.year;
initializeSongs(a.songs);
}
void initializeSongs(vector <string> & songs)
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void printAlbum(Album a)
{
cout << "The album name is " << a.name << endl;
cout << "The artist name is " << a.artist << endl;
cout << "The release date is " << a.release.day << "/" << a.release.month << "/" << a.release.year << endl;
cout << "The songs are:";
for (int x = 0; x < a.songs.size(); x++ )
cout << " " << a.songs[x] << endl;
}
Class code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
class Album
{
public:
void printAlbum();
void initializeAlbum();
string name;
string artist;
vector <string> songs;
Date release;
private:
void initializeSongs();
};
int main()
{
Album a;
a.initializeAlbum();
a.printAlbum();
return 0;
}
void Album::initializeAlbum( )
{
cout << "Enter album name:" << endl;
getline(cin, name);
cout << "Enter artist name:" << endl;
getline(cin, artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> release.month;
cout << "Enter the release day:" << endl;
cin >> release.day;
cout << "Enter the release year:" << endl;
cin >> release.year;
initializeSongs(songs);
}
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void Album::printAlbum()
{
cout << "The album name is " << name << endl;
cout << "The artist name is " << artist << endl;
cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl;
cout << "The songs are:";
for (int x = 0; x < songs.size(); x++ )
cout << " " << songs[x] << endl;
}
Maybe this line is your error: initializeSongs(songs); in void Album::initializeAlbum() function?
Follow compiler messages.
This function takes zero arguments:
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
Yet you call
initializeSongs(songs);
You can access your members within your class functions so just call it like:
initializeSongs();
This compiles fine:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
class Album
{
public:
void printAlbum();
void initializeAlbum();
string name;
string artist;
vector <string> songs;
Date release;
private:
void initializeSongs();
};
int main()
{
Album a;
a.initializeAlbum();
a.printAlbum();
return 0;
}
void Album::initializeAlbum( )
{
cout << "Enter album name:" << endl;
getline(cin, name);
cout << "Enter artist name:" << endl;
getline(cin, artist);
cout << "Enter the release month (numeric format: ex. 06/10/1995):" << endl;
cin >> release.month;
cout << "Enter the release day:" << endl;
cin >> release.day;
cout << "Enter the release year:" << endl;
cin >> release.year;
initializeSongs();
}
void Album::initializeSongs()
{
cout << "How many songs are in the album?" << endl;
int j = 0;
cin >> j;
string answer;
cout << "Enter each song name one at a time:" << endl;
for ( int y = 0; y <= j; y++)
{
getline (cin, answer);
songs.push_back(answer);
}
}
void Album::printAlbum()
{
cout << "The album name is " << name << endl;
cout << "The artist name is " << artist << endl;
cout << "The release date is " << release.day << "/" << release.month << "/" << release.year << endl;
cout << "The songs are:";
for (int x = 0; x < songs.size(); x++ )
cout << " " << songs[x] << endl;
}
First time here, an amateur programmer, I have quite a bit of issue. I want to make a virtual model shop. I can add new items, check the inventory, all in a vector but when I want to delete a specific item (by producer&name&scale) it always deletes the last element.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class model
{
private:
int price;
string scale, name, producer;
public:
vector<model> vectorr;
model();
virtual ~model();
int getprice();
string getscale();
string getname();
string getproducer();
void setprice(int);
void setscale(string);
void setname(string);
void setproducer(string);
friend ostream &operator>>(ostream &stream, model &mt);
model &operator=(model &mt);
};
That's the header.
#include <iostream>
#include "header1.h"
#include <string>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
vector<machete> vectorr;
machete c;
machete t;
cout << "Welcome to the shop , what do you want to do?" << endl;
cout << "Chose your destiny : 1. Add 2.Get 3.Remove 4.Sort 0.Quit" << endl;
int menu, temp2;
string temp;
cin >> menu;
while (menu != 0) {
switch (menu) {
case 0 : cout << "Bye bye";
_getch();
exit(1);
break;
case 1 : cout << "Enter a producer" << endl;
cin >> temp;
c.setproducer(temp);
cout << "Enter a name" << endl;
cin >> temp;
c.setname(temp);
cout << "Enter a scale" << endl;
cin >> temp;
c.setscale(temp);
cout << "Enter a price" << endl;
cin >> temp2;
c.setprice(temp2);
vectorr.push_back(c);
cout << "You have succesfully added a model !" << endl;
break;
case 2 : cout << "Producer | Name | Scale | Price" << endl;
for (std::vector<model>::iterator it = vectorr.begin(); it != vectorr.end(); ++it) {
cout >> *it;
}
cout << endl;
break;
case 3 : cout << "What do you want to remove?" << endl;
cout << "Enter the producer" << endl;
string producer;
cin >> producer;
cout << "Enter the name" << endl;
string name;
cin >> name;
cout << "Enter the scale" << endl;
string scale;
cin >> scale;
for (unsigned i = 0; i < vectorr.size(); ++i) {
t.setname(vectorr[i].getnume());
t.setproducer(vectorr[i].getproducator());
t.setscale(vectorr[i].getscara());
if (t.getname() == name && t.getproducer() == producer && t.getscle() == scale) {
vectorr.erase(vectorr.begin() + i);
cout << "You have succesfully erased the " << name << " model" << endl;
break;
}
}
break;
}
cout << "Chose your destiny : 1. Add 2.Get 3.Remove 4.Sort 0.Quit" << endl;
cin >> menu;
}
return 0;
}
That's the main. case 3 is the problem deleting thingamajack. It always deletes the last element in my vector. The if(name=name and so on) works properly, tested it three times to be sure that wasn't the problem. It appears whatever I write vectorr.erase just won't work the way I want it to.
Sorry for the messy code, I might have done something wrong before the case 3, ignore case 4 the sort, not implemented yet, any help is appreciated, thanks!
In case 2, you've written
std::vector<model>::iterator it = vectorr.begin()
whereas you defined
vector<machete> vectorr;
I think you've confused between machete and model.
I have to make an address book using C++ and should include classes and inherited classes. The main class contains name and address and the inherited Teacher class contains teacher Phone number and CNIC and Employee class contains Phone and CNIC and the Student class contains student ID and Phone. I made a program in C++ but that would always over write the previous data. I am stuck here. I would need a skull program or simple algorithms to guide me ahead.
This is my code.
// new.cpp : Defines the entry point for the console application.
// Address Book.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include <conio.h>
#include <fstream>
#include <sstream>
#include <iomanip>
using namespace std;
class Main
{
private:
string Name;
string Address;
char test;
public:
virtual void getdata()
{
ofstream outfile("TEMP.txt");
cout <<"Enter Name: " ;
cin>>test;
getline(cin, Name); //cin >> Name;
cout << endl;
cout << "Enter Address: ";
getline(cin, Address);// cin >> Address;
outfile << Name
<< endl
<< Address;
}
virtual void setdata()
{
cout << "\n\nName: " << test+Name;
cout << "\nAddress: " << Address;
}
virtual void remove()
{
}
};
//----------------------------------------------------------
class Teacher : public Main
{
private:
int T_Number;
string T_CNIC;
public:
void getdata()
{
ofstream outfile("Teacher.txt");
Main::getdata();
cout << "Enter CNIC: ";
getline(cin,T_CNIC);//cin >> T_CNIC;
cout << "Enter Contact Number: " << endl;
cin >> T_Number;
outfile << T_CNIC
<< endl
<< T_Number;
cout << "Data entered" << endl;
}
void setdata()
{
Main::setdata();
cout << "\nTeacher CNIC: " << T_CNIC;
cout << "\nTeacher Contact Number: " << T_Number;
}
};
//------------------------------------------------------------
class Student : public Main
{
private:
int S_Number;
int S_ID;
public:
void getdata()
{
ofstream outfile("Student.txt");
Main::getdata();
cout << "Enter ID: ";
cin >> S_ID;
cout << "Enter Contact Number: ";
cin >> S_Number;
outfile << S_ID
<< endl
<< S_Number;
cout << "Data entered" << endl;
}
void setdata()
{
Main::setdata();
cout << "\nStudent Unique ID: " << S_ID;
cout << "\nStudent Contact Number: " << S_Number;
}
};
class Employee : public Main
{
private:
int E_Number;
string E_CNIC;
public:
void getdata()
{
ofstream outfile("Employee.txt");
Main::getdata();
cout << "Enter Employee CNIC: ";
getline(cin,E_CNIC);//cin >> E_CNIC;
cout << "Enter Contact Number: ";
cin >> E_Number;
outfile << E_CNIC
<< endl
<< E_Number;
cout << "Data entered" << endl;
;}
void setdata()
{
Main::setdata();
cout << "\nEmployee Unique ID: " << E_CNIC;
cout << "\nEmployee Contact Number: " << E_Number;
}
};
//-----------------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
Main* myarr[100];
int var = 0;
int choice;
char input;
start:
printf("===============MAIN MENU===============");
printf("\n[1]Insert\n[2]Search\n[3]Delete\n[4]Exit\n");
printf("=======================================");
printf("\n\nEnter Your Choice: ");
cin >> choice;
switch(choice)
{
case 1:
cout << "\nEnter data for Student or Teacher or Employee (s/t/e)? ";
cin >> input;
do
{
if( input=='t' )
myarr[var] = new Teacher;
else if (input == 'e')
myarr[var] = new Employee;
else
myarr[var] = new Student;
myarr[var++]->getdata();
cout << " Enter another (y/n)? ";
cin >> input;
if(input == 'n')
{
goto start;
}
}
while( input =='y');
for(int j=0; j<var; j++)
myarr[j]->setdata();
cout << endl;
break;
case 2:
return 0;
break;
case 3:
return 0;
break;
case 4:
cout << "Are you sure you want to quit? " << endl;
getch();
break;
default:
cout << "Choose from 1 - 4 ONLY!!" << endl;
goto start;
break;
}
return 0;
}
Then I would try declared it protected, and instead of calling main::getdata(), you directly write data into the file. see if that works.