How to pass variables to a class - c++

I have been working on this code for quite some time now and I had posted it before but then after fixing that problem another problem arose so I created a new post with the name of this problem. Ok the problem is that I am obviously not passing the variables to the Administrator class the right way. I have tried two ways which is all my book shows and both have given me an error that says error C2512: 'SalariedEmployee' : no appropriate default constructor available". I have tried
//Lynette Wilkins
//Week 12
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
class SalariedEmployee
{
private:
double wageRate;
int hours;
protected:
string name;
string ssn;
double netPay;
string department;
public:
SalariedEmployee(string n, string s, double np, double w, int h, string d);
~SalariedEmployee() {cout<<endl;}
string Getname(); //returns name
string Getssn(); // returns social security number
double GetnetPay(); //returns netPay
string Getdepartment(); // returns department
double GetwageRate(); //returns wage rate
int Gethours(); //returns hours
void Setname(string); //sets name
void Setssn(string); //sets ssn
void SetnetPay(double); //sets net pay
void Setdepartment(string); //sets department
void SetwageRate(double); //sets wage rate
void Sethours(int); //sets hours
};
SalariedEmployee::SalariedEmployee(string n, string s, double np, double w, int h, string d) : name(n),
ssn(s),
netPay(np),
wageRate(w),
hours(h),
department(d)
{}
string SalariedEmployee::Getname()
{
return name;
}
string SalariedEmployee::Getssn()
{
return ssn;
}
double SalariedEmployee::GetnetPay()
{
return netPay;
}
double SalariedEmployee::GetwageRate()
{
return wageRate;
}
int SalariedEmployee::Gethours()
{
return hours;
}
void SalariedEmployee::Setname(string n)
{
name = n;
}
void SalariedEmployee::Setssn(string s)
{
ssn = s;
}
void SalariedEmployee::SetnetPay(double np)
{
netPay = np;
}
void SalariedEmployee::Setdepartment(string d)
{
department = d;
}
void SalariedEmployee::SetwageRate(double w)
{
wageRate = w;
}
void SalariedEmployee::Sethours(int h)
{
hours = h;
}
class Administrator : public SalariedEmployee
{
protected:
string title;
string responsi;
string super;
double salary;
public:
Administrator(string t, string r, string s, double sa);
~Administrator();
string Gettitle();
string Getresponsi();
string Getsuper();
double Getsalary();
void Settitle(string);
void Setresponsi(string);
void Setsuper(string);
void Setsalary(double);
void print();
};
Administrator::Administrator(string t, string r, string s, double sa) : title(t), responsi(r), super(s), salary(sa)
{
}
Administrator::~Administrator()
{
cout<<endl;
}
string Administrator::Gettitle()
{
return title;
}
string Administrator::Getresponsi()
{
return responsi;
}
string Administrator::Getsuper()
{
return super;
}
double Administrator::Getsalary()
{
return salary;
}
void Administrator::Settitle(string ti)
{
title = ti;
}
void Administrator::Setresponsi(string re)
{
responsi = re;
}
void Administrator::Setsuper(string su)
{
super=su;
}
void Administrator::Setsalary(double sa)
{
salary= sa;
}
void Administrator::print( )
{
cout << "\n_______________________________________________\n";
cout << "Pay to the order of " << name<< endl;
cout << "The sum of " << netPay << " Dollars\n";
cout << "_________________________________________________\n";
cout <<endl<<endl;
cout << "Employee Number: " << ssn << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
int main()
{
string name;
string soc;
double net = 0;
double wage = 0;
int hrs = 0;
string dept;
string admtitle;
string resp;
string sup;
double sal = 0;
int response = 0;
string date = "January 12, 2013";
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
SalariedEmployee emp1(name, soc,net, wage, hrs, dept);
Administrator adm1(admtitle, resp, sup, sal);
while(response != 4){
cout<<"Employee and Administrator Salary Program "<<endl;
cout<<"(You will have to enter data first before you do anything else)"<<endl<<endl;
cout<<"Enter Employee Data, Enter 1"<<endl;
cout<<"Change data, Enter 2"<<endl;
cout<<"Print Check, Enter 3"<<endl;
cout<<"End Program, Enter 4"<<endl<<endl;
cout<<"Please make your selection"<<endl;
cin>> response;
switch (response)
{
case 1:
cout <<"The employee's data will be entered here: "<<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 2:
cout<<"Please change the data you entered previously here. " <<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 3:
cout <<"Information Printed"<<endl<<endl;
cout<<"_____________________________"<<date<<endl;
&Administrator::print;
break;
default:
cout<<endl<<endl
<<"Invalid Selection! Try Again"<<endl;
exit(1);
}
}
system("PAUSE");
return 0;
}

Administrator(string t, string r, string s, double sa); will attempt to call the default constructor of the base class if you don't specify another. (a default constructor is one that can be called without any arguments)
The base class doesn't have a default constructor, ergo the error.
To call another constructor of the base class:
Administrator::Administrator(string t, string r, string s, double sa) :
SalariedEmployee(<args>), //base constructor call
title(t), responsi(r), super(s), salary(sa) //members
{
}

Related

My c++ code gives the following error (error: no matching function for call to ‘Bank::Bank()’) [duplicate]

This question already has answers here:
Default constructor error no matching function for call to
(2 answers)
C++ array of a self-defined class, no matching function call
(3 answers)
no matching function to call for "constructor"
(1 answer)
Closed last year.
I'm a beginner in c++. Though the code is still incomplete, I would like to know why I'm not able to create an array to store my objects from the class. I have to store 5 bank accounts in an array and I was trying to do so by soring the objects but it keeps showing error.
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class Bank
{
string depositor;
int accno;
char type;
float balance;
public:
Bank(string depositor, int accno, char type, float balance); //to assign initial values
void deposit(); //to deposit amount
float withdraw(); //to withdraw amount
void show(); //to show name and balance
Bank(string depositor, int accno); //constructor function for name and account no.
Bank(float balance, int accno); //constructor function for balance and account no.
Bank(char type, int accno); //constructor function for type and account no.
Bank(const Bank&); //copy constructor
//getter and setter functions for all data members
void setname(string depositor);
void setacc(int accno);
void settype(char type);
void setbal(float balance);
string getname();
int getacc();
char gettype();
float getbal();
};
Bank::Bank(string depos, int acno, char typ, float bal)
{
depositor=depos;
accno = acno;
type = typ;
balance = bal ? bal : 0;
}
void Bank::deposit()
{
float damt1;
cout << "Enter deposit amount: ";
cin >> damt1;
if (damt1 < 0.0) {
cout << "Can't deposit negative amount." << endl;
damt1 = 0.0;
}
balance += damt1;
}
float Bank::withdraw()
{
int amount;
cout << "Enter withdrawal amount: ";
cin >> amount;
if (amount < 0.0) {
cout << "Negative amount can't be withdrawn" << endl;
amount = 0;
}
if (amount > balance - 1000.0) {
cout << "Not enough balance.";
}
balance -= amount;
return amount;
}
Bank::Bank(string name, int no)
{
depositor = name;
accno = no;
}
Bank::Bank(float bal, int no)
{
balance = bal;
accno = no;
}
Bank::Bank(char ty, int no)
{
type = ty;
accno = no;
}
Bank::Bank(const Bank& p)
{
balance = p.balance;
accno = p.accno;
}
void Bank::setname(string name)
{
depositor = name;
}
void Bank::setacc(int n)
{
accno = n;
}
void Bank::settype(char ty)
{
type = ty;
}
void Bank::setbal(float bal)
{
balance = bal?bal:0;
}
string Bank::getname()
{
return depositor;
}
int Bank::getacc()
{
return accno;
}
char Bank::gettype()
{
return type;
}
float Bank::getbal()
{
return balance;
}
void Bank::show()
{
cout << "Name: " << depositor<<endl;
cout << "Account number: " << accno<<endl;
cout << "Type: " << type<<endl;
cout << "Balance: " << balance<<endl;
}
int main()
{
Bank acct[5];//This is the line with error.I am unable to complete the code bcoz of this
int acno,i;
char ty;
string name;
float bal;
for (i=0;i<5;i++){
cout << "Enter details: \n";
cout << "name: ";
cin >> name;
cout << "\nEnter accno: ";
cin >> acno;
cout << "\nEnter type: ";
cin >> ty;
cout << "\nEnter balance: ";
cin >> bal;
Bank b1(name, acno, ty, bal);
}
return 0;
}
Can someone help me with what corrections I should make?

PrintInfo() Giving Me A Hard Time

So I'm studying with classes and and I'm making a program that prints user entered data, however I can't seem to get past this one compiler error. The gist of the program is to take student user data and output it.
#include<iostream>
#include<string>
using namespace std;
class Student {
public:
Student();
~Student();
void SetName(string studentname); //mutator for name
void SetId(int studentid); //mutator for id
void SetGender(string studentgender); //mutator for gender
void SetEthnicity(string studentethnicity); // mutator for ethnicity
void SetMajor(string studentmajor); //mutator for major
void SetMinor(string studentminor); //mutator for minor
void SetGPA(float studentgpa); // mutator for gpa
string GetName(); //accessor for name
int GetId(); //accessor for id
string GetGender(); //accesor for gender
string GetEthnicity(); // accessor for ethnicity
string GetMajor(); // accessor for major
string GetMinor(); //accessor for minor
float GetGPA(); // accessor for GPA
void PrintInfo(void); // print student information
private:
string Name;
int id_number;
string gender;
string ethnicity;
string major;
string minor;
float gpa;
};
Student::Student()
{
Name = " ";
id_number = 0;
gender = " ";
ethnicity = " ";
major = " ";
minor = "None";
gpa = 0;
}
//mutator functions
void Student::SetName(string studentname)
{
Name = studentname;
};
void Student::SetId(int studentid)
{
id_number = studentid;
}
void Student::SetGender(string studentgender)
{
gender = studentgender;
}
void Student::SetEthnicity(string studentethnicity)
{
ethnicity = studentethnicity;
}
void Student::SetMajor(string studentmajor)
{
major = studentmajor;
}
void Student::SetMinor(string studentminor)
{
minor = studentminor;
}
void Student::SetGPA(float studentgpa)
{
gpa = studentgpa;
}
//accessor functions
string Student::GetName()
{
return Name;
}
int Student::GetId()
{
return id_number;
}
string Student::GetGender()
{
return gender;
}
string Student::GetEthnicity()
{
return ethnicity;
}
string Student::GetMajor()
{
return major;
}
string Student::GetMinor()
{
return minor;
}
float Student::GetGPA()
{
return gpa;
}
void Student::PrintInfo(void)
{
cout<<"Name: "<<myStudent.GetName()<<endl;
cout<<"ID #: "<<myStudent.GetId()<<endl;
cout<<"Gender: "<<myStudent.GetGender()<<endl;
cout<<"Ethnicity: "<<myStudent.GetEthnicity()<<endl;
cout<<"Major: "<<myStudent.GetMajor()<<endl;
cout<<"Minor: "<<myStudent.GetMinor()<<endl;
cout<<"GPA: "<<myStudent.GetGPA()<<endl;
}
int main()
{
int* studentarray;
studentarray = new int[5];
Student myStudent;
string names;
int ids;
string genders;
string ethnicities;
string majors;
string minors;
float gpas;
cout<<"Student Information."<<endl;
cout<<"Name: "<<endl;
getline (cin, names);
cout<<"ID Number: "<<endl;
cin>> ids;
cout<<"Gender: "<<endl;
getline(cin, genders);
cout<<"Ethnicity: "<<endl;
getline(cin, ethnicities);
cout<<"Major: "<<endl;
getline(cin, majors);
cout<<"Minor: "<<endl;
getline(cin, minors);
cout<<"Enter GPA: "<<endl;
cin>> gpas;
myStudent.SetName(names);
myStudent.SetId(ids);
myStudent.SetGender(genders);
myStudent.SetEthnicity(ethnicities);
myStudent.SetMajor(majors);
myStudent.SetMinor(minors);
myStudent.SetGPA(gpas);
myStudent.PrintInfo();
return 0;
}
Then this is my compiler error
In member function 'void Student::PrintInfo()':
117:18: error: 'myStudent' was not declared in this scope
myStudent does not exist inside Student::PrintInfo().
PrintInfo() is actually a member function, so it should look simply like this.
void Student::PrintInfo(void)
{
cout<<"Name: "<< GetName()<<endl;
cout<<"ID #: "<< GetId()<<endl;
cout<<"Gender: "<< GetGender()<<endl;
cout<<"Ethnicity: "<< GetEthnicity()<<endl;
cout<<"Major: "<< GetMajor()<<endl;
cout<<"Minor: "<< GetMinor()<<endl;
cout<<"GPA: "<< GetGPA()<<endl;
}
This one is even better (Edit: Better in this case. If the getter methods were to perform a more complex task than just retrieve the value, you would use them):
void Student::PrintInfo(void)
{
cout<<"Name: "<< Name<<endl;
cout<<"ID #: "<< id_number<<endl;
cout<<"Gender: "<< gender<<endl;
cout<<"Ethnicity: "<< ethnicity<<endl;
cout<<"Major: "<< major<<endl;
cout<<"Minor: "<< minor<<endl;
cout<<"GPA: "<< gpa<<endl;
}
Or, for clarity, you might prefer this format:
void Student::PrintInfo(void)
{
cout<<"Name: "<< this->Name<<endl;
cout<<"ID #: "<< this->id_number<<endl;
cout<<"Gender: "<< this->gender<<endl;
cout<<"Ethnicity: "<< this->ethnicity<<endl;
cout<<"Major: "<< this->major<<endl;
cout<<"Minor: "<< this->minor<<endl;
cout<<"GPA: "<< this->gpa<<endl;
}
As a side note, you should specify that your getter methods (and PrintInfo as well) don't modify the object.
int GetWhatever() const {} // <-- The const keyword right there

C++ Polymorphism Employee Project

For class I have to adapt a program I wrote last week for polymorphism. Last week it used a specific set of information for the employees but now I have to make it work with polymorphism as well as read/write data from a file, I am completely lost with what I am supposed to be doing, If someone could even point me in the right direction it would be so much help. I can post my current .h and .cpp file for a look at what I have as well as the instructions of what I am supposed to be doing.
.h
#pragma once
#include <string>
using namespace std;
class Employee {
private:
int employeeNumber; // Employee's employee number
string employeeName; //Employee's name
string streetAddress; //Employee's street address
string phoneNumber; //Employee's phone number
double hourlyWage; //Employee's hourly wage
double hoursWorked; //Employee's hours worked
double netPay; //Net pay
double grossPay; //Gross pay
public:
Employee();
Employee(int, string, string, string, double, double);
int getEmployeeNumber();
void setEmployeeNumber(int);
string getEmployeeName();
void setEmployeeName(string);
string getStreetAddress();
void setStreetAddress(string);
string getPhoneNumber();
void setPhoneNumber(string);
double getHourlyWage();
void setHourlyWage(double);
double getHoursWorked();
void setHoursWorked(double);
double calcPay()
{
const int OVER = 40;
double federal = 0.20;
double state = 0.075;
double timeHalf = 1.5;
double grossPay;
double netPay;
if (getHoursWorked() < OVER)
{
grossPay = getHoursWorked() * getHourlyWage();
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
if (getHoursWorked() >= OVER)
{
grossPay = getHoursWorked() * ((getHourlyWage() * timeHalf));
netPay = grossPay - (grossPay * federal) - (grossPay * state);
}
return netPay;
}
};
.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
#include <iomanip>
using namespace std;
Employee::Employee()
{
employeeNumber = 0; // Employee's employee number
employeeName = ""; //Employee's name
streetAddress = ""; //Employee's street address
phoneNumber = ""; //Employee's phone number
hourlyWage = 0; //Employee's hourly wage
hoursWorked = 0;
grossPay = 0;
netPay = 0;
}
Employee::Employee(int empNum, string empName, string streetAddress,
string phoneNumber, double hourlyWage, double hoursWorked)
{
employeeNumber = empNum;
employeeName = empName;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
grossPay = 0;
netPay = 0;
}
int Employee::getEmployeeNumber()
{
return employeeNumber;
}
void Employee::setEmployeeNumber(int empNum)
{
employeeNumber = empNum;
}
string Employee::getEmployeeName()
{
return employeeName;
}
void Employee::setEmployeeName(string empName)
{
employeeName = empName;
}
string Employee::getStreetAddress()
{
return streetAddress;
}
void Employee::setStreetAddress(string strtAddrs)
{
streetAddress = strtAddrs;
}
string Employee::getPhoneNumber()
{
return phoneNumber;
}
void Employee::setPhoneNumber(string phnNum)
{
phoneNumber = phnNum;
}
double Employee::getHourlyWage()
{
return hourlyWage;
}
void Employee::setHourlyWage(double hrWage)
{
hourlyWage = hrWage;
}
double Employee::getHoursWorked()
{
return hoursWorked;
}
void Employee::setHoursWorked(double hrWorked)
{
hoursWorked = hrWorked;
}
void printCheck(Employee ee)
{
cout << "\n\n--------------------- Fluff Shuffle Electronics -------------------------------- \n";
cout << " Pay to the order of " << ee.getEmployeeName() << "...........................$" << ee.calcPay();
cout << "\n\n United Bank of Eastern Orem \n";
cout << "------------------------------------------------------------------------------- \n";
cout << " Hours Worked: " << ee.getHoursWorked();
cout << "\n Hourly Wage: " << ee.getHourlyWage();
cout << endl << endl;
}//End of function
void read(ifstream &in)
{
Employee employees[10];
int counter = 0;
while (in.read((char *)&employees[counter++], sizeof(Employee)))
for (int i = 0; i<counter; i++)
{
printCheck(employees[i]);
}
in.close();
}
void write(ofstream &out)
{
Instantiate your employees here first, then call their functions.
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 10.00,
45.00);
printCheck(joe);
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 12.00,
30.00);
printCheck(sam);
Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 15.00, 40.00);
printCheck(mary);
out.write((char *)(&joe), sizeof(Employee));
out.write((char *)(&sam), sizeof(Employee));
out.write((char *)(&mary), sizeof(Employee));
out.close();
}
//Main function
int main()
{
int choice;
string filename;
while (true)
{
cout << "\nThis program has two options:\n";
cout << "1 - Create a data file, or\n";
cout << "2 - Read data from a file and print paychecks\n";
cout << "\n Press any other key to quit..........\n";
cout << "Please enter <1> to create a file or <2> to print
checks: ";
cin >> choice;
if (choice == 1)
{
cout << "Enter the file name: ";
cin >> filename;
ofstream out(filename);
out.open(filename.c_str(), ios::binary);
write(out);
}
else if (choice == 2)
{
cout << "Enter the file name: ";
cin >> filename;
ifstream in(filename);
in.open(filename.c_str(), ios::binary);
read(in);
}
else break;
//Calls function to displays information
}
}//End of main
These are the instructions for the project.
This is the diagram it refers to
To start: create two classes derived from Employee:
class HourlyEmployee: public Employee
{
};
class SalariedEmployee: public Employee
{
}
and move members related to Hourly working from Employee to HourlyEmployee, then add members related to Salary to SalariedEmployee (WeeklySalary).
This way (removing attributes related to hourly working) you make Employee class more general that can be a base for other kind of employees to (SalariedEmployee).
When you derive HourlyEmployee or SalariedEmployee from Employee, you mean they are kind of Employee, so members that Employee has, they will inherit automatically.

searching an array of type class

I am working on this C++ program that controls student information. I have been able to add and display the information of the student. I need to search the the array for a specific student by his ID, but it seems there is a problem assigning the array of type class to int variable in order to search for the ID?
#include <iostream>
#include <string>
#include <array>
#include <iomanip>
using namespace std;
class student{
private:
int id, age;
std::string fname, lname;
string cob;
char s;
public:
student()
{
id=age=0;
fname=lname=cob="";
}
void setid(int);
void setage(int);
void setfname(string);
void setlname(string);
void setsex(char);
void setcob(string);
int getid();
int getage();
string getfname();
string getlname();
string getcob();
char getsex();
};
void student::setfname(string n)
{ fname=n;}
void student::setlname(string n)
{ lname=n;}
void student::setcob(string n)
{ cob=n;}
void student::setid(int n)
{ id=n;}
void student::setage(int n)
{ age=n;}
void student::setsex(char n)
{ s=n;}
string student::getfname()
{ return fname;}
string student::getlname()
{ return lname;}
string student::getcob()
{ return cob;}
int student::getid()
{ return id;}
int student::getage()
{ return age;}
char student::getsex()
{ return s;}
std::array<student, 100> studentlist;
void addstd()
{
int id, age;
string fname, lname;
string cob;
char s;
for(int i=0;i<3;i++)
{
cout<<"Enter ID Number"<<endl;
cin>>id;
studentlist[i].setid(id);
cout<<"Enter First Name"<<endl;
cin>>fname;
studentlist[i].setfname(fname);
cout<<"Enter last Name"<<endl;
cin>>lname;
studentlist[i].setlname(lname);
cout<<"Enter age"<<endl;
cin>>age;
studentlist[i].setage(age);
cout<<"Enter student's Sex(F or M) "<<endl;
cin>>s;
studentlist[i].setsex(s);
cout<<"Enter Country of birth"<<endl;
cin>>cob;
studentlist[i].setcob(cob);
}
}
void displayinfo()
{
for(int i=0;i<3;i++)
{
cout<<"Student ID: "<<studentlist[i].getid()<<endl;
cout<<"First Name: "<<studentlist[i].getfname()<<endl;
cout<<"Last Name: "<<studentlist[i].getlname()<<endl;
cout<<"Student Age: "<<studentlist[i].getage()<<endl;
cout<<"Student Gender: "<<studentlist[i].getsex()<<endl;
cout<<"Student Birth City: "<<studentlist[i].getcob()<<endl;
cout<< "\n";
}
}
void searchstd()
{
int num;
cout<<"Enter ID of Student "<<endl;
cin>>num;
int temp;
for(int i=0;i<3;i++)
{
studentlist[i]=temp; // here is the problem!!
if(temp==num)
{
}
}
}
/*std::array<student, 100> studentlist;*/
void main()
{
/*
student s;
student std[100];
*/
student s;
int choice;
do{
cout << "-----------Menu------------" << endl;
cout << " 1. Add a student " << endl;
cout << " 2. Search for student by ID " << endl;
cout << " 3. Display all students information " << endl;
cout << " 4. Remove a students " << endl;
cout << " 5. Display students aged between 34 - 50 " << endl;
cout << " 6. Modify a student's information " << endl;
cout << " 7. Exit " << endl;
cout << " Enter your choice 1, 2, 3, 4 ,5,6,7 " << endl;
cin>>choice;
switch(choice) {
case 1:
addstd();
break;
case 2:
searchstd();
break;
case 3:
displayinfo();
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
exit(0);
break;
default:
cout << "Please enter 1, 2, 3, 4, 5, 6 or 7 : " << endl << endl;
}
}while(choice!=8);
}
studentlist[i]=temp; // here is the problem!!
studentlist[] is an array of objects of the class student, and temp is a variable of type int. This is invalid. You are trying to assign an uninitialized int, which could have any value, to an array of students.
I believe, by looking at your implementation, that you are looking for something like this (although this search could be improved):
int num;
cout<<"Enter ID of Student "<<endl;
cin>>num;
for(int i=0;i<3;i++)
{
if(studentlist[i].getid() == num)
{
// found student with id == num
}
}

How to send variables to class methods [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have some variables in my program that are actually in a case statement. I have tried to get them to go to my class functions but I keep getting an error. I am suppose to get the variables to go to the SalariedEmployee and the Administrator class.
//Lynette Wilkins
//Week 12
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
class SalariedEmployee
{
private:
double wageRate;
int hours;
protected:
string name;
string ssn;
double netPay;
string department;
public:
SalariedEmployee(string n, string s, double np, double w, int h, string d);
~SalariedEmployee() {cout<<endl;}
string Getname(); //returns name
string Getssn(); // returns social security number
double GetnetPay(); //returns netPay
string Getdepartment(); // returns department
double GetwageRate(); //returns wage rate
int Gethours(); //returns hours
void Setname(string); //sets name
void Setssn(string); //sets ssn
void SetnetPay(double); //sets net pay
void Setdepartment(string); //sets department
void SetwageRate(double); //sets wage rate
void Sethours(int); //sets hours
};
string SalariedEmployee::Getname()
{
return name;
}
string SalariedEmployee::Getssn()
{
return ssn;
}
double SalariedEmployee::GetnetPay()
{
return netPay;
}
double SalariedEmployee::GetwageRate()
{
return wageRate;
}
int SalariedEmployee::Gethours()
{
return hours;
}
void SalariedEmployee::Setname(string n)
{
name = n;
}
void SalariedEmployee::Setssn(string s)
{
ssn = s;
}
void SalariedEmployee::SetnetPay(double np)
{
netPay = np;
}
void SalariedEmployee::Setdepartment(string d)
{
department = d;
}
void SalariedEmployee::SetwageRate(double w)
{
wageRate = w;
}
void SalariedEmployee::Sethours(int h)
{
hours = h;
}
class Administrator : public SalariedEmployee
{
protected:
string title;
string responsi;
string super;
double salary;
public:
Administrator(string t, string r, string s, double sa);
~Administrator();
string Gettitle();
string Getresponsi();
string Getsuper();
double Getsalary();
void Settitle(string);
void Setresponsi(string);
void Setsuper(string);
void Setsalary(double);
void print();
};
Administrator::~Administrator()
{
cout<<endl;
}
string Administrator::Gettitle()
{
return title;
}
string Administrator::Getresponsi()
{
return responsi;
}
string Administrator::Getsuper()
{
return super;
}
double Administrator::Getsalary()
{
return salary;
}
void Administrator::Settitle(string ti)
{
title = ti;
}
void Administrator::Setresponsi(string re)
{
responsi = re;
}
void Administrator::Setsuper(string su)
{
super=su;
}
void Administrator::Setsalary(double sa)
{
salary= sa;
}
void Administrator::print( )
{
cout << "\n_______________________________________________\n";
cout << "Pay to the order of " << name<< endl;
cout << "The sum of " << netPay << " Dollars\n";
cout << "_________________________________________________\n";
cout <<endl<<endl;
cout << "Employee Number: " << ssn << endl;
cout << "Salaried Employee. Regular Pay: "
<< salary << endl;
cout << "_________________________________________________\n";
}
int main()
{
string name;
string soc;
double net = 0;
double wage = 0;
int hrs = 0;
string dept;
string admtitle;
string resp;
string sup;
double sal = 0;
int response;
string date = "January 12, 2013";
cout<<setprecision(2)
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint);
SalariedEmployee emp1(name, soc,net, wage, hrs, dept);
while(response != 4){
cout<<"Employee and Administrator Salary Program "<<endl;
cout<<"(You will have to enter data first before you do anything else)"<<endl<<endl;
cout<<"Enter Employee Data, Enter 1"<<endl;
cout<<"Change data, Enter 2"<<endl;
cout<<"Print Check, Enter 3"<<endl;
cout<<"End Program, Enter 4"<<endl<<endl;
cout<<"Please make your selection"<<endl;
cin>> response;
switch (response)
{
case 1:
cout <<"The employee's data will be entered here: "<<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 2:
cout<<"Please change the data you entered previously here. " <<endl<<endl;
cout<<"Enter the employees name: ";
cin.ignore();
getline(cin, name);
cout<<"Enter the employees social security number: ";
cin.ignore();
getline(cin, soc);
cout<<"Enter the employees net pay: ";
cin>>net;
cout<<"Enter the employees wage rate: ";
cin>>wage;
cout<<"Enter the number of hours the employer worked: ";
cin>>hrs;
cout<<"Enter the employees title: ";
cin.ignore();
getline(cin,admtitle);
cout<<"Enter the employees area responsibility: ";
cin.ignore();
getline(cin, resp);
cout<<"Enter the employees salary: ";
cin>>sal;
cout<<endl<<endl<<endl;
break;
case 3:
cout <<"Information Printed"<<endl<<endl;
cout<<"_____________________________"<<date<<endl;
&Administrator::print;
break;
default:
cout<<endl<<endl
<<"Invalid Selection! Try Again"<<endl;
exit(1);
}
}
system("PAUSE");
return 0;
}
You never defined the constructors for SalariedEmployee or Administrator. Another answerer showed how you might define an implementation of a constructor that matches the signature in your existing definition, but in your code, the values of those variables are meaningless when you instantiate the emp1 object anyway, so you'd do better to just use a default constructor where you simply initialize most variables to 0:
SalariedEmployee::SalariedEmployee() :
wageRate(0), hours(0), netPay(0) {}
Note that I didn't bother initializing the string members; they automatically initialize themselves to "" (nothing).
Also, when you input data with getline(), you're not doing anything with it. Call one of your setter functions to pass the value that you read from getline(cin,...) to your emp1 object. Your option '3' looks like it's supposed to print whatever you entered previously, but you're not calling any "print" function. Your code has &Administrator::print; but that doesn't print anything. That statement evaluates to the address of the print method of the Administrator class, but you don't do anything with that address, so that statement does nothing. You might want to call emp1.print(), but emp1 is an object of type SalariedEmployee, not Administrator, and there's no print() method in the SalariedEmployee class.
Has your class talked about virtual inheritance (polymorphism)? If so, then you're probably supposed to be declaring a print() method in your SalariedEmployee class, and then defining an implementation of it in Administrator. So in class SalariedEmployee, you'll want this:
void print() = 0;
Then, in class Administrator, define it just as you've done. But when you create your emp1 object, be sure to make its type be Administrator because SalariedEmployee is just an abstract base class (since you only declared that objects of types inherited from SalariedEmployee should have a print() method, but print() isn't actually defined in the SalariedEmployee class).
You didn't implement the constructor of SalariedEmployee. You need something like:
SalariedEmployee::SalariedEmployee(string n, string s, double np,
double w, int h, string d)
: name(n),
ssn(s),
netPay(np),
wageRate(w),
hours(h),
department(d)
{
}