Конструктор работает некорректно (или не работает вовсе)
#include "Vehicle.h"
#include "Car.h"
#include <string>
#include <iostream>
int main()
{
Car Car1("1", 1000, 400, 2000);
Car1.Print();
}
Программа запускается, но вместо нужных параметров на месте заданных параметров нули (вместо строки пустота). Не могу понять причину. Помогите пожалуйста.
Вывод:
Vehicle Object Created
Car Object Created
Name:
Horse Power: 0
Gas Tank Capacity: 0
Mass of the car: 0
Maximum Speed: -2.14748e+09
Car Object Deleted
Vehicle object Deleted
Vehicle.h:
#pragma once
#include <string>
class Vehicle
{
std::string name;
float horsePower;
float gasTank;
float mass;
public:
std::string GetName();
float GetHP();
float GetGT();
float GetMass();
virtual void Print();
Vehicle(std::string n, float hp, float gas, float mass);
Vehicle();
~Vehicle();
};
Vehicle.cpp:
#include "Vehicle.h"
#include <string>
#include <iostream>
std::string Vehicle::GetName()
{
return this->name;
}
float Vehicle::GetHP()
{
return this->horsePower;
}
float Vehicle::GetGT()
{
return this->gasTank;
}
float Vehicle::GetMass()
{
return this->mass;
}
void Vehicle::Print()
{
std::cout << "Name: " << this->GetName() << std::endl;
std::cout << "Horse Power: " << this->GetHP() << std::endl;
std::cout << "Gas Tank Capacity: " << this->GetGT() << std::endl;
}
Vehicle::Vehicle(std::string n, float hp, float gt, float m)
{
name = n;
horsePower = hp;
gasTank = gt;
mass = m;
std::cout << "Vehicle Object Created" << std::endl;
}
Vehicle::Vehicle()
{
name = "";
horsePower = 0;
gasTank = 0;
mass = 0;
std::cout << "Vehicle Object Created" << std::endl;
}
Vehicle::~Vehicle()
{
std::cout << "Vehicle object Deleted" << std::endl;
}
Car.h:
#pragma once
#include "Vehicle.h"
#include <string>
class Car : public Vehicle
{
std::string name;
float horsePower;
float gasTank;
float mass;
float maxSpeed;
public:
float CalcMaxSpeed();
void Print() override;
//Constructor
Car();
Car(std::string n, float hp, float gt, float m);
//Destructor
~Car();
};
Car.cpp:
#include "Car.h"
#include <iostream>
float Car::CalcMaxSpeed()
{
int maxSpeed = this->GetHP() / this->GetMass() * 100;
return maxSpeed;
}
void Car::Print()
{
std::cout << "Name: " << this->GetName() << std::endl;
std::cout << "Horse Power: " << this->GetHP() << std::endl;
std::cout << "Gas Tank Capacity: " << this->GetGT() << std::endl;
std::cout << "Mass of the car: " << this->GetMass() << std::endl;
std::cout << "Maximum Speed: " << this->CalcMaxSpeed() << std::endl;
}
Car::Car()
{
std::cout << "Car Object Created" << std::endl;
}
Car::Car(std::string n, float hp, float gt, float m)
{
this->name = n;
this->horsePower = hp;
this->gasTank = gt;
this->mass = m;
std::cout << "Car Object Created" << std::endl;
}
Car::~Car()
{
std::cout << "Car Object Deleted" << std::endl;
}
Ответы (1 шт):
Автор решения: Harry
→ Ссылка
А зачем вы дублируете в потомке
std::string name;
float horsePower;
float gasTank;
float mass;
Результат: вы заполняете эти члены потомка, но в Print
используете НЕвиртуальные функции предка, так что они возвращают данные из предка, вами не заполненные...
Попробуйте убрать их из потомка, в предке объявить как protected
, а конструктор записать так:
Car::Car(std::string n, float hp, float gt, float m)
:Vehicle(n,hp,gt,m){}
Впрочем, это protected
не сильно и надо, раз вы обращаетесь к ним через открытый интерфейс Vehicle
.