如何将变量发送给类方法
How to send variables to class methods
我的程序中有一些变量实际上是在case语句中。我试图让他们去我的类函数,但我一直得到一个错误。我想让变量去salarieemployee和管理员类。
//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 << " Dollarsn";
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;
}
您从未定义过salarieemployee或Administrator的构造函数。另一位回答者展示了如何定义与现有定义中的签名匹配的构造函数的实现,但是在代码中,当您实例化emp1
对象时,这些变量的值是没有意义的,因此您最好使用默认构造函数,只需将大多数变量初始化为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
中,像您所做的那样定义它。但是,当您创建emp1
对象时,请确保将其类型设置为Administrator
,因为SalariedEmployee
只是一个抽象基类(因为您只声明了继承自SalariedEmployee
的类型的对象应该具有print()方法,但实际上并没有在SalariedEmployee
类中定义print())。
您没有实现salarieemployee的构造函数。您需要这样的内容:
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)
{
}
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 打印所有继承的类成员变量和方法
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- 如何在公共类方法中初始化公共变量
- 让类方法为每个实例保存不同的静态变量
- 类变量无法从类方法访问
- 在结构的构造函数中使用类方法赋值变量
- 将const指针(EVP_MD)保存到变量中,以将其重新使用为类方法(HMAC)
- 类方法变量,如果将它们存储在类本身中会更快吗?
- C++类方法包含对静态变量的未定义引用
- C++ - 类方法更改成员变量,但不在 main 中
- 将局部方法变量分配给类指针
- 有没有更好的方法可以从子类访问父类的变量?
- const 类方法是否阻止在类外部分配变量?
- 基于变量输入的泛型类方法计算
- 在 android 上从 Java 调用C++类方法或函数,而无需在每次调用时重新创建类/变量
- 是否可以使用自动类方法初始化自动变量?
- 类方法读取同一变量C++的不同值
- 一个类的成员变量可以用在另一个类方法中吗
- 访问类方法中的私有指针变量C++