I need help with an assignment for my programming course.

I have three classes A,B,C. all stored in one array.
(These are not my real classes there are just a guide)

Class A{
       char name;
       virtual print(){ printf("%s",name); }

Class B : public A{
       char middleName;
       print(){ A::print(); printf("%s", middleName); }

Class C : public B{
       char lastName;
       print(){ printf("%s",lastName); }

/*I wish to call the print from all the classes, assume this array is full of a variaty of A,B,C */

A* classes[20];


This works fine for classes A and B. B will print the common info from A then its own.

But this is not the case for Class C, -> seems to call the B::print() rather than C::print(). so i cannot print the lastName.

The assignment states explicitly that C inherits from B.

(perhaps multiple inheritance is needed?)

any ideas? thanks in advance.


(for those that don't want to help me "cheat" on my work i can assure you this is a small part of the assignment that i am stuck on)

Edited by gilly231: n/a

6 Years
Discussion Span
Last Post by gilly231

It's hard to believe that this is your actual code, because you don't declare a return type for any of your print functions. I see no reason why calling print on a pointer to a C object should call anything but C::print, so I can only conclude that your problem lies in the difference, whatever it might be, between your actual code and what you posted.


I will post my full code later if no one can understand my problem.

I am new to c++, so what i am asking may well be nonsence

I am sure i can get around it if i give multiple inheritance of A and B to C, if it comes to it.

/*constructors are excluded*/
class Vehicle{
	char make[11];
	char model[21];
	int numWheels;
	int weight;
	Vehicle(char*, char*, int, int);
	virtual void print(){	
            printf("%-10s %-20s %i %8i ",make, model, numWheels, weight);

class regVehicle : public Vehicle {
	char numberPlate[9];
	char registeredKeeper[21];
	int registrationDate;
	regVehicle(char* , char* , int , int , char* , int , char* );
	virtual void print(){	
	      printf("%-8s %-20s %8i ",numberPlate, registeredKeeper, registrationDate);

class TaxiVehicle : public regVehicle{
	int taxiLicence;
	TaxiVehicle(char* , char* , int , int , char* , int , char* , int);
	void print(){ /*Point1*/ regVehicle::print(); printf("%-8i", taxiLicence); }

Vehicle* cars[20];

/*This is then filld with the various classes*/


/*When the object in cars[x] is a TaxiVehicle, only the information held in regVehicle is printed. NOTE also that 'point1' in never reached when the program is run */

Again, I see nothing in your code that suggests that there should be any problem.

Can you post a short example of what is going wrong?


You say:

/*When the object in cars[x] is a TaxiVehicle, only the information held in regVehicle is printed. NOTE also that 'point1' in never reached when the program is run */

but you never show any actual code that proves it. How do the values get into cars[0], cars[1], etc? What actually happens when you run the code?

If your problem is really what you say it is, you should be able to say

Vehicle* v = new TaxiVehicle(appropriate arguments);

and have it do the wrong thing. But so far all we have is your unsupported claim that it doesn't work.

Please post a small, complete test case that fails.

Edited by arkoenig: n/a


I am saying that:

Vehicle* v = new TaxiVehicle(appropriate arguments);

Will not work.
i don't understand what you want me to post realy.
the above will print evrything that is within the regTaxi, so

Vehicle* v = new TaxiVehicle(x ,y ,z);
(say these are the arguements and z is unique to taxi)
Has the SAME output as if i did,
Vehicle* v = new regVehicle(x ,y);

the program knows regVehicle extends Vehicle, and will work how you would expect.
but it cannot seem to see that TaxiVehicle has its own print().

i thought it was an issue with extending methods beyond one inheritance.
are you saying that this should not be a problem?


i don't understand what you want me to post realy.

I want you to post a complete program that I can compile and run, and that illustrates the problem that you are having.


I appologise for taking so long to reply, was unable to access the net.

I have however in this time worked out more of what is going on, u were right to say it should work and if i do,
Vehicle* car = new TaxiVehicle(.....);
i get the correct output.

However, the problem arrises only when the array, cars[] contains other vehicles aswell. I suspect this is due to something with the memory allocation.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.