One thing is confusing me about virtual methods in abstract classes. I don't have much experience with the OOP in C++, so hope you dudes can delete[] my_doubts;
Suppose we have a mother class Car and a child one called Ferrari:
//Car.h
#include <string>
using std::string;
class Car{
protected:
//properties
string brand;
string model;
int wheels;
public:
//this is the only constructor that has sense to exist
Car();
//destructor
virtual ~Car();
//setMethods are all virtual
virtualvoid setBrand(string) = 0;
//This function has to be defined in the child class.
virtualvoid setModel(string) = 0;
virtualvoid setWheels(int) = 0;//This too.
//getMethods are all virtual
virtual string getBrand() const;
//Just returns the brand//this function can be called from the child class.
virtualint getWheels() const;//This too.
};
//Ferrari.h
#include <string>
#include "Car.h"
using std::string;
class Ferrari : public Car
{
public:
Ferrari(string _model, int _wheels)
: brand("Ferrari"), model(_model), wheels(_wheels) { }
//error: class Ferrari does not have any field named brand, model, wheels
//But above this properties are protected, not private. What am I missing?
//inline functions
virtualvoid setBrand(string _brand) { brand = _brand; }
virtualvoid setModel(string _model) { model = _model; }
virtualvoid setWheels(int _wheels) { wheels = _wheels; }
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//main.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include "Ferrari.h"
using std::cout;
using std::cin;
using std::endl;
int main(int argc, char *argv[])
{
Ferrari f1("599", 4);
f1.setWheels(6);
cout << f1.getWheels() << endl;
cin.get();
return 0;
}
The problem is with your initializer list; the members 'brand', 'model' and 'wheels' are members of 'Car' not 'Ferrari'. So to fix this you'll need to create a constructor for 'Car':
Car(std::string, std::string, int); //Also write the constructor, this is just the declaration and not the definition.
and fix the constructor for 'Ferrari':
1 2
Ferrari(string _model, int _wheels)
: Car("Ferrari", _model, _wheels)
But shouldn't the derived classes inherite all the non-private members and therefore use them freely?
I forgot also to implement the destructor (I had just declared it)
But shouldn't the derived classes inherite all the non-private members and therefore use them freely?
It does, and I just showed you how to initialize them. Every other interaction with these variables within the scope of the child object will feel the same as if these variables were direct members (as opposed to inherited) with the possible exception of the variables destructor when you start getting fancy with things.
But shouldn't the derived classes inherite all the non-private members and therefore use them freely?
The initializer list constructs each object.
Objects can only be constructed once.
It is any given class's responsibility to construct its members.
Therefore... a child class cannot construct its parent's members because the parent has already constructed them.
The child class shouldn't be doing that anyway. Even though that stuff is inherited, it's still part of the parent -- and the parent and child are two distinctly different classes that should not be sharing a lot of stuff.