Link to home
Start Free TrialLog in
Avatar of deleyd
deleydFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial