带有set和get的c++程序
c++ program with set and get in class
哪里出了问题?但这就是我想做的:我想创建一个员工类,包括这些数据成员名字(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 ;
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址