简体   繁体   中英

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". 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. As for member functions it will have two. The first one will return m_a. The second one will print "Hello from A!". Let's move on to B. B will have a private string 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. As far as functions go, firstly B will have a function that will return 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!" 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. 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.

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. 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.

However when you say Class B : public A it is the other end of the spectrum. 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 .

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 . Just create another member function:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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