-3

私のプログラムには、実際には case ステートメントにある変数がいくつかあります。それらをクラス関数に移動させようとしましたが、エラーが発生し続けます。SalariedEmployee および Administrator クラスに移動する変数を取得するとします。

//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;
}
4

2 に答える 2

2

SalariedEmployee または Administrator のコンストラクターを定義していません。別の回答者は、既存の定義の署名に一致するコンストラクターの実装を定義する方法を示しましたが、コードでは、emp1とにかくオブジェクトをインスタンス化すると、これらの変数の値は無意味になるため、単にほとんどの変数を 0 に初期化するだけのデフォルト コンストラクター:

SalariedEmployee::SalariedEmployee() :
    wageRate(0), hours(0), netPay(0) {}

わざわざstringメンバーを初期化していないことに注意してください。自動的に "" (なし) に初期化されます。

また、 でデータを入力しgetline()ても、何もしていません。セッター関数の 1 つを呼び出して、読み取った値をgetline(cin,...)emp1 オブジェクトに渡します。オプション「3」は、以前に入力したものを印刷することになっているように見えますが、「印刷」機能を呼び出していません。あなたのコードは持っていますが&Administrator::print;、それは何も印刷しません。そのステートメントはクラスのprintメソッドのアドレスに評価されますがAdministrator、そのアドレスに対して何もしないので、そのステートメントは何もしません。を呼び出したいと思うかもしれませんがemp1.print()、それはではなくemp1型のオブジェクトであり、クラス にはメソッドがありません。SalariedEmployeeAdministratorprint()SalariedEmployee

クラスで仮想継承 (ポリモーフィズム) について話しましたか? もしそうなら、あなたはおそらくprint()あなたのクラスでメソッドを宣言しSalariedEmployee、次にその実装をAdministrator. ではclass SalariedEmployee、次のようになります。

void print() = 0;

次に、 でclass Administrator、行ったのと同じように定義します。ただし、emp1オブジェクトを作成するときは、その型を必ず抽象基本クラスにする必要があります (継承された型のオブジェクトには print() メソッドが必要であると宣言しただけAdministratorで、print() は実際には定義されていないため)クラス) 。SalariedEmployeeSalariedEmployeeSalariedEmployee

于 2013-01-10T21:22:53.380 に答える
1

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)
{
}
于 2013-01-10T21:18:37.017 に答える