Deriving base class

Apr 30, 2013 at 8:05pm
Can someone give me an example of how to create a base class that is derived (inherited) by three other classes?


Thanks
Apr 30, 2013 at 8:08pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Vehicle
{

};

class Car : public Vehicle
{

};

class Bicycle : public Vehicle
{

};

class Truck : public Vehicle
{

};
Apr 30, 2013 at 8:22pm
thanks, so here's what I have my next question is how do I use the base classes' constructor in the derived classes? For my code I am trying to simply have a variable in the base class "name" that contains "My name is", and then if the derived classes are invoked then they append there name to that string.

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
34
35
36
37
38
39
40
41
42
43
44
#ifndef A3MOD_H
#define A3MOD_H
#include <iostream>
#include <string>
using namespace std;
class BaseClass1 {
public:
	string name;

};

BaseClass1::BaseClass1() {
	name = "My Name is ";
	cout << name;
}



class dclass1 : public BaseClass1 {
public:
	string name;
};
BaseClass1::dlcass1() {
	name += "dclass1";
}

class dclass2 : public BaseClass1 {
public:
	string name;
};

class dclass3 : public BaseClass1 {
public:
	string name;
};



#endif




Apr 30, 2013 at 8:24pm
It'll happen automatically. The parent class is constructed before the child class.
Apr 30, 2013 at 10:08pm
ok so it doesn't seem to work, any ideas?
main.cpp
1
2
3
4
5
6
7
8
9
10
#include "a3mod.h"

int main() {
	int a;
	BaseClass1 Control;
	cin >> a;
	return 0;
}



header
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
34
35
36
#ifndef A3MOD_H
#define A3MOD_H
#include <iostream>
#include <string>
using namespace std;
class BaseClass1 {
public:
	string name;
	BaseClass1();
	BaseClass1(string&);

};
BaseClass1::BaseClass1(){}
BaseClass1::BaseClass1(string& me) {
	name = "My Name is " + me;
	cout << name;
}



class dclass1 : public BaseClass1 {
public:
	dclass1(string&);
private:
};

void dclass1(string& who) {
	BaseClass1::BaseClass1(who);
}




#endif

Apr 30, 2013 at 10:13pm
What doesn't work?

From the looks of that, the code will create an instance of BaseClass then ask for an int?
Apr 30, 2013 at 10:15pm
the int a and cin >> a I just to keep the command prompt window open. The idea is that when I create an instance of the base class, I can then access the derived class which then uses the base classes constructer to cout a string.
Apr 30, 2013 at 10:27pm
Okay that is different from the previous code you posted.

If you don't explicitly call a parent ctor it will use the default (parameterless) ctor. If you do not want to use that one, you must explicitly call the ctor you want in the initializer list:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class dclass1 : public BaseClass1 {
public:
	dclass1(string&);
private:
};

/*   This is totally wrong
void dclass1(string& who) {
	BaseClass1::BaseClass1(who);
}
*/


// This is how you define the ctor
dclass1::dclass1(string& who)
   : BaseClass1( who )   // <- this is the initializer list.  This calls the parent ctor
{
}
Apr 30, 2013 at 10:37pm
How would I declare it in main? However I do it, it doesn't seem to cout its name
Apr 30, 2013 at 10:38pm
A tip. If you're using VC++ (or Visual Studio) Ctrl + F5 will force the console window to stay open.

Or my preferred method is this at the end of your program
1
2
3
4
 
while (true) 
{
};
Apr 30, 2013 at 10:58pm
This stuff is really confusing, I'll try and clear up what I'm attempting.
Create a base class and three classes that derive from that base class. All classes including the base class need to have a variable. and then create constructors for all classes that initialize all attributes. The derived classes should use the base class’ constructor from within their initialization lists.
May 1, 2013 at 7:15am
Let me elaborate on my previous example:
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

// The base class
class Vehicle
{
public:
	// The constructor that initializes m_maxSpeed
	Vehicle( int maxSpeed ) : m_maxSpeed( maxSpeed ) { }

	// This returns the value of m_maxSpeed
	int getMaxSpeed() const { return ( m_maxSpeed ); }

private:
	// The variable that all derived classes share
	int m_maxSpeed;
};

// The first derived class
class Car : public Vehicle
{
public:
	// The constructor
	Car( int maxSpeed ) : Vehicle( maxSpeed ) { } // See? Here the constructor of the base class is called

	// No need to declare m_maxSpeed again, since it's already inherited, same for getMaxSpeed()
};

// The second derived class
class Bicycle : public Vehicle
{
public:
	// The constructor
	Bicycle( int maxSpeed ) : Vehicle( maxSpeed ) { } // See? Here the constructor of the base class is called

	// No need to declare m_maxSpeed again, since it's already inherited, same for getMaxSpeed()
};

// The third derived class
class Truck : public Vehicle
{
public:
	// The constructor
	Truck( int maxSpeed ) : Vehicle( maxSpeed ) { } // See? Here the constructor of the base class is called

	// No need to declare m_maxSpeed again, since it's already inherited, same for getMaxSpeed()
};

int main( int argc, char* argv[] )
{
	// Create an instance of Car
	Car car( 120 );

	// And print its maxspeed
	std::cout << car.getMaxSpeed() << std::endl;

	// Create an instance of Bicycle
	Bicycle bike( 30 );

	// Print its maxspeed
	std::cout << bike.getMaxSpeed() << std::endl;

	// Create an instance of Truck
	Truck truck( 80 );

	// Print its maxspeed
	std::cout << truck.getMaxSpeed() << std::endl;

	std::cin.get();

	return ( 0 );
}


The output is:
120
30
80
Last edited on May 1, 2013 at 7:16am
May 3, 2013 at 5:56am
Thanks, that helped out a lot!
Topic archived. No new replies allowed.