【发布时间】:2021-06-21 11:11:56
【问题描述】:
这是我的代码:-
#include<iostream>
using std::string;
//bydefault private cant access attributes outside class
class Employee{
public:
string name;
string company;
int age=0;
void Introduce() {
std::cout << "my age is- " << age << std::endl;
}
Employee(string company, string name, int age) {
name = name;
company = company;
age = age;
}
};
int main() {
Employee emp1 = Employee("bww","susmit",24);
emp1.Introduce();
//Employee emp2;
//same example
}
输出是我的年龄是0
我希望它是我输入 emp1 args 的内容
请帮忙。
【问题讨论】:
-
你希望像
age = age;这样的作业能做什么? -
age = age;-- 我很惊讶你没有对此感到困惑,因为你有一个同名的参数和一个成员变量。 -
你应该改变你的编码风格,让参数和成员有不同的名字。
标签: c++ visual-studio class constructor scope