Original Post
In C++, if you make a derived class from a base class, when u make derived object, the constructor of the base class should be called right? Well it works for me but only halfway through,..i am using MSVC++ 6.0 standard edition and i tried this: Now on my compiler that builds and runs. the creation of Me, calls the base class second constructor. ( i see the message it prints if called) the creation of Me2, should call the default constructor of the base class but doesnt seem to do so. ( i dont see any message verifying that it does) Why does this happen? Is this some bug with MSVC 6.0 as one person of the IRC channel has suggested. BUT, if i change the first constructor of Ctriangle to accept a parameter. (as such: Ctriangle(int bla){} ) and create an object using that constructor as such: ( ctriangle me2(6); ) then the default constructor of the base class DOES get called. Its strange,...is the base class's default constructor only supposed to be called when the derived takes a parameter?
#include <iostream> // Include our standard header
#include <windows.h>
#include <fstream> // Include this to use file streams
using namespace std; // set our namespace to standard;
class Cpolygon
{
protected:
int height;
int width;
public:
void SetArea(int a,int b){height=a; width=b;}
Cpolygon(){cout<<"empty def const for base class(Cpolygon)"<<endl;}
Cpolygon(int tri){cout<<"base class (cpolygon) const sent the value of "<<tri<<endl;}
};
class Ctriangle:public Cpolygon
{
public:
Ctriangle(){}
Ctriangle (int a,int b):Cpolygon(b){ cout<<"derived object constructor wit value of "<<a<<endl;}
int Area(){return(0.5*height*width);}
};
int main()
{
Ctriangle me(5,6); //calls the base class's second constor fine
Ctriangle me2(); //this should call base's def const but doesnt
return 0;
}