저는 C++을 처음 사용하기 때문에 답변을 찾을 수 없습니다. 다음 코드를 작성하고 싶습니다.같은 멤버 함수로 다른 멤버를 어떻게 변경합니까?
#include <iostream>
using namespace std;
class Employee{
private:
string name;
string gender;
public:
void display();
void update(string);
Employee(string a, string b){
name = a;
gender = b;
};
~Employee(){};
};
void Employee::display(void){
cout << "Name: " << name << endl;
cout << "Gender: " << gender << endl;
}
void Employee::update(string a){
/*
a function that updates either the
name element or gender element
based on which element it is used by.
*/
}
int main(){
Employee employee1 ("Joe","Male");
Employee employee2 ("Jon","Male");
employee1.display();
employee2.display();
employee1.name.update("Mary"); // This is what I want to do: Same function
employee2.gender.update("Female"); // for different elements of same type
employee1.display();
employee2.display();
return 0;
}
어떻게해야합니까? 함수 오버로드에 대해 생각했지만 두 요소가 같은 형식입니다. 여분의 값을 전달하고 코드가 엉성하게 보이게하고 싶지 않습니다. 어떤 아이디어? 고맙습니다. 이 같은
"동일한 멤버 함수로 다른 요소를 어떻게 변경합니까?" - * 무엇? * ... 편집 : 아 글쎄, 지금 내가 가진 모든 것을 읽은 후에. 그러나 그 질문은 ... 잘 표현되지 않았습니다. – dom0
제 의견으로는 회원은 비공개이므로 수업 외에서는 회원이 이용할 수 없습니다. 그래서 당신은 당신의 메인에'employee.name'을 사용할 수 없습니다 ... – sop