C++ Inheritance question

Dec 22, 2011 at 4:37pm
Hi,



I got a question about inheritance in C++

For example, I got a derived class and a Base class.

The base class got a constructor like this:

Base(int x, int y,int z)
{
...
}

and the derived class got a constructor like this:

Derived(int m)
{
...
}

So if I want to make an object of the derived class, what do I need to type if I want to set x, y, z and p.

Derived(x, y, z, m) doesn't work but I don't know what to type...


Thanks in advance.
Dec 22, 2011 at 4:41pm
Dec 22, 2011 at 4:46pm
Be a little more clear in what you're asking. I'll post what I think is the answer, but...it might not help much, because I'm not entirely sure what you need.

When you derive a class from another class, it inherits everything in that class, except of course constructors/destructors, virtual members and friends.

So take the example from http://cplusplus.com/doc/tutorial/inheritance/ below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// constructors and derived classes
#include <iostream>
using namespace std;

class mother {
  public:
    mother ()
      { cout << "mother: no parameters\n"; }
    mother (int a)
      { cout << "mother: int parameter\n"; }
};

class daughter : public mother {
  public:
    daughter (int a)
      { cout << "daughter: int parameter\n\n"; }
};

class son : public mother {
  public:
    son (int a) : mother (a)
      { cout << "son: int parameter\n\n"; }
};

int main () {
  daughter cynthia (0);
  son daniel(0);
  
  return 0;
}


As you can see, daughter and son are derived from mother, so they inherit mother's attributes, but they are declared in int main() as daughter cynthia (0); and son daniel (0);. This creates two classes objects, one is of type daughter : mother, the other is of type son : mother.
Last edited on Dec 22, 2011 at 4:50pm
Dec 22, 2011 at 5:01pm
I'll explain it better because thats not really what I'm searching for.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Base
{
	public:
	Base(int x, int y, int z)
	{
		// this constructor does something with x, y and z and the function where you can do something with x, y and z are declareted in this class	
	}


};



class Derived : public Base
{
	public:
	Derived(int m)
	{

	}



};

int main()
{
	int x=0,y=1,z=2,m=3;
	

	Derived(x, y, z, m); // This doesn't work but I want to create a Derived object and set the parameters (x, y, z) of the Base class too...not only m because I need the others too.

}

Dec 22, 2011 at 5:03pm
class Derived : public Base does not inherit the constructor, or any of the constructor's members or methods from class Base.
Dec 22, 2011 at 5:24pm
How do I need to do it then if I want to initialize the Base class parameters (e.g x,y,z) ?
Dec 22, 2011 at 5:40pm
If you want to create an instance of class Base, then declare it in main.
1
2
3
4
int main(){
    int x = 0, y = 1, z = 2, m = 3;
    Base base(x, y, z);
}


If you want an instance of class Derived that has the same members (x, y, z) as Base, then change Base to:
1
2
3
class Base{
    int x, y, z;
}


Then you can just make instances of Derived that still contain their own members x, y, z.
Dec 22, 2011 at 5:52pm
Athar's reply (the first reply in the thread) had the right solution.

If you want to construct a Derived with 4 parameters, then Derived's ctor must take 4 parameters, not 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Base
{
	public:
	Base(int x, int y, int z)
	{
		// do stuff with x, y, z	
	}
};

class Derived : public Base
{
	public:
	// Derived(int m)  // <- bad, it has to take 4 params, not 1
	Derived(int x, int y, int z, int m)  // <- good, takes 4 params
	   : Base(x,y,z)  // <- pass the first 3 to Base's ctor
	{
	    // do something with m
	}
};

int main()
{
	int x=0,y=1,z=2,m=3;
	

	Derived(x, y, z, m); // now this works
}
Dec 22, 2011 at 6:42pm
Aah thanks! Thats what I was looking for.
Dec 22, 2011 at 6:44pm
I'll explain it better because thats not really what I'm searching for.

+
Thats what I was looking for.

=
>.<
Dec 22, 2011 at 7:25pm
I didn't noticed Athar's reply ;)
Topic archived. No new replies allowed.