[英]If a base class has private data members that are modified in a member function, how does a derived class use that function?
I'm working on a project to learn about object oriented programming. 我正在研究一个关于面向对象编程的项目。 From what I understand, when a class is derived from a base class, it can access all of the public member functions of the base class.
根据我的理解,当一个类派生自基类时,它可以访问基类的所有公共成员函数。 In C++ a public inheritance means that private members cannot be accessed.
在C ++中,公共继承意味着无法访问私有成员。 So what happens when a private member function is modified in a base class?
那么在基类中修改私有成员函数会发生什么?
I have tried building some test programs to see what happens, but I'm still extremely confused. 我已经尝试构建一些测试程序来看看会发生什么,但我仍然非常困惑。 I also tried looking online to see if I can find any answers, but I didn't really find any.
我也尝试在线查看我是否能找到任何答案,但我没有找到任何答案。
class Base
{
private:
int length;
public
void increaseLengthByOne();
};
void Base::increaseLengthByOne()
{
this->length++;
}
class Derived : public Base
{
private:
int dLength;
public:
void newFunction();
};
void Derived::newFunction()
{
printf("New function working");
increaseLengthByOne();
}
From what I understand, dLength won't be changed and Length can't be changed. 根据我的理解,dLength不会被改变,长度也不能改变。 What does the increaseLengthByOne function do then?
那么increaseLengthByOne函数做了什么? And how would I make it so the increaseLength function can increase dLength?
我怎样才能使增量长度函数增加dLength?
From what I understand, [...] Length can't be changed.
据我了解,[...]长度无法改变。
You've misunderstood. 你误会了。 A member being private does not imply that it cannot be changed.
私有的成员并不意味着它不能被改变。
What does the increaseLengthByOne function do then?
那么increaseLengthByOne函数做了什么?
It increases the member length
. 它增加了成员
length
。
It's not because it's not visible that it doesn't exist or cannot access it. 这不是因为它不可见它不存在或无法访问它。 The method will be able to access the member variable because it was declared as being able to access it.
该方法将能够访问成员变量,因为它被声明为能够访问它。
That's the principle of visibility for traditional method calls as well, such a method can access private or protected member of the class, even if the caller code cannot. 这也是传统方法调用的可见性原则,这样的方法可以访问类的私有或受保护成员,即使调用者代码不能。
How does a derived class use that function?
派生类如何使用该函数?
When the Base function is public or protected, the derived class function can simply invoke the Base function. 当Base函数是public或protected时,派生类函数可以简单地调用Base函数。
When the Base function is private, the derived class function can not directly invoke it. 当Base函数是私有时,派生类函数不能直接调用它。
Below I have lightly touched your code to initialize, and demonstrate derive class function invocation of Base functions. 下面我轻轻触摸你的代码进行初始化,并演示派生类函数调用Base函数。
Notes: 笔记:
Remember to initialize your data attributes --- I prefer initialization list of ctor. 记得初始化你的数据属性---我更喜欢ctor的初始化列表。
use cout to show what is happening, these are easily removed for submittal 使用cout来显示正在发生的事情,这些很容易被删除以便提交
learn to use gdb 学会使用gdb
#include <iostream>
using std::cout, std::endl; // feature requires -std=c++17
class Base
{
private:
int length;
public:
Base(int lInit = 0) : length(lInit) // init length
{ cout << "\n length init: " << length << endl; }
void increaseLengthByOne();
};
void Base::increaseLengthByOne()
{
length++; // this->length++;
cout << "\n length++ : " << length;
}
class Derived : public Base
{
private:
int dLength;
public:
Derived() : Base(1), dLength(0) // init Base and dLength
{ cout << "\n dLength init: " << dLength << endl; }
void newFunction(int d);
};
void Derived::newFunction(int d)
{
cout << "\n Derived::newFunction(int)";
cout << "\n dLength a: " << dLength;
dLength = d;
cout << "\n dLength b: " << dLength;
increaseLengthByOne();
cout << "\n dLength c: " << dLength;
}
class T989_t
{
public:
int operator()() { return exec(); } // functor entry
private: // methods
int exec()
{
Derived d;
d.newFunction(5);
return 0;
}
}; // class T98_t
int main(int , char** ) { return T989_t()(); } // invoke functor
Output: 输出:
length init: 1
dLength init: 0
Derived::newFunction(int)
dLength a: 0
dLength b: 5
length++ : 2
dLength c: 5
私有的成员将无法在类外部访问。但是你总是可以编写包装器或帮助器函数来提供它在世界之外的访问。这种情况发生在你的情况下。所以长度的值会增加。
From what I understand, ... Length can't be changed.
据我了解,......长度无法改变。
length can't be changed directly outside Base
class since it is a private member of Base
class, however you can change it using public member function of Base
class. 长度不能改变直接外
Base
类,因为它是一个私有成员Base
类,但是你可以使用的公共成员函数改变Base
类。
What does the
increaseLengthByOne
function do then?那么
increaseLengthByOne
函数做了什么?
Derived class can access Base class public/protected members as such when you call increaseLengthByOne
in derived class it executes Base
class function increaseLengthByOne
which increases length. 派生类可以访问Base类public / protected成员,因为当您在派生类中调用
increaseLengthByOne
,它会执行Base
类函数increaseLengthByOne
,这会增加长度。
how would I make it so the
increaseLength
function can increasedLength
?我怎么会让它所以
increaseLength
功能可以增加dLength
?
There are 2 ways of doing this 有两种方法可以做到这一点
1) Just add dLength++
to your function newFunction()
ie 1)只需将
dLength++
添加到函数newFunction()
即
void Derived::newFunction()
{
std::cout << "New function working";
dLength++;
increaseLengthByOne();
}
2) In case you want to keep function name increaseLengthByOne
same then you can override it in derived class ie 2)如果你想保持函数名称
increaseLengthByOne
相同,那么你可以在派生类中覆盖它,即
class Derived : public Base
{
private:
int dLength;
public:
void newFunction();
void increaseLengthByOne(); //this overrides Base class function
};
void Derived::increaseLengthByOne()
{
dLength++;
Base::increaseLengthByOne(); //this increases length;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.