附加值错误

Wrong addition values

在这个程序中,我接受了一些值并添加了它们。该程序正在接受值但显示错误的添加。

#include<iostream.h>
#include<conio.h>
class Employee
{
    char name[30];
    int id;
    public:
    void Getdata()
    {
        cout<<"Enter Name: ";
        cin>>name;
        cout<<"Enter ID: ";
        cin>>id;
    }
    Employee()
    {
    }
    void Putdata()
    {
        cout<<name<<endl;
        cout<<id<<endl;
    }
};
class Salary : public Employee
{
    int sal;
        int basic, hra, da, cla;
    public:
    void set()
    {
        cout<<"Enter Basic Pay: ";
        cin>>basic;
        cout<<"Enter HRA: ";
        cin>>hra;
        cout<<"Enter Da: ";
        cin>>da;
        cout<<"Enter CLA: ";
        cin>>cla;
    }
    Salary() : Employee()
    {
        sal = (basic+hra+da+cla) ;
    }
    void show()
    {
        cout<<"Salary: "<<sal;
    }
};
void main()
{
    clrscr();
    Salary s;
    s.Getdata();
    s.set();
    s.Putdata();
    s.show();
    getch();
}

我预计薪水为 13000,结果却得到薪水:11172。

您正在从默认构造函数中未初始化的成员计算 sal 的值:sal = (basic + hra + da + cla);。所以你的程序运行了一个未定义的行为。

这是因为构造函数在任何其他成员之前运行。要解决它,您应该先获取值,然后再计算:

Salary() : Employee(){
//  cout << basic << ", " << hra << ", " << da << ", " <<  cla << endl; // this line proves that hra, basic... are not initialized.
    set(); // assign values before calculate.
    sal = (basic + hra + da + cla);
}
  • 我建议从默认构造函数中删除这一行:sal = (basic + hra + da + cla); 并将其放入set。所以 set() 看起来像:

    void set(){
        cout << "Enter Basic Pay: ";
        cin >> basic;
        cout << "Enter HRA: ";
        cin >> hra;
        cout << "Enter Da: ";
        cin >> da;
        cout << "Enter CLA: ";
        cin >> cla;
        sal = (basic + hra + da + cla); // after getting values it is a good place here to process the sal.
    }
    
  • 另一件需要考虑的事情是 main 应该 return 一个不为空的整数。返回void是错误的。

  • 也不用字符串而是用std::string值得用:

    class Employee{
        std::string name;
        int id;
        // ...
    };
    
  • 也使 SalaryEmployee 派生是无关紧要的: 问问自己:"is a salray an employee?" 所以相关的是实现 Has-a 关系而不是 Is-a 因为员工确实有薪水,但他不是 salaray。

    class Salary {
        public:
            Salary() :
                sal{}, basic{}, hra{}, da{}, cla{} {
            }
            void set() {
                std::cout << "Enter Basic Pay: ";
                std::cin >> basic;
                std::cout << "Enter HRA: ";
                std::cin >> hra;
                std::cout << "Enter Da: ";
                std::cin >> da;
                std::cout << "Enter CLA: ";
                std::cin >> cla;
                sal = basic + hra + da + cla;
            }
            void show() {
                std::cout << "Salary: " << sal << std::endl;
            }
        private:
            int sal, basic, hra, da, cla;
    };
    
    class Employee {
        public:
            Employee() :
                name{}, id{}, sal{} {
            }
            void Getdata() {
                std::cout << "Enter Name: ";
                std::getline(std::cin, name);
                std::cout << "Enter ID: ";
                std::cin >> id;
                sal.set();
            }
            void Putdata() {
                std::cout << name << std::endl;
                std::cout << id << std::endl;
                sal.show();
            }
        private:
            std::string name;
            int id;
            Salary sal;
    };
    
    int main(){
        Employee emp{};
        emp.Getdata();
        emp.Putdata();
    
    }
    
  • 注意:我编写的代码无法在 "Turbo C++" 上编译。请放弃那个旧的和错误的编译器并使用一个新的复杂的。