несколько переменных наследования возвращаются правильно - PullRequest
1 голос
/ 24 октября 2019

Я работаю над множественным наследованием и сталкиваюсь со странной ошибкой. Мой код не возвращает правильную информацию на консоль. Это просто выводит ноль, я проверял несколько раз в своем коде, и я не могу найти что-то явно неправильное. Я довольно новичок в C ++, поэтому любая помощь, которую я буду признателен, в дополнение к любой другой критике.

Консоль будет выводить

Maverick
South station 
50  - passengers, is ok
40  - speed
0   - it is supposed to take distance/mph and output 2.6 in this case, but is returning nothing.

MBTA.cpp

#include "MBTA.h"

//objects 
transportation Dest;




MBTA::MBTA()
{
}

MBTA::MBTA(string strIn, string strInTransport, int iIn, int distIn, int eIn)
{
    setTrain(strIn);
    //Destination
    Dest.setTransport(strInTransport);
    //set passengers 
    setPass(iIn);

    Dest.setMilesToDest(distIn);
    engine.setMPH(eIn);
    //outputs train information 
    printTrainDestinationHours();
    //used printf as I was running into issue with using cout here
    //cout << " I am going to "; 
    printf("I am going to %s\n", Dest.getTransport().c_str());
    //uses engine stats function 
    cout << "I go " << engine.getMPH() << endl;
    printf("It will take me %.2f hours to arrive", redline.getTotal());
}

void MBTA::setTravelDist(int iIn)
{
    double destdistance = Dest.getDist();
    double trainMPH = engine.getMPH();

    //this divides miles by MPH, this might return a float 

    redline.setTotal(50, 10);

}



MBTA::~MBTA()
{
}

MBTA.H

#pragma once
#include "train.h"
class MBTA :
    public train
{
public:
    engine engine;
    train redline;

    MBTA();
    //train, destination, passengers, traveldist, speed
    MBTA(string, string, int, int, int);
    void setTravelDist(int);
    //double getTotal();


    //uses engine stats function 
    //double total = 0;

    ~MBTA();
};

Train.cpp

#include "train.h"



#include <iostream>
#include <cstdio>
#include <cmath>

using std::string;
using std::cout;
using std::endl;

//object member for transport
transportation tTrain;


train::train()
{
}



void train::printTrainDestinationHours()
{
    printf("\n\nTrain type: %s\n", getTrain().c_str());
    //passengers 
    printf("I have %d passengers\n", getPass());



}

void train::setPass(int iIn)
{
    passengers = iIn;
}

int train::getPass()
{
    return passengers;
}

void train::setTrain(string strIn)
{
    trainName = strIn;
}

string train::getTrain()
{
    return trainName;
}


void train::setTotal(int aIn, int bIn)
{
    //dist / mph
    total = aIn / bIn;
}

double train::getTotal()
{
    return total;
}


train::~train()
{
}

Заголовок поезда

#pragma once
#include <iostream>
#include <cstdio>

using std::string;
using std::cout;
using std::endl;

#include "engine.h"
#include "transportation.h"

class train : public transportation
{
public:
    train();

    void printTrainDestinationHours();
    //set and get destination 


    //num of pass
    void setPass(int);
    int getPass();

    //train 
    void setTrain(string);
    string getTrain();

    //distance

    void setTotal(int, int);
    double getTotal();

    ~train();




private:
    engine engineStats;
    int total = 0;
    string trainName = "";
    string destination = "";
    int passengers = 0;
};

engine.cpp

#include "engine.h"



engine::engine()
{
}

void engine::setMPH(int iIn)
{
    MPH = iIn;
}

int engine::getMPH()
{
    return MPH;
}


engine::~engine()
{
}

заголовок двигателя

#pragma once
class engine
{
public:
    engine();

    //return 
    void setMPH(int);
    int getMPH();

    ~engine();



protected:

    int MPH = 0;

};


'''
transportation cpp
'''

#pragma once
class engine
{
public:
    engine();

    //return 
    void setMPH(int);
    int getMPH();

    ~engine();



protected:

    int MPH = 0;

};

транспортный заголовок


#include "transportation.h"



transportation::transportation()
{
}

void transportation::setTransport(string strIn)
{
    destination = strIn;
}

string transportation::getTransport()
{
    return destination;
}

void transportation::setMilesToDest(int iIn)
{
    MilesToDestination = iIn;
}


int transportation::getDist()
{
    return MilesToDestination;
}


transportation::~transportation()
{
}

основной файл

#include <iostream>


using std::string;
using std::cout;
using std::endl;
using std::cin; //for ignore

#include "challenger.h"
#include "MBTA.h"
#include "plane.h"


int main()
{
    //object composition of vehicle type
    //  vehicle type location, passengers, MPH , distance 

    challenger SRT8707("Boston", 2, 100, 200);

    plane boeing("boeing", "houston", 50, 500, 300);

    MBTA redline("Maverick", "South station", 50, 100, 40);


    //pause and blank line
    cout << endl << endl;
    cin.ignore();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...