Initializing field in base class

I have a base class MyBase.  ClassA inherits MyBase. I want to set the field txt defined in MyBase when I create an instance of ClassA. How do I do that?

 
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;

class MyBase 
{
   public:
      MyBase(void);
      MyBase(string);
      string txt;
};

MyBase::MyBase(void)
{
}
MyBase::MyBase(string s)
{
  txt = s;  //(should use initializer list here instead)
}


class ClassA : public MyBase
{
   public:
   ClassA(string x)
   {
     //set txt = x
   }
};

void _tmain()
{
   std::string hw = "hello world";
   ClassA ca1(hw);
   std::cout << ca1.txt << std::endl;

   std::cout << "Hit enter to continue..." << std::endl; 
   getchar();
}

Open in new window

deleydSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
Basically, you access the base class members the same way as if they were the members of the inherited class. Please note that from the inherited class you could only access public and protected members of the base class. So, in your example you would just do this:
class ClassA : public MyBase
{
   public:
   ClassA(string x)
   {
     txt = x; //txt is a member of the base class
   }
};

Open in new window

However, this constructor is exactly as the base member constructor MyBase(string). It does not do any extra work. In this case this constructor can be omitted altogether.

If you plan to extend this constructor with some other code you could write this:
class ClassA : public MyBase
{
   public:
   int a;
   ClassA(string x):
   MyBase(x) // call the base class constructor
   {
     // the initialisation of txt will be performed by the Base class constructor
     // do some other stuff here.... E.g.
     a = atoi(txt); //txt is now equal to x
   }
};

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HooKooDooKuCommented:
The way I normally see this is that you have the constructors of the inherited class call the constructors of the base class:

class MyBase
{
  private:
  string txt;
  public:
  MyBase( string );
};

MyBase::MyBase( string s )
: txt( s )
{
  /*NOP*/;
}

class ClassA : public MyBase
{
public:
    ClassA( string );
};

ClassA::ClassA( string s )
: MyBase( s )
{
  /*NOP*/;
}

Open in new window


In this case, notice that txt is private to MyBase.  But the ClassA constructor simply calls the MyBase constructor within its own constructor.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.