Hallo all,

I have a question for implementing Inheritance in C++. I think I miss the concept of inheritance C++ somehow, because the second class can not take the parameter from the first class..

I need to use a class Carbol and Carbol2. In Carbol2, I need to add a particular extra function, and the rest is taking the parameters and methods from Carbol

for Class Carbol, this is I am going to make

class Carbol
{
    public:
	float radius,dtof,color,lofs;
   	Carbol(float r,float df,char c,float ls)
		{
		    	radius=r;
			dtof=df;
			color=c;
			lofs=ls;
        }

	void draw(Carbol a)
		{
         the function that I want to implement with parameter passing
                }
};

and for the second class

class Carbol2:public Carbol
{

    void gambar(Carbol2)
   {
   	another extra function that I want to add
   }
};

and in int main

Carbol object(0.7,0,'y',1); 
     object.draw(object);
    Carbol2 ubjuct; 
    ubjuct.gambar(0.6,2,'g',3); // this object should take the function from carbol, and add another function from carbol2

and the error when I compile this is

error: no matching function for call to ‘Carbol::Carbol()

I think the problem is that I made a wrong implementation of Carbol2 (but I tried to make a lot of changes in the source code, it failed somehow).. From the error, it is said no matching function to call the first class, so does it mean that I have to change the structure for the first class? But if I don't use the second class, and only implement Carbol object(0.7,0,'y',1); object.draw(object); in int main, it worked just fine.. Can anyone point out what mistake I have in this general concept? I apologize if you guys having a difficulty in understanding my English, but I hope you could get the general idea about the difficulty I am having..

Cheerz,

Reza

add the default constuctor in Carbol class.
Check the line:

Carbol2 ubjuct;

when u do this it invokes the default constructor of Carbol2 which is Carbol2::Carbol2()
and also the default constructor to the base class is invoked which does not exist.
You have to write the default constructor for the base class i.e. Carbol.
In Carbol class just add:

Carbol(){}

in the public scope.

cheers...

now it showing a different error (but thanks, it show least error not like before)..

no matching function for call to ‘Carbol2::gambar(double, int, char, int)’|
note: candidates are: void Carbol2::gambar(Carbol2)|
||=== Build finished: 1 errors, 0 warnings ===|

it still can not invoke the function of "gambar" in the second class.. I need to take the four parameter in the first class, and add another extra function to gambar for the second object..

Thank you

The error u are getting is clear enough to know whats wrong. I didn't checked that before.

U are passing improper parameters to the Carbol2::gamber() function.
The expected param is an object of type Carbol2 but u are passing something else.

Change the protocol of the gamber method in Carbol2 so that it takes the parameter u want to pass.

Edited 7 Years Ago by dkalita: n/a

The error u are getting is clear enough to know whats wrong. I didn't checked that before.

U are passing improper parameters to the Carbol2::gamber() function.
The expected param is an object of type Carbol2 but u are passing something else.

Change the protocol of the gamber method in Carbol2 so that it takes the parameter u want to pass.

hm.. oke, i will try to make some changes then.. thanks a lot for sharing the knowledge :)

This question has already been answered. Start a new discussion instead.