带有set和get in class的c ++程序

时间:2015-10-18 10:39:21

标签: c++

在某个地方,我出错了似乎是什么问题?但这就是我想要做的:我想创建一个Employee类,其中包括这些数据成员的名字(类型字符串),姓氏(类型字符串)和月薪(类型int,也是初始化的构造函数)三个数据成员,为每个数据成员提供一个集合和一个get函数,如果月薪不是正数,我想将其设置为0,创建两个Employee对象并显示每个对象的年薪,并给每个Employee一个20%再次提高并显示每位员工的年薪

addDropdown

#include <iostream>
#include <string>
using namespace std;

class Employee {

public:

Employee(string f ,string l ,int m ){
setFName(f) ;
setLName(l) ;
setMSalary(m) ;
}

void setFName(string f){
fName = f ;
}

string getFName(){
return  fName ;
}

void setLName(string l){
lName = l ;
}

string getLName(){
return lName ;
}

void setMSalary(int m){
if ( m <0 ){
mSalary = 0 ;
    }
else {
mSalary = m ;
    }
}

intgetMSalary(){
returnmSalary ;
}

intySalary(){
return12 * mSalary ;
}

 intraise(){
 return (0.10 * ySalary()) + ySalary() ;
 }

private :

string fName ;
string lName ;
int mSalary ;
 }; 



int main(){

Employeeem1("SARA" , "SALEH" , 5000);
Employeeem2("Bayan" , "Khaled" , 8000 ) ;

cout<<"The yearly salary of first employee is "<< em1.ySalary() <<endl ;
cout<<"The yearly salary of second employee is "<< em2.ySalary()<<endl ;

cout<<"\nThe yearly salary of first employee is after raising "<<em1.raise()<<endl ;

cout<<"The yearly salary of second employee is after raising "<<em2.raise() <<endl ;
}

我收到这些错误

#include <iostream>
#include <string>
using namespace std ;

class Employee {

public:

Employee(string f ,string l ,int m ){
setFName(f) ;
setLName(l) ;
setMSalary(m) ;
}

void setFName(string f){
fName = f ;
}

string getFName(){
return fName ;
}

void setLName(string l){
lName = l ;
}

string getLName(){
return lName ;
}

void setMSalary(int m){
if ( m <0 ){
mSalary = 0 ;
    }
else {
mSalary = m ;
    }
}

int getMSalary(){
return mSalary ;
}

private :

string fName ;
string lName ;
intmSalary ;
};



int main(){

Employeeem1("SARA" , "SALEH" , 5000);
Employeeem2("Bayan" , "Khaled" , 8000 ) ;
int raise1 , raise2 ;

cout<<"The yearly salary of first employee is "<< em1.getMSalary()*12<<endl ;

cout<<"The yearly salary of second employee is "<< em2.getMSalary() *12<<endl ;

raise1 = (0.20 * em1.getMSalary()) + em1.getMSalary() ;
raise2 = (0.20 * em2.getMSalary()) + em2.getMSalary() ;

cout<<"\nThe yearly salary of first employee is after raising "<< raise1 * 12<<endl ;

cout<<"The yearly salary of second employee is after raising "<< raise*12<<endl ;
}

1 个答案:

答案 0 :(得分:2)

以下是第二个版本的语法更正。您只有与空间相关的小问题...请参阅内联评论

#include <iostream>
#include <string>
using namespace std ;

class Employee
{

public:

    Employee(string f , string l , int m )
    {
        setFName(f) ;
        setLName(l) ;
        setMSalary(m) ;
    }
    void setFName(string f)
    {
        fName = f ;
    }
    string getFName()
    {
        return fName ;
    }
    void setLName(string l)
    {
        lName = l ;
    }
    string getLName()
    {
        return lName ;
    }
    void setMSalary(int m)
    {
        if ( m < 0 )
        {
            mSalary = 0 ;
        }
        else
        {
            mSalary = m ;
        }
    }
    int getMSalary()
    {
        return mSalary ;
    }

private :
    string fName ;
    string lName ;
    //you forgot space after int
    int mSalary ;
};

int main()
{
    //you forgot space after  Employee
    Employee em1("SARA" , "SALEH" , 5000);
    Employee em2("Bayan" , "Khaled" , 8000 ) ;
    int raise1 , raise2 ;

    cout << "The yearly salary of first employee is " << em1.getMSalary() * 12 << endl ;

    cout << "The yearly salary of second employee is " << em2.getMSalary() * 12 << endl ;

    raise1 = (0.20 * em1.getMSalary()) + em1.getMSalary() ;
    raise2 = (0.20 * em2.getMSalary()) + em2.getMSalary() ;

    cout << "\nThe yearly salary of first employee is after raising " << raise1 * 12 << endl ;
    // I think you meant raise2 instead of raise
    cout << "The yearly salary of second employee is after raising " << raise2 * 12 << endl ;
}