有什么方法可以将数据从一个 class 继承到另一个?
Is there any way that data can be inherited from one class to another?
我正在尝试学习面向对象编程,但遇到了一个问题。我有两个 class A
和 B
。我将命令行参数传递给 class A
,然后执行一些计算并形成一个二维向量。 (让我们调用向量 data
)
我想要classB
继承classA
.
所以我想知道有什么方法可以调用 class B
的默认构造函数并打印二维向量 data
.[=24= 的内容]
我尝试过的示例代码
class A
{
public:
vector<vector<string>>data;
fstream file;
string word, filename;
A()
{
}
A(string fileOpen)
{
file.open(fileOpen);
while (file >> word)
{
vector<string>rowTemp={word};
data.push_back(rowTemp);
}
}
vector<vector<string>> getVector()
{
return data;
}
};
class B:A
{
public:
B()
{
for(auto i:data)
{
for(auto j:i)
{
cout<<j<<' ';
}
cout<<endl;
}
}
};
int main(int argc, char* argv[]){
fstream file;
string word, filename;
file.open(argv[1]);
string fileOpen=argv[1];
A s(fileOpen);
B c;
return 0;
}
我基本上希望 class B 能够访问二维向量 data
以便我可以对其执行进一步的计算,而计算逻辑保留在 class B
中.
有办法吗?
此外,正如您在 class A
中看到的,默认构造函数是空的。但它是必需的,因为没有它,我收到一个错误,即无法调用 class B
的默认构造函数。有没有更好的方法来写这个?因为有一个空的默认构造函数看起来很糟糕。
您似乎误解了继承的工作原理,只是不清楚您的期望。问题是:基 class 的成员总是被继承的。他们的访问可能会受到限制,但他们仍然存在。
考虑这个简化的例子:
#include <iostream>
class A {
public:
int data = 42;
A() = default;
A(int value) : data(value) {}
int getData() { return data; }
};
class B : A {
public:
B() {
std::cout << A::data; // ok
std::cout << A::getData(); // ok
}
};
int main(){
B c;
//std::cout << c.data; // error: data is private!
}
输出为:
4242
因为 B
确实继承了 A
的 data
成员。在 B
中,您可以直接或通过 getData
访问 data
,因为两者都是 A
中的 public
。但是,因为 B
私有地继承自 A
(这是通过 class
定义的 classes 的默认继承),您不能直接访问 data
或 getData
在 main
.
此外,当你写:
A s(fileOpen);
B c;
那么s
和c
是两个完全不相关的对象。我想你更想:
B c{fileOpen};
并从 B
:
的构造函数中调用 A
的构造函数
B(const std::string& filename) : A(filename) {
// now you can access A::data
// which has been initialized in
// constructor of A
}
我正在尝试学习面向对象编程,但遇到了一个问题。我有两个 class A
和 B
。我将命令行参数传递给 class A
,然后执行一些计算并形成一个二维向量。 (让我们调用向量 data
)
我想要classB
继承classA
.
所以我想知道有什么方法可以调用 class B
的默认构造函数并打印二维向量 data
.[=24= 的内容]
我尝试过的示例代码
class A
{
public:
vector<vector<string>>data;
fstream file;
string word, filename;
A()
{
}
A(string fileOpen)
{
file.open(fileOpen);
while (file >> word)
{
vector<string>rowTemp={word};
data.push_back(rowTemp);
}
}
vector<vector<string>> getVector()
{
return data;
}
};
class B:A
{
public:
B()
{
for(auto i:data)
{
for(auto j:i)
{
cout<<j<<' ';
}
cout<<endl;
}
}
};
int main(int argc, char* argv[]){
fstream file;
string word, filename;
file.open(argv[1]);
string fileOpen=argv[1];
A s(fileOpen);
B c;
return 0;
}
我基本上希望 class B 能够访问二维向量 data
以便我可以对其执行进一步的计算,而计算逻辑保留在 class B
中.
有办法吗?
此外,正如您在 class A
中看到的,默认构造函数是空的。但它是必需的,因为没有它,我收到一个错误,即无法调用 class B
的默认构造函数。有没有更好的方法来写这个?因为有一个空的默认构造函数看起来很糟糕。
您似乎误解了继承的工作原理,只是不清楚您的期望。问题是:基 class 的成员总是被继承的。他们的访问可能会受到限制,但他们仍然存在。
考虑这个简化的例子:
#include <iostream>
class A {
public:
int data = 42;
A() = default;
A(int value) : data(value) {}
int getData() { return data; }
};
class B : A {
public:
B() {
std::cout << A::data; // ok
std::cout << A::getData(); // ok
}
};
int main(){
B c;
//std::cout << c.data; // error: data is private!
}
输出为:
4242
因为 B
确实继承了 A
的 data
成员。在 B
中,您可以直接或通过 getData
访问 data
,因为两者都是 A
中的 public
。但是,因为 B
私有地继承自 A
(这是通过 class
定义的 classes 的默认继承),您不能直接访问 data
或 getData
在 main
.
此外,当你写:
A s(fileOpen);
B c;
那么s
和c
是两个完全不相关的对象。我想你更想:
B c{fileOpen};
并从 B
:
A
的构造函数
B(const std::string& filename) : A(filename) {
// now you can access A::data
// which has been initialized in
// constructor of A
}