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.
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.
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
{
}
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.
#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 );
}