Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:48:24+00:00 2026-06-15T23:48:24+00:00

I am trying to understand inheritance and I need some help building two classes.

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:48:25+00:00Added an answer on June 15, 2026 at 11:48 pm
    (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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand the inheritance mappings in EF4. My database has two
Trying to understand a little more about Crockford's approach to Prototypical Inheritance whereby he
I've been trying to understand inheritance when interfaces are involved. I want to know
I'm trying to understand how does the inheritance work in play! But unsuccessfully yet.
I am new in JS programming and trying to understand prototype-based inheritance. below is
Trying to understand a question I got wrong on a test: How does inheritance
I'm trying to understand pure prototype-based JavaScript and one specific thing I'm struggling with
I'm trying to understand inheritance in JavaScript. I'm using prototype to link a child
I am a newbie and trying to understand concepts of inheritance and design patterns.
I am trying to really understand how inheritance and the super keyword work. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.