I’m writing C++ code in Visual Studio and can’t get the expected outcome. The main goal is to output GetMaxPower but seems like the program is stuck in some place. What could be the issue to this problem?
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using namespace std;
class OverflowException {
public:
OverflowException() {
cout << endl << "Exception created!" << endl;
}
OverflowException(const OverflowException&) {
cout << "Exception copied!" << endl;
}
~OverflowException() {
cout << "Exception finished!" << endl;
}
};
class Vehicle {
protected:
char* registracijas_numurs;
int razosanas_gads;
float valsts_razotajs;
public:
Vehicle();
Vehicle(char*, int, float);
virtual ~Vehicle() {
cout << "Vehicle - destroyed!" << endl;
}
virtual void Print() const;
char* GetRn() const {
return registracijas_numurs;
}
int GetRg() const {
return razosanas_gads;
}
float GetVr() const {
return valsts_razotajs;
}
void SetRn(char* rn) {
registracijas_numurs = rn;
}
void SetRg(int rg) {
razosanas_gads = rg;
}
void SetVr(float vr) {
valsts_razotajs = vr;
}
};
class Car : public Vehicle {
private:
int dzineja_jauda;
public:
Car() : Vehicle(), dzineja_jauda(0) {
}
Car(char*, int, float, int);
virtual ~Car() {
cout << "nCar - destroyed!" << endl;
}
int GetDzinejaJauda() const {
return dzineja_jauda;
}
void SetDzinejaJauda(int dzineja_jauda) {
this->dzineja_jauda = dzineja_jauda;
}
virtual void Print() const;
};
class CarPark {
private:
typedef Car* CPointer;
CPointer* Nodes;
static const unsigned int DEFAULT_MAX_LENGTH = 5;
unsigned int MaxLength;
unsigned int Length;
public:
CarPark() :
MaxLength(DEFAULT_MAX_LENGTH), Length(0) {
Nodes = new CPointer[MaxLength];
}
CarPark(unsigned int MaxLength) : MaxLength(MaxLength), Length(0) {
this->MaxLength = MaxLength;
Nodes = new CPointer[MaxLength];
}
~CarPark();
static unsigned int GetDefaultMaxLength() {
return DEFAULT_MAX_LENGTH;
}
int GetMaxLength() const {
return MaxLength;
}
int GetLength() const {
return Length;
}
int GetMaxPower();
void addNode(const Car&);
void Print() const;
};
Vehicle::Vehicle() : registracijas_numurs(nullptr), razosanas_gads(0), valsts_razotajs(0) {
}
Vehicle::Vehicle(char* Prn, int Prg, float Pvr) {
registracijas_numurs = Prn;
razosanas_gads = Prg;
valsts_razotajs = Pvr;
}
inline void Vehicle::Print() const {
cout << "Registracijas_numurs: " << (registracijas_numurs != nullptr ? registracijas_numurs : "N/A") << endl;
cout << "Razosanas_gads: " << razosanas_gads << endl;
cout << "Valsts_razotajs: " << valsts_razotajs << endl;
}
Car::Car(char* Prn, int Prg, float Pvr, int Pdj) : Vehicle(Prn, Prg, Pvr) {
dzineja_jauda = Pdj;
}
inline void Car::Print() const {
Vehicle::Print();
cout << "Dzineja_jauda: " << dzineja_jauda << endl;
}
CarPark::~CarPark() {
for (unsigned int i = 0; i < Length; i++)
delete Nodes[i];
delete[] Nodes;
}
void CarPark::Print() const {
cout << "nCar nodes:" << endl;
for (unsigned int i = 0; i < Length; i++) {
cout << (i + 1) << ". ";
Nodes[i]->Print();
cout << "." << endl;
}
}
void CarPark::addNode(const Car& Node) {
if (Length == MaxLength)
throw OverflowException();
else
Nodes[Length++] = new Car(Node);
}
int CarPark::GetMaxPower() {
int MaxPower = 0;
for (unsigned int i = 0; i < Length; i++) {
if (Nodes[i]->GetDzinejaJauda() > MaxPower) {
MaxPower = Nodes[i]->GetDzinejaJauda();
}
}
return MaxPower;
}
int main(void) {
CarPark* CP = new CarPark(2);
Car* C1 = new Car(strdup("TEST4"), 1990, 4.79, 100);
Car C2(strdup("TEST6"), 2000, 6.55, 150);
try {
CP->addNode(*C1);
cout << "nNew node has been added successfully!" << endl;
}
catch (const OverflowException&) {
cout << "*** Error: maximal length exceeded! ***" << endl;
}
catch (...) {
cout << "Unknown error!" << endl;
}
cout << "nDefault maximal length (from CLASS): " <<
CarPark::GetDefaultMaxLength() << endl;
cout << "Default maximal length (from OBJECT): " <<
CP->GetMaxLength() << endl;
cout << "Maximal length: " << CP->GetMaxLength() << endl;
cout << "Current length: " << CP->GetLength() << endl;
try {
CP->addNode(C2);
cout << "nNew node has been added successfully!" << endl;
}
catch (const OverflowException&) {
cout << "*** Error: maximal length exceeded! ***" << endl;
}
catch (...) {
cout << "Unknown error!" << endl;
}
CP->Print();
cout << "Get max power: " << CP->GetMaxPower() << endl;
cout << endl << "Broken car deletion:";
delete CP;
delete C1;
cin.get();
return 0;
}
I tried to insert some debug statements in main() but even that the program doesn’t outputs that.