数据显示和构造函数的问题

时间:2014-04-06 06:56:00

标签: c++ class object constructor

我在理解构造函数时遇到问题,这在使用字符串名称和字符串位置显示数据时会导致问题。删除名称和位置的第一个字符,并用“”(空格)初始化。

请忽略任何缩进问题等因为这是我第一次在这里发帖...

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;


class Employee
{
    private:
        string name ; // for employee's name
        int id ;      // for employee's id number
        string department ;  // for name department
        string position ; //for position employee holds

    public:

        Employee()  // constructor
        {
            name = " " ;
            id = 0 ;
            department = " " ;
            position = " " ;
        }

        Employee ( char a , int i )
        {
            name = a ;

            id = i ;

            position = " " ;

            department = " " ;

        }

        Employee ( char a , char b , char c , int i )
        {
            name = a ;

            id = i ;

            department = b ;

            position = c ;


        }

        Employee ( const Employee &obj )
        {
            name = obj.name ;

            department = obj.position ;

            position = obj.position ;

            id = obj.id ;

        }

        int set_Name( char a )
        {
            name = a ;

            return 0;

        }

        int set_Id( int i )
        {
            id = i ;

            return 0;

        }

        int set_Position( char b )
        {
            position = b ;

            return 0;

        }

        int set_Department( char d )
        {
            department = d ;

            return 0;
        }



        string get_Name( void ) const
        {
            return name ;

        }

        int get_Id( ) const
        {
            return id ;

        }

        string get_Position( void ) const
        {
            return position ;

        }

        string get_Department( void ) const
        {
            return department ;

        }



        int set_Info( char a , char b , char c , int i )
        {
            name = a ;

            id = i ;

            department = b ;

            position = c ;

            return 0;
        }

        void get_Info ()
        {
            cout << endl  << " Enter Employee name =  " ;
            cin.ignore();
            getline(cin , name);

            cout << " Enter id number of employee = " ;
            cin >> id ;


            cout << " Enter Employee department = " ;
            cin.ignore();
            getline(cin , department);

            cout << " Enter Emloyee position in company = ";
            cin.ignore();
            getline(cin , position);


        }

        void put_Info(  )
        {
            cout << "Employees info : - " ;
            cout << "   " << name << "   "  << id << "   " << department << "   " << position << endl ;

        }

        ~Employee()
        {
            cout << " destructor executed .... " ;
        }

};


int main()
{

    Employee obj1 , obj2 , obj3 , obj4 , obj5 ;

    obj1.get_Info();
    obj2.get_Info();
    obj3.get_Info();
    obj4.get_Info();
    obj5.get_Info();

    cout << endl;

    obj1.put_Info();
    obj2.put_Info();
    obj3.put_Info();
    obj4.put_Info();
    obj5.put_Info();

    return 0;
}

1 个答案:

答案 0 :(得分:0)

cin >> name;

之前使用cin >> department;cin.ignore();

您可以在cin.ignore()使用here

时删除getline