Child class inherites from parent class question

Jun 15, 2009 at 7:17am
Hi,

I am trying to call a parent constructor from a child class.

class parent{
public:
double global;
parent(double x){global=x};
}

class child{
public:
child(double xxx):parent(xxx){};

child(int a1, int a2){
double xx.
xx=a1/a2; // calculate xx from a1 and a2
child(xx);
}
}


However, child(int,int) doesn't really assign xx to "global".

Can anyone help me with it? it's hard to believe a child class can't inherit from its parent class.

Thanks in advance!

Kai
Jun 15, 2009 at 7:37am
There's no inheritance here - child class is not derived from parent

As a matter of fact what you have written is full of mistakes/errors.
Last edited on Jun 15, 2009 at 7:42am
Jun 15, 2009 at 7:41am
Constructor of the parent class can't be called like child(xx). It can be called in header of the constructor. like child(int a1, int a2):parent(a1/a2){}.

The child(xx) will create a new object, but it is not stored in any of the variable.

--Maha.
Last edited on Jun 15, 2009 at 7:41am
Jun 15, 2009 at 10:54am
You can call any parent ctor from the initializer list, but only from the intializer list. You cannot call a parent ctor from the body of your child's ctor.

You cannot call one ctor of your child class from another ctor. Only 1 ctor can be called. IE: There is no way to call child(double) from child(int,int) -- at least not on the same object.

Here is code that does what you're trying to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class parent
{
public:
  double global;
  parent(double x){global=x;}  // no need for semicolon after function bodies
};                             // but you need one after class bodies

class child : public parent       // make child derive from parent
{
public:
child(double xxx):parent(xxx){}   // calls parent ctor OK

child(int a1, int a2) : parent( ((double)a1) / a2 )  // calls parent ctor OK
                                              // notice I cast to double here
                                              // otherwise it will do integer division
  {
  }
};
Jun 15, 2009 at 11:02am
it's hard to believe a child class can't inherit from its parent class


Not really. In reality children do inherit something from their parents, but in C++ that classes deriving from one another expresses the is-a relationship. Are you sure that a child is a kind of parent, at least in your program?
Last edited on Jun 15, 2009 at 11:03am
Jun 15, 2009 at 5:33pm
Hi guys,

Thanks for the fast reply!

I am still quite new to C++, please let me explain the idea of my program. Maybe inheritance is not the best design choice.

I have a Matrix class, which is a very big class that has many constructors and methods adapting all kinds of data. Meanwhile, I need another Array class, which I want to have all features of Matrix class, but a few more abilities. e.g. an extra constructor that takes any number of Array objects and append them to a new Array object. And this is where I stuck.

class Matrix{
public:
lots of variables;
Matrix(data, length, width){set up all the data for matrix}
...
};

class Array:Matrix{
public:
Array(data, size):Matrix(data,1,size){} //an array is a matrix with length=1, width=size
/** Array constructor that takes any number of array objects (have to include stdarg.h )**/
Array(array1, ... ){
calculate data from all arguments
calculate size from all arguments
array(data, size); //COMPILE, BUT DON'T WORK.
//The idea is that I don't have to manually set variables that are inherited from Matrix
}
};


You see the complication is from arbitrary number of arguments.

I was thinking to have some static helper functions: getData(), and getSize(). Then, the constructor will look like:
Array(array1, ...) : Matrix( getData(array1, ...), 1 , getSize(array1, ... ) ){} , but because the syntax doesn't really work out, it won't even compile.

I hope I express my question clearly.... In short, I want my Array class (the child class) to be able to take arbitrary number of arguments, and calculate data and size to tell Matrix class(the parent class) build a instance of it.

Thanks again!

Kai
Jun 22, 2009 at 7:16am
Just in case some other people interested in the solution of this problem.

I think the best way to call parent Constructor is to have a protected initialize() in parent class, and child class can just call that method to set up whatever variable you want to access.

<code>
class Matrix{
public:
//lots of variables;
Matrix(data, length, width){initialize(data, length, width)}
protected:
initialize(data,length,width);
};

class Array:Matrix{
public:
/** Array constructor that takes any number of array objects (have to include stdarg.h )**/
Array(array1, ... ){
//calculate data from all arguments
//calculate size from all arguments
this->initialize(data, 1, size)
}
};
</code>

cheers,

Kai
Jun 22, 2009 at 7:43am
I still don't see what the constructor of Matrix is meant to do
Matrix(data, length, width){set up all the data for matrix}

Do you want to initialize all the elements in the matrix at the time your create it? It's not convenient as the ctor just takes one argument as data. You might want to declare a multi-dimensional vector or array as a member variable of your class and make it private (or protected), then use pointers to access individual elements. Declaring a lot of "variables" in the class declaration fixes the amount of "variables" and...what are they actually?

Matrix(data, length, width) {initialize(data, length, width)}
but what does initialize do?
To make an Array (here, I think you mean something of size n by 1 or 1 by n) from a particular row or column in a matrix, simply make an array of appropriate size then copy the elements from the matrix.

By the way the correct syntax in the forum is [code][/code].
Topic archived. No new replies allowed.