我的项目在C ++中使用Class时出错

问题描述 投票:-5回答:2

我是新来的。基本上我刚学会了如何在C ++中使用class。当我尝试打印出来时,这些值似乎只有0.任何人都可以帮助我吗?它打算打印出来:

Susan Myers 47899会计副总裁Mark Jones 39119 IT职位Joy Rogers 81774制造工程师

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
    private:
        string name; 
        int idNumber; 
        string department; 
        string position; 

    public:
        Employee()
        {
            name=" "; 
            idNumber=0; 
            department=" "; 
            position=" "; 
        }

        Employee(string, int, string, string)
        {
            int id; 
            string n,d,p; 

            name=n; 
            idNumber=id; 
            department=d; 
            position=p; 
        }

        Employee(string, int)
        {
            string n; 
            int id; 

            name=n; 
            idNumber=id; 
        }
    void setName(string)
    {
        string n; 
        name=n; 
    }
    void setId(int)
    {
        int id;
        idNumber=id; 
    }
    void setDepartment(string)
    {
        string d; 
        department=d; 
    }
    void setPosition(string)
    {
        string p; 
        position=p; 
    }
    string getName() const
    {
        return name; 
    }
    int getId() const
    {
        return idNumber; 
    }
    string getDepartment() const
    {
        return department; 
    }
    string getPosition() const
    {
        return position; 
    }

}; 


int main()
{

    Employee e1; 
    Employee e2; 
    Employee e3; 

    e1.setName("Susan Meyers"); 
    e2.setName("Mark Jones"); 
    e3.setName("Joy Rogers"); 


    e1.setId(47899); 
    e2.setId(39119); 
    e3.setId(81744); 

    e1.setDepartment("Accounting"); 
    e2.setDepartment("IT"); 
    e3.setDepartment("Manufacturing"); 

    e1.setPosition("Vice President"); 
    e2.setPosition("Programmer"); 
    e3.setPosition("Engineer"); 

    cout<<"---------------------------------------"<<endl; 
    cout<<"Name"<<setw(6)<<"ID Number"<<setw(10)<<"Department"<<setw(12)<<"Position"<<endl; 

    cout<<e1.getName()<<setw(6)<<e1.getId()<<setw(10)<<e1.getDepartment()<<setw(12)<<e1.getDepartment()<<endl; 
    cout<<e2.getName()<<setw(6)<<e2.getId()<<setw(10)<<e2.getDepartment()<<setw(12)<<e2.getDepartment()<<endl; 
    cout<<e3.getName()<<setw(6)<<e3.getId()<<setw(10)<<e3.getDepartment()<<setw(12)<<e3.getDepartment()<<endl; 


    return 0; 
}
c++ class
2个回答
4
投票

这是你依赖猜测而不是正确阅读C ++入门教材时得到的

Employee类的构造函数(除了我删除的空行之外),您定义为

Employee(string, int, string, string)
  {
        int id; 
        string n,d,p; 
        name=n; 
        idNumber=id; 
        department=d; 
        position=p; 
  }

具有以下效果。

  • 调用者传递的四个参数将被忽略,因为它们未被命名。
  • 四个默认初始化变量(idndp)在构造函数体的本地定义。 id将是未初始化的。其他人,因为他们是std::string,默认初始化(到一个空字符串)
  • 接下来的四个语句将这些变量复制到类成员中。结果是初始化idNumber具有未定义的行为(因为id未初始化)并且三个字符串被初始化为空字符串。

为了达到你想要的效果(我假设),将其更改为;

Employee(std::string n, int id, std::string d, std::string p)
{
    name=n; 
    idNumber=id; 
    department=d; 
    position=p; 
}

请注意,我用string的全名调用std::string。这允许删除using namespace std,其中(除其他外)是头文件中的BAD练习。

更好的是,将其更改为

Employee(const std::string &n, int id, const std::string &d, const std::string &p) : 
     name(n), idNumber(id), department(d), position(p)
{
}

它通过const引用传递字符串(避免使用std::strings的其他副本)并使用初始化列表而不是分配给构造函数中的成员。

类似的注释适用于Employee的所有成员函数,除了只有构造函数可以有初始化列表。


0
投票

Errors made

  1. 介绍

你的代码非常混乱,并且有很多无关紧要的东西。

  1. 句法

void setPosition(string){这里你的函数没有参数!什么是字符串?

Code

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee{
    public:
        string name; 
        int idNumber; 
        string department; 
        string position; 

    void setName(string n){ 
        name=n; 
    }
    void setId(int k){
        int id;
        idNumber=id; 
    }
    void setDepartment(string d){
        department=d; 
    }
    void setPosition(string p){
        position=p; 
    }
    string getName(){
        return name; 
    }
    int getId(){
        return idNumber; 
    }
    string getDepartment(){
        return department; 
    }
    string getPosition(){
        return position; 
    }
}; 
int main(){
    Employee e1; 
    Employee e2; 
    Employee e3; 
    e1.setName("Susan Meyers"); 
    e2.setName("Mark Jones"); 
    e3.setName("Joy Rogers"); 
    e1.setId(47899); 
    e2.setId(39119); 
    e3.setId(81744); 
    e1.setDepartment("Accounting"); 
    e2.setDepartment("IT"); 
    e3.setDepartment("Manufacturing"); 
    e1.setPosition("Vice President"); 
    e2.setPosition("Programmer"); 
    e3.setPosition("Engineer"); 
    cout<<"---------------------------------------"<<endl; 
    cout<<"Name"<<" "<<"ID Number"<<" "<<"Department"<<" "<<"Position"<<endl; 
    cout<<e1.getName()<<" "<<e1.getId()<<" "<<e1.getDepartment()<<" "<<e1.getPosition()<<endl; 
    cout<<e2.getName()<<" "<<e2.getId()<<" "<<e2.getDepartment()<<" "<<e2.getPosition()<<endl; 
    cout<<e3.getName()<<" "<<e3.getId()<<" "<<e3.getDepartment()<<" "<<e3.getPosition()<<endl; 
}

Output

---------------------------------------
Name ID Number Department Position
Susan Meyers 32767 Accounting Vice President
Mark Jones 32767 IT Programmer
Joy Rogers 32767 Manufacturing Engineer

Explanation

我已经将你的代码缩短了50%(显示你有多少冗余的东西),这是一个有效的代码。

void setDepartment(string d){
     department=d; 
}

这里,字符串d在函数中定义为参数。请注意,您的代码也会两次cout << department,我已经在上面的代码中为您更正了。希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.