简体   繁体   English

学习继承

[英]Learning inheritance

I am trying to understand inheritance and I need some help building two classes. 我正在尝试了解继承,并且需要一些帮助来建立两个类。 The fist one is called A, the second one is called B. A has one private integer value "m_a". 第一个称为A,第二个称为B。A具有一个私有整数值“ m_a”。 It has two constructors, the default one sets m_a to 5. and another one which takes as an argument an integer called m and sets m_a's value to m. 它有两个构造函数,默认构造函数将m_a设置为5,另一个构造函数将名为m的整数作为参数并将m_a的值设置为m。 As for member functions it will have two. 至于成员函数,它将有两个。 The first one will return m_a. 第一个将返回m_a。 The second one will print "Hello from A!". 第二个将打印“您好,A!”。 Let's move on to B. B will have a private string m_s. 让我们继续到B。B将有一个私有字符串m_s。 A default constructor which will set m_s to "asd" or to anything other than an empty string and a constructor which will take as an argument a string and set m_s to it's value. 默认的构造函数会将m_s设置为“ asd”或除空字符串以外的其他任何值,而默认构造函数会将字符串作为参数并将m_s设置为其值。 As far as functions go, firstly B will have a function that will return m_s. 就函数而言,首先,B将具有一个将返回m_s的函数。 It will have a function which will have the same name as the print "Hello from A" function in A which will override it and it will printout "Hello from B!" 它将具有与A中的打印“来自A的Hello”功能同名的功能,它将覆盖该功能,并打印出“来自B的Hello!”。 instead (is that polymorphism ?). 相反(是多态性吗?)。

Those are the classes needed. 这些是所需的类。 I have the following questions (I will post what I have created below) Firstly, is there any way I can get to the private data fileds from the base class. 我有以下问题(我将在下面发布我创建的内容)首先,有什么方法可以从基类访问私有数据文件。 For example let's say I want to take the m_s variable, add it to another one and print out their sum. 例如,假设我要使用m_s变量,将其添加到另一个变量中并打印出它们的总和。 Is that possible ? 那可能吗 ? (and how) (如何)

Also when I try to create a class with a constructor different from the default one I get errors. 另外,当我尝试使用与默认构造函数不同的构造函数创建类时,也会出现错误。 I am obviously doing something wrong. 我显然做错了。 The question is what. 问题是什么。

I think those are all of my questions for now, so it is time for me to post the source code. 我认为这些是我目前所有的问题,因此现在该发布源代码了。

#include <iostream>
#include <string>

using namespace std;

class A
{
private:
    int m_a;
public:
    A(){m_a = 5;}
    A(int m)
    {
        m_a = m;
    }
    void pm()
    {
        cout << "Hello from A!" << endl;
    }
    int get_a()
    {
        return m_a;
    }
};

class B : A
{
private :
    string m_s;
public:
    B(){m_s = "asd";}
    B(string s)
    {
        m_s = s;
    }
    void pm()
    {
        cout << "Hello from B!" << endl;
    }
    string get_s()
    {
        return m_s;
    }
};

int main()
{
     A a(10);
     a.pm();
     cout << a.get_a() << endl;
     B b("asd");
     b.pm();
     cout << b.get_s() << endl;
      cout << b.get_a() << endl;
  return 0;
}
(is that polymorphism ?).

Not the way you have done it. 不是您完成的方式。 It would be polymorphism if you had a pointer of type A* which pointed to what was actually a B object, and calling pm on that pointer correctly invoked the member function of B. This would only be possible if the pm function in A were declared as virtual, like below. 如果您有一个类型为A*的指针,它实际上指向了一个B对象,并且对该指针调用pm正确地调用了B的成员函数,那将是多态性。只有在声明了A中的pm函数的情况下,这才有可能如下面所示。

class A
{
...
    virtual void pm(){
...
};
...

int main()
{
    A* = new B();
    A->pm(); //"Hello from B!"
}

is there any way I can get to the private data fileds from the base class 有什么办法可以从基类获取私有数据文件吗?

Not sure what you mean here - your example talks of a private field of the derived class. 不确定您的意思-您的示例谈到了派生类的私有字段。 Typically good class design means that derived class should not need to access the (private) fields of the base class, if this is needed you should make that field protected. 通常,良好的类设计意味着派生类不需要访问基类的(私有)字段,如果需要,则应使该字段受保护。

As to the compile error @ArunKumar got it exactly. 至于编译错误, @ ArunKumar准确地得到了它。 When you say Class B : A You inherit from A, but all the members are inherited as private by default, due to this, base class constructor is private, so you cannot use it. 当您说Class B : A您继承自A,但是默认情况下所有成员都被继承为私有,因此,基类构造函数是私有的,因此您不能使用它。

However when you say Class B : public A it is the other end of the spectrum. 但是,当您说Class B : public A它是频谱的另一端。 All members of the base class retain their accesibility in the derived class (public remains public, etc) 基类的所有成员都保留其在派生类中的可访问性(公众保持公开状态,等等)

尝试将class B : A更改为class B : A class B : public A

The problem is that you're using private inheritance: 问题是您正在使用私有继承:

class B : A {

Inheritance through classes are private by default. 默认情况下,通过类的继承是私有的。 Add public before A . A之前添加public

class B : public A {

As for your other problem... 至于你的其他问题...

I want to take the m_s variable, add it to another one and print out their sum.

This is easy when it comes to std::string . 当涉及到std::string时,这很容易。 Just create another member function: 只需创建另一个成员函数:

void addTom_s(string s) { m_s += s; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM