如何获得“水平”从另一个班级获取私人信息的课程? [英] How do I get my "level" class to obatain private information from another class?
问题描述
我有一个名为data的类,我在其中执行一些文件,其中我有4个私有变量。我想在另一个名为level的类中使用这些数据。如何在另一个类中使用这些私有变量?
我尝试了什么:
我试过了通过一个函数返回值来访问一个对象但是它说了一些关于<< operator的东西,我认为它与重载<<<<但我不知道怎么做,也不知道它是否适用于类
从基础开始:
< a href =https://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm> C ++中的变量范围 [ ^ ]
C ++类和对象 [ ^ ]
一个明显的解决方案,已经建议,正在实施getter
和setter
(如果你是新的)对类data
中的变量读取和写入访问方法,然后在类级别
中使用它。
#include < iostream >
使用 命名空间标准;
class 数据
{
int d; // 私有变量d
public :
data( int d):d(d){}
int get_d() const { return d;} // 授予对d的读取权限
void set_d( int d){此 - > d = d;} // 授予对d
}的写入权限;
class 级别
{
public :
// 对d的读取权限
void show_d( const data& dt)
{
cout<< dt.get_d()<< ENDL;
}
// 写入d
void inc_d(data& dt)
{
dt.set_d(dt.get_d()+ 1 < /跨度>);
}
};
int main()
{
data dt( 5 < /跨度>);
等级lv;
lv.show_d(dt);
lv.inc_d(dt);
lv.show_d(dt);
}但是,这种方式,私有变量(通过getter和setter)暴露给所有
数据
类使用者。
另一种方法是使用朋友 [ ^ ]说明者授予级别
对所有数据
类成员的类完全访问权限。
#include < iostream >
使用 命名空间标准;
class 级别; // 级别类的前向声明
class 数据
{
int d; // 私有变量d
public :
data( int d):d(d){}
friend class 级别; // 授予级别对私人成员的完全访问权限
};
class 级别
{
public :
// 对d的读取权限
void show_d( const data& dt)
{
cout<< dt.d<< ENDL;
}
// 写入d
void inc_d(data& dt)
{
++ dt.d;
}
};
int main()
{
data dt( 5 < /跨度>);
等级lv;
lv.show_d(dt);
lv.inc_d(dt);
lv.show_d(dt);
}
I have a class named "data" in which i perform some work with files where i have 4 private variables.I want to use this data in another class named "level".How can i use those private variables in another class?
What I have tried:
I've tried getting acces with an object by returning the value via a function but it says something about the "<< operator",i assume that it has something to do with overloading the "<<" but i don't know how to do that and don't know if it works between classes
Start with basics:
Variable Scope in C++[^]
C++ Classes and Objects[^]
An obvious solution, already suggested, is implementinggetter
andsetter
(if you new both read and write access to the variables) methods in classdata
and then use it in classlevel
.
#include <iostream> using namespace std; class data { int d; // the private variable d public: data(int d):d(d){} int get_d() const {return d;} // grant read access to d void set_d(int d){this->d = d;} // grant write access to d }; class level { public: // read access to d void show_d(const data & dt) { cout << dt.get_d() << endl; } // write access to d void inc_d(data & dt) { dt.set_d( dt.get_d() + 1); } }; int main() { data dt(5); level lv; lv.show_d( dt); lv.inc_d(dt); lv.show_d(dt); }This way, however, the private variable is exposed (via getter and setter) to all the
data
class consumers.
Another approach uses the friend[^] specifier to grant tolevel
class full access to alldata
class members.
#include <iostream> using namespace std; class level; // forward declaration of level class class data { int d; // the private variable d public: data(int d):d(d){} friend class level; // grant to level class full access to private members }; class level { public: // read access to d void show_d(const data & dt) { cout << dt.d << endl; } // write access to d void inc_d(data & dt) { ++dt.d; } }; int main() { data dt(5); level lv; lv.show_d( dt); lv.inc_d(dt); lv.show_d(dt); }
这篇关于如何获得“水平”从另一个班级获取私人信息的课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!