如何发送变量到类方法
我的程序中有一些变量实际上是在case语句中。我试图让他们去我的班级功能,但我不断收到错误。我想让变量去SalariedEmployee和管理员类。如何发送变量到类方法
//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;
}
您没有实施SalariedEmployee的构造函数。您需要如下所示:
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)
{
}
OOOOOH !!!好吧,让我回头看看程序然后 –
您从未为SalariedEmployee或Administrator定义过构造函数。另一个回答者说明了如何定义一个与现有定义中的签名匹配的构造函数的实现,但是在代码中,这些变量的值在实例化对象时毫无意义,因此您最好只使用默认的构造函数,你只需初始化大多数变量以0:
SalariedEmployee::SalariedEmployee() :
wageRate(0), hours(0), netPay(0) {}
请注意,我没有理会初始化string
成员;他们会自动初始化为“”(没有)。
此外,当你输入数据getline()
时,你没有做任何事情。调用其中一个setter函数,将您从getline(cin,...)
读取的值传递给emp1对象。你的选项'3'看起来应该是打印你之前输入的任何东西,但是你没有调用任何“打印”功能。您的代码有&Administrator::print;
,但不会打印任何内容。该陈述的评估结果为Administrator
类的print
方法的地址,但您不对该地址做任何事情,因此该陈述不做任何处理。您可能需要致电emp1.print()
,但emp1
是SalariedEmployee
类型的对象,而不是Administrator
,并且SalariedEmployee
类中没有print()
方法。
你的课是否谈过虚拟继承(多态)?如果是这样,那么你可能应该在SalariedEmployee
类中声明print()
方法,然后在Administrator
中定义它的实现。所以在class SalariedEmployee
,你会想这样的:
void print() = 0;
然后,在class Administrator
,定义它就像你已经做到了。但是当你创建你的对象时,请确保它的类型为Administrator
,因为SalariedEmployee
只是一个抽象基类(因为你只声明从SalariedEmployee
继承的类型的对象应该有print()方法,但print()isn'实际上在SalariedEmployee
类中定义)。
看到这个,我很困惑,因为我曾经想过一旦我向用户询问它所在的数据 - > \t SalariedEmployee emp1(name,soc,net,wage,hrs,dept ); - >和构造函数被定义。 –
@CarlaBridgesWilkins,它被宣布,但没有定义。无论如何,为了你想要发生的事情,你的班级必须存储引用作为其数据成员,这将是一个非常糟糕的解决方案。相反,构造函数通常会将您给它的数据复制到类中,或者基于它设置成员,而不实际复制它(例如'std :: vector'获取大小并将其数据设置为具有许多初始化值的元素)。 – chris
@CarlaBridgesWilkins - 你要求用户输入数据的代码中没有任何内容告诉系统将用户的响应放入'emp1'对象。啊!我看到你的错误观念在哪里:构造函数调用不会将变量'name','soc','net'等等与所有永恒的'emp1'对象绑定;它只是将这些变量的* current *值传递给对象的构造函数。如果这些值稍后发生变化,则这些更改不会传递给'emp1'对象。你必须通过调用它们来传递它们,例如'emp1.Setname(name);' – phonetagger
你“得到”什么“错误”? – phonetagger
main.obj:错误LNK2019:无法解析的外部符号“public:__thiscall SalariedEmployee :: SalariedEmployee(class std :: basic_string,class std :: allocator >,class std :: basic_string ,class std :: allocator >,double,double,int,class std :: basic_string ,class std :: allocator >)“(?? 0SalariedEmployee @@ QAE @ V $ $ basic_string @ DU $ $ char_traits @ D @ std @@ V $ $ allocator @ D @ 2 @@ std @@ 0NNH0 @ Z)在函数中引用_main 1>致命错误LNK1120:1个未解析的外部 –
该链接错误表示您的代码未定义SalariedEmployee :: SalariedEmployee()方法(即构造函数)。 – phonetagger