• Advertisement
Sign in to follow this  

weird inheritance problem

This topic is 4798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

#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;
}




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?

Share this post


Link to post
Share on other sites
Advertisement
It is this line:

Ctriangle me2(); //this should call base's def const but doesnt

All you want is "Ctriangle me2;"

Essentially the compiler is taking it as a method declaration, instead of creating an instance of a class.

[Edited by - visage on January 6, 2005 6:21:20 PM]

Share this post


Link to post
Share on other sites
It may by a MSVC 6 error. In any case, explicitly call the base class constructor in every subclass constructor.


Ctriangle ():Cpolygon(){...}

Share this post


Link to post
Share on other sites
There's a rule of thumb in C++ that pretty much anything that can be parsed as a function declaration will be parsed as a function declaration. Ctriangle me2(); does not define an instance of Ctriangle. It declares a function named me2 which takes no parameters and returns a Ctriangle. You want Ctriangle me2; (no parenthesis).

Enigma

Share this post


Link to post
Share on other sites



Thanks a lot for your help. I cant believe my stupid mistake.

NOTE: this is advice for everyone. Its also probably best to confirm things on the forum first. I was going to write in my c++ learning book that it only works when the derived class is called with a parameter. If i didnt come here to get corrected who knows how long i would have carried such incorrect knowledge for in my head.
the thought actually scares me.

Anyways, thanks.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement