deleyd
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();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.