PrintInfo() Giving Me A Hard Time - c++

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

Related

Exception has occured, unknown signal error when using class object again inside each function

I'm trying to write a C++ code for a course I'm enrolled in, where I keep the information of the students enrolled in the course.
I should be able to add a student to the classrrom in the user interface written in main , by calling the function void addNewStudent(int ID, string name, string surname), where I create my object instances, Student, and Course inside the function.
I should also be able to search by given ID by calling the function void showStudent(int ID) in the main, where the function uses the getStudent(ID) method of the object of the classCourse
I did not write all the methods, but when I try to debug this code, I got the error " Exception has occured, unknown signal error."
My questions are:
What is the reason of this error? How can I fix it?
Suppose that the user interface in the main is necessary to use as well as the functions it calls. Do I have to create a class object again inside each function as I wrote?
Can a more effective implementation be made in accordance with the object oriented principles I have defined above?
#include <iostream>
using namespace std;
#define MAX 10
class Student {
private:
int ID;
string name;
string surname;
public:
Student()
{
ID = 0;
string name = "" ;
string surname = "";
}
void setID(int ID_set);
int getID();
void setName(string name_set);
string getName();
void setSurName(string surname_set);
string getSurName();
};
class Course {
private:
Student students[MAX];
int num =0 ; // The current number of students in the course, initially 0.
float weightQ;
float weightHW;
float weightF;
public:
Course()
{
students[num] = {};
weightQ = 0.3;
weightHW = 0.3;
weightF = 0.4;
}
int getNum(); // Returns how many students are in the course
void addNewStudent(Student new_student);
void updateWeights(float weightQ_update, float weightHW_update, float weightF_update);
void getStudent(int ID_given);
};
// Method declerations for the class Student
void Student :: setID(int ID_set){
ID = ID_set;
}
int Student :: getID(){
return ID;
}
void Student :: setName(string name_set){
name = name_set;
}
string Student :: getName(){
return name;
}
void Student :: setSurName(string surname_set){
surname = surname_set;
}
string Student :: getSurName(){
return surname;
}
// Method declerations for the class Course
int Course :: getNum(){
return num;
}
void Course :: addNewStudent(Student new_student){
students[num] = new_student ;
num = num + 1;
}
void Course :: updateWeights(float weightQ_update, float weightHW_update, float weightF_update){
weightQ = weightQ_update;
weightHW = weightHW_update;
weightF = weightF_update;
}
void Course :: getStudent(int ID_given){
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
void addNewStudent(int ID, string name, string surname){
Student student;
Course ECE101;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
ECE101.addNewStudent(student);
}
void showStudent(int ID){
Course ECE101;
ECE101.getStudent(ID);
}
int main(){
Course ECE101;
cout << "Welcome to the ECE101 Classroom Interface"<<"\n";
cout << "Choose your option\n";
string option_1 = "1) Add a student ";
string option_2 = "2) Search a student by ID";
cout << "Enter your option: ";
int x;
int ID;
string name, surname;
cin >> x;
if (x == 1)
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname);
return 0;
}
 To make the menu more interactive you could add a do while statement that would accept 3 options:
register
show data
exit
int main(){
Course ECE101;
int x;
int ID;
string name, surname;
string option_1 = "1) Add a student\n";
string option_2 = "2) Search a student by ID\n";
cout << "Welcome to the ECE101 Classroom Interface\n";
cout << "Choose your option\n";
cout << option_1 << option_2;
cin >> x;
do {
if (x == 1) {
cout << "Enter the student ID ";
cin >> ID;
cout << endl;
cout << "Enter the student name ";
cin >> name;
cout << endl;
cout << "Enter the student surname " ;
cin >> surname;
addNewStudent(ID, name, surname, ECE101);
}
else {
cout << "Enter the student ID\n";
cin >> ID;
showStudent(ID, ECE101);
}
cout << "Choose your option\n";
cin >> x;
} while(x != 3);
return 0;
}
addnewStudent() and showStudent() methods now accepts an instance of Course as an argument to be able to add students.
void addNewStudent(int ID, string name, string surname, Course &course) {
Student student;
student.setID(ID);
student.setName(name);
student.setSurName(surname);
course.addNewStudent(student);
}
void showStudent(int ID, Course &course) {
course.getStudent(ID, course);
}
the function is modified from the same class as well.
void Course::getStudent(int ID_given, Course &course) {
for(int i = 0; i<MAX; i++){
if(ID_given == students[i].getID()){
cout << "Student Name & Surname : " << students[i].getName() << " " << students[i].getSurName()<<"\n";
}
}
}
Demo
Your addNewStudent function creates a new course everytime it is called. You could pass a reference to the course as a parameter into the function and call Course.addNewStudent(student). You'll want to make sure you specify it's a reference though when you define your function or you'll just create a copy of the course.

Dynamically creating an array from class c++

Throwing this out there first I'm a still learning how to program in school. I'm having an issue reading in to a dynamically created array with a pointer to one of my classes. The function readClassArray() isn't getting the variable back from student.getCreditNumber. The program complies fine in Visual Studio but when I get the the readClassArray it just skips over the function because s.getCreditNumber returns 0.
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;
class Courses{
private:
int courseNumber;
double hours;
string courseName;
char grade;
public:
void setCourseNumber(int n){courseNumber = n; }
void setCreditHours(double c) { hours = c; }
void setCourseName(string n) { courseName = n; }
void setGrade(char g) { grade = g; }
int getCourseNumber() { return courseNumber; }
double getCreditHours() { return hours; }
string getCourseName() { return courseName; }
char getGrade() { return grade; }
};
class Student : public Courses{
private:
string firstName;
string lastName;
string studentNumber;
int creditNumber;
double gpa;
public:
Courses * courses;
Student() {
firstName = " ";
lastName = " ";
studentNumber = " ";
creditNumber = 0;
gpa = 0.0;
courses = NULL;
}
~Student() {
delete[] courses;
};
void setFirstName(string n) { firstName = n; }
void setLastName(string l) { lastName = l; }
void setStudentNumber(string a) { studentNumber = a; }
void setCreditNumber(int num) { creditNumber = num; }
string getFirstName() { return firstName; }
string getLastName() { return lastName; }
string getStudentNumber() { return studentNumber; }
int getCreditNumber() { return creditNumber; }
};
#endif
Student.cpp
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
void readStudent();
void readCourseArray();
void computeGPA();
void printSummary();
void readStudent() {
Student a;
string number;
string firstName;
string lastName;
int courses;
cout << "Enter student number: ";
cin >> number;
a.setStudentNumber(number);
cout << "Enter student first name: ";
cin >> firstName;
a.setFirstName(firstName);
cout << "Enter student last name: ";
cin >> lastName;
a.setLastName(lastName);
cout << "Enter student number of courses: ";
cin >> courses;
a.setCreditNumber(courses);
cout << "\n"; }
void readCourseArray(){
Student s;
s.courses = new Courses[s.getCreditNumber()];
int num;
double cHours;
string cName;
char grade;
cout << "test" << endl;
for (int i = 0; i < s.getCreditNumber(); i++){
cout << "Enter class " << i + 1 << " number: ";
cin >> num;
s.courses[i].setCourseNumber(num);
cout << "Enter class " << i + 1 << " name: ";
cin >> cName;
s.courses[i].setCourseName(cName);
cout << "Enter class " << i + 1 << " hours: ";
cin >> cHours;
s.courses[i].setCreditHours(cHours);
cout << "Enter class " << i + 1 << " grade: ";
cin >> grade;
s.courses[i].setGrade(grade);
cout << "\n";
}
}
At the start of readCourseArray you've created s. When that happens the value of the creditNumber member is 0 as set by the default constructor. You need to do something to set it to a non-zero value. If you're expecting the value set in readStudent to carry over you need to plumb the two functions together. Either pass in a Student object as a reference to each function, or have readStudent return a Student object and pass that to readCourseArray.

Localize my Constructor

class Student
{
private:
string name;
int year;
string semester;
int AmtClass;
static string Year[4];
public:
Student();
Student(int AmtClass);
Student(Student &);
void setName(string name);
void setYear(int year);
void setSemester(string semester);
void setAmtClass(int AmtClass);
string getName();
int getYear();
string getSemester();
int getAmtClass();
~Student()
{
if(AmtClass > 0)
delete [] course;
}
};
string Student::Year[4] = { "Freshman", "Sophomore", "Junior", "senior" };
Student::Student()
{
name = "";
year = 0;
semester = "";
AmtClass = 0;
}
Student::Student(int amount)
{
AmtClass = amount;
string *pName;
pName = new string[AmtClass];
for(int i = 0; i < AmtClass; i++)
{
pName[i] = "";
}
}
Skipping Accessors and Mutator functions...
void readStudentData(Student &);
void printStudentData(Student);
int main()
{
int amount;
cout << "How many courses are you currently taking? ";
cin >> amount;
Student kid;
kid.setAmtClass(amount);
readStudentData(kid);
}
void readStudentData(Student &kid)
{
cin.ignore(10000, '\n');
int amount = kid.getAmtClass();
string name = "";
string semester = "";
int year = 0;
cout << "What is your full name? ";
getline(cin,name);
cout << "\nHow many years have you been in college? ";
cin >> year;
cout << "\nWhat is your current semester? ";
getline(cin,semester);
Student kid1(amount);
cout << "Please enter the name of all your courses." << endl;
for(int i = 0; i < amount; i++)
{
cout << "Course #" << i+1 << " : ";
getline(cin,pName[i]);
}
}
Okay edited this program a bit after realizing my pName is a local variable in the constructor...I am supposed to create a constructor that received an integer parameter corresponding to the number of courses the student is taking.The function dynamically allocates the string array of courses and sets each element to "". And then I am supposed to use this to record the names of the courses the student is taking.
The Year array should simply be allocated to the amount, and that's it:
Student::Student(int amount)
: Year(new std::string[amount]()), AmtClass(amount)
{
}
You can also use std::vector for this so you do not have to deal with deleting the memory yourself:
class Student
{
std::vector<std::string> Year;
// ...
};
Student::Student(int amount)
: Year(amount), AmtClass(Year.size())
{
}

How to pass variables to a class

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
{
}

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)
{
}