Class inheritance issues c++ -


so i'm creating assignment in uni.

it has use class structure 'vehicle' main class, , 2 classes inherit functions it; 'car' , 'lorry'.

so i've created class structure (i think) need create basic ui this, bearing mind console application, creating basic menu using switches.

how ever, in "getdetails" section, grabbing 'vehicle::getdetails' when should getting 'car/lorry::getdetails', vehicle details.

any ideas causing that?

first post here, sorry if post 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 details vehicle"<< endl;     cout << "please enter manufacturer of vehicle: "<< endl;     cin >> manufacturer;     cout << "please enter year of vehicle's manufacture: "<< endl;     cin >> year;     cout << "please enter vehicle's registration number:  "<< endl;     cin >> regnum;  }   void printdetails() {      cout << "your vehicle's details follows: " << endl;     cout << "your vehicle's manufacturer " << manufacturer << endl;     cout << "your vehicle's year of manufacture " << year << endl;     cout << "your vehicle's registration number " << 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 number of maximum passengers car can hold: "<< endl;     cin >> numpassengers;     cout << "please enter car body type: "<< endl;     cin >> cartype;     cout << "thank your details"<< endl; }  void printdetails() {      vehicle::printdetails();      cout << "your car's maximum passengers is: " << numpassengers << endl;     cout << "the body type of 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 gross weight of lorry: "<< endl;     cin >> tonnage;     cout << "please enter body type of lorry: "<< endl;     cin >> bodtype;     cout << "thank your details"<< endl; }  void printdetails() {      vehicle::printdetails();      cout << "your lorry's details follows: " << endl;     cout << "your lorry's maximum weight is: " << tonnage << endl;     cout << "the body type of 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 allow ease of access within program.     cout << "select 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 vehicle car or lorry? " << endl;         cout << "press '1' car " << endl;         cout << "press '2' 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;        }      } } 

the key point of exercise make void getdetails() method virtual in base vehicle class.

(i'd define virtual destructor in vehicle class well, coding practice.)

class vehicle {     public:              string manufacturer;     int year;     string regnum;      // make virtual     virtual void getdetails() {           ... 

in derived classes may want use the new c++11 override specifier well.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -