带有set和get的c++程序

c++ program with set and get in class

本文关键字:c++ 程序 get set 带有      更新时间:2023-10-16

哪里出了问题?但这就是我想做的:我想创建一个员工类,包括这些数据成员名字(string类型),一个姓(string类型)和一个月工资(int类型,也是一个构造函数,初始化三个数据成员,提供一组和一个get函数为每个数据成员,如果月薪不是积极的,我想将它设置为0,创建两个员工对象和显示每个对象的年薪,并给每个员工加薪20%,并显示每个员工的年薪了

#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 ;
}

我得到这些错误

-------------------Configuration: Employee - Win32 Debug--------------------
Compiling...
Employee.cpp
C:UsersafaqDesktopdelEmployee.cpp(42) : warning C4183: 'intgetMSalary': member function definition looks like a ctor, but name does not match enclosing class
C:UsersafaqDesktopdelEmployee.cpp(46) : warning C4183: 'intySalary': member function definition looks like a ctor, but name does not match enclosing class
C:UsersafaqDesktopdelEmployee.cpp(50) : warning C4183: 'intraise': member function definition looks like a ctor, but name does not match enclosing class
C:UsersafaqDesktopdelEmployee.cpp(41) : error C2065: 'returnmSalary' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(45) : error C2065: 'return12' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(45) : warning C4552: '*' : operator has no effect; expected operator with side-effect
C:UsersafaqDesktopdelEmployee.cpp(49) : error C2065: 'ySalary' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(63) : error C2065: 'Employeeem1' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(64) : error C2065: 'Employeeem2' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(66) : error C2065: 'em1' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(66) : error C2228: left of '.ySalary' must have class/struct/union type
C:UsersafaqDesktopdelEmployee.cpp(67) : error C2065: 'em2' : undeclared identifier
C:UsersafaqDesktopdelEmployee.cpp(67) : error C2228: left of '.ySalary' must have class/struct/union type
C:UsersafaqDesktopdelEmployee.cpp(69) : error C2228: left of '.raise' must have class/struct/union type
C:UsersafaqDesktopdelEmployee.cpp(71) : error C2228: left of '.raise' must have class/struct/union type
C:UsersafaqDesktopdelEmployee.cpp(72) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
Employee.exe - 11 error(s), 5 warning(s)

以下是第二个版本的语法更正。你只有一些与空间有关的小问题……参见内联注释

#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 ;
}