2014-04-15 2 views
3

그래서 Uni에서 과제를 위해 이것을 만들었습니다.클래스 상속 문제 C++

'Vehicle'을 기본 클래스로 사용하고 클래스에서 일부 함수를 상속하는 두 클래스를 사용해야합니다. '자동차'와 '트럭'.

그래서 클래스 구조를 성공적으로 만들었지 만 기본 UI를 만들어야 콘솔 응용 프로그램이라는 것을 염두에두면 스위치를 사용하여 기본 메뉴를 만들뿐입니다.

내 "getDetails"섹션에서는 자동차 세부 정보뿐만 아니라 '자동차/트럭 :: getdetails'도 가져와야 할 때까지 'getDetails'섹션에서 'Vehicle :: getDetails'를 잡는 것이 가장 이상적입니다.

어떤 아이디어가 그 원인 일 수 있습니까?

여기

첫 번째 게시물, 그래서 내 게시물 나쁜 :(죄송 경우.

감사합니다! 운동의 핵심은

#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; 




    }  

} 
} 
+1

당신은 방법을 가상 할 필요가 :, 그것을 virtual 즉 레이블을 붙입니다. – kfmfe04

+0

다형성을 사용하려면 가상으로 'getDetails' 함수를 선언해야합니다. – perencia

+0

아 멋지다! 고맙습니다! – Provoke

답변

2

는 기본 vehiclevoid getDetails() 방법 virtual를 만들기 위해

(나는 또한 vehicle 클래스의 소멸자를 좋은 코딩 방법으로 정의 할 것입니다.)

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

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

the new C++11 override specifier도 사용할 수 있습니다. 당신이 함수는 파생하는 클래스에 의해 재정의 할 수 있도록하려면

+0

우수, 정말 고마워요! :) – Provoke

+0

virtual 키워드는 vtable에 대한 숨겨진 멤버 포인터를 생성하기 위해 컴파일러에 신호를 보냅니다. 이 함수는 런타임에 올바른 함수 호출을 결정할 수 있기 때문에 다형성이 어떻게 작동하는지에 대한 기초입니다. 가상을 사용하는 것과 같은 한 가지 종류의 잡동사니는 사기성으로 개체의 크기를 증가시킵니다 (숨겨진 vtable ptr)! –

0

는 C의 ++에서는, 당신은

class vehicle { 
    ... 
    virtual void getDetails() 
    { 
     ... 
    } 
    ... 
}