Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class inheritance issues c++

So I'm creating this for an assignment in Uni.

It has to use a class structure with 'Vehicle' as the main class, and two classes which inherit some functions from it; 'Car' and 'Lorry'.

So I've created my class structure successfully (I think) but I need to create a basic UI for this, bearing it mind it is a console application, I am just creating a basic menu using switches.

How ever, in my "getDetails" section, it is only grabbing the 'Vehicle::getDetails' when it should be also getting the 'Car/lorry::getdetails', as well as the vehicle details.

Any ideas what could be causing that?

First post here, so sorry if my post is bad :(.

Thanks!

#include <iostream> 
#include <string>
#include <fstream>

using namespace std;


class vehicle {    

public:     

string manufacturer;
int year;
string regnum;

void getDetails() { 

    cout << "Please enter the details for your vehicle"<< endl;
    cout << "Please enter the manufacturer of your vehicle: "<< endl;
    cin >> manufacturer;
    cout << "Please enter the year of your vehicle's manufacture: "<< endl;
    cin >> year;
    cout << "Please enter your vehicle's registration number:  "<< endl;
    cin >> regnum;

}


void printDetails() {

    cout << "Your vehicle's details are as follows: " << endl;
    cout << "Your Vehicle's manufacturer is " << manufacturer << endl;
    cout << "Your Vehicle's year of manufacture is " << year << endl;
    cout << "Your Vehicle's registration number is " << regnum << endl;
}

void saveDetails() { 

    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "***Your Vehicle's Details***" << endl;
    vehiclefile << "Manufacturer:" << manufacturer << endl;
    vehiclefile << "Year of Manufacture:" << year << endl;
    vehiclefile << "Registration Number: " << regnum << endl;
    vehiclefile.close();
}

void openVehicleDetails() { 

}
};


class car : public vehicle{

public:

int numpassengers;
string cartype;

void getDetails() { 

    vehicle::getDetails();

    cout << "Please enter the number of maximum passengers your car can hold: "<< endl;
    cin >> numpassengers;
    cout << "Please enter the car body type: "<< endl;
    cin >> cartype;
    cout << "Thank your for your details"<< endl;
}

void printDetails() {

    vehicle::printDetails();

    cout << "Your car's maximum passengers is: " << numpassengers << endl;
    cout << "The body type of your car is: " << cartype << endl;
}

void saveDetails() { 

    vehicle::saveDetails();

    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Car" << endl;
    vehiclefile << "Number of passengers: " << numpassengers << endl;
    vehiclefile << "Type of car: " << cartype << endl;
    vehiclefile.close();
}
};


class lorry : public vehicle{

public:

double tonnage;
string bodtype;

void getDetails() {

    vehicle::getDetails();

    cout << "Please enter the gross weight of your Lorry: "<< endl;
    cin >> tonnage;
    cout << "Please enter the body type of your Lorry: "<< endl;
    cin >> bodtype;
    cout << "Thank your for your details"<< endl;
}

void printDetails() {

    vehicle::printDetails();

    cout << "Your lorry's details are as follows: " << endl;
    cout << "Your lorry's maximum weight is: " << tonnage << endl;
    cout << "The body type of your lorry is: " << bodtype << endl;
}

void saveDetails() { 

    vehicle::saveDetails();

    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Lorry" << endl;
    vehiclefile << "Maximum weight: " << tonnage << endl;
    vehiclefile << "Body type: " << bodtype << endl;
    vehiclefile.close();
}

};


int main () {

int flag = 0;
char choice;
int ifchoice;

vehicle*v;


while (flag == 0){

    cout << "***Main Menu***" << endl;      //Menu to allow ease of access within the program.
    cout << "Select by letter:" << endl;     
    cout << "1 - Add new entry" << endl;
    cout << "2 - Show entry" << endl;
    cout << "3 - Save entry" << endl;
    cout << "4 - Open saved document" << endl;
    cout << "5 - Delete entry" << endl;
    cin >> choice;          

    switch(choice) {       

    case '1':

        cout << "Is your vehicle a Car or a Lorry? " << endl;
        cout << "Press '1' for Car " << endl;
        cout << "Press '2' for Lorry " << endl;
        cin >> ifchoice;

            if (ifchoice == 1)
            {
                v = new car();
            }
            if (ifchoice == 2)
            {
                v = new lorry();
            }

            v->getDetails();

        break;

    case '2':

        v -> printDetails();

        break;

    case '3': 

        v -> saveDetails();

        break;




   }    

}
}
like image 650
Provoke Avatar asked Feb 21 '26 05:02

Provoke


1 Answers

The key point of the exercise is to make the void getDetails() method virtual in the base vehicle class.

(I'd also define a virtual destructor in the vehicle class as well, as good coding practice.)

class vehicle {    
public:         
    string manufacturer;
    int year;
    string regnum;

    // Make this virtual
    virtual void getDetails() { 
         ...

In the derived classes you may want to use the new C++11 override specifier as well.

like image 69
Mr.C64 Avatar answered Feb 23 '26 19:02

Mr.C64



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!