如何在c++中访问不同类的方法中的变量

  • 本文关键字:同类 方法 变量 访问 c++ c++
  • 更新时间 :
  • 英文 :


如何访问不同类中方法中的变量,例如本代码中的变量。

#include <iostream>
using namespace std;
class Student
{
public:
void Addition()
{
int c=17;
}
};
int main()
{

}

如何在不返回值的情况下访问Student类中Addition方法中的变量c

感谢阅读。

祝你度过美好的一天(:

您可以在类中使用setter和getter,然后使用它们在主中实现您想要的内容

private:
int c;
public:
void setIntC(int intC) { c = intC; } 
int getIntC() const { return c; } 

最新更新