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;
public:
       virtual print(){ printf("%s",name); }
}

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

Class C : public B{
       char lastName;
public:
       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];

classes[0]->print();
classes[1]->print();
........

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.

gilly

(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 5 Years Ago by gilly231: n/a

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;
public:
	Vehicle(char*, char*, int, int);
	~Vehicle();
	virtual void print(){	
            printf("%-10s %-20s %i %8i ",make, model, numWheels, weight);
        }
};

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

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

Vehicle* cars[20];

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

cars[0]->print();
cars[1]->print();
cars[2]->print();
.............

/*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);
v->print();

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 5 Years Ago by arkoenig: n/a

I am saying that:

Vehicle* v = new TaxiVehicle(appropriate arguments);
v->print();

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)
v->print();
Has the SAME output as if i did,
Vehicle* v = new regVehicle(x ,y);
v->print();

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(.....);
car->print();
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.