×

Loading...

Better way to initialize the member variable in constructor.

#include "stdafx.h"
#include <iostream.h>
class Base
{
public:
Base()
{
tt = 8; // make tt always initialized in the base and derived class.
}
int getTT()
{
return tt;
}
virtual void did(){};
void done();
private:
int tt;
};

class Derived : public Base
{
public:
void did()
{
cout << "this is derived class" << endl;
cout << "tt = " << getTT() << endl;
}
private:
};

void Base::done()
{
Base* b = new Derived;
b->did();
delete b;
}


int main(int argc, char* argv[])
{
Base b;
b.done();
return 0;
}
Report

Replies, comments and Discussions: