One of the practice problem my prof gave me is to change the the Vehicle class so that all the data members are pointer which are Dynamically allocated such that the new class still give the same result as the original. This is what I have thus far I just get random numbers.
ORIGINAL CODE:
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle();
Vehicle(int passengers, int fuelcap, int mpg);
int rangeMiles();
double rangeKm();
void printSpecs();
void setSpecs(int, int, int);
private:
void set(int, int, int);
int passengers;
int fuelcap;
int mpg;
};
int main()
{
char go_on;
Vehicle honda;
cout << "\nHonda specs after using defult constructor";
honda.printSpecs();
cout << "Honda's range in miles " << honda.rangeMiles() << endl;
cout << "Honda's fange in Km " << honda.rangeKm() << endl;
cin >> go_on;
Vehicle mazda(6, 15, 40);
cout << "\nMazda specs after using parameterized constructor";
mazda.printSpecs();
cout << "Mazda's range in miles " << mazda.rangeMiles() << endl;
cout << "Mazda's range in Km " << mazda.rangeKm() << endl;
cin >> go_on;
cout << "\nHonda specs before using mutator method";
//honda.passengers = 5;
//honda.fuelcap = 17;
//honda.mpg = 32;
//honda.set(5, 17, 32);
honda.printSpecs();
cout << "Old honda's range in miles " << honda.rangeMiles() << endl;
cout << "old honda's range in Km " << honda.rangeKm() << endl;
cin >> go_on;
cout << "\nHonda specs after using mutator method";
honda.setSpecs(5, 17, 32);
honda.printSpecs();
cout << "new honda's range in miles " << honda.rangeMiles() << endl;
cout << "new honda's range in Km " << honda.rangeKm() << endl;
private:
void set(int, int, int);
int *passengers;
int *fuelcap;
int *mpg;
};
int main()
{
char go_on;
Vehicle honda;
cout << "\nHonda specs after using defult constructor";
honda.printSpecs();
cout << "Honda's range in miles " << honda.rangeMiles() << endl;
cout << "Honda's fange in Km " << honda.rangeKm() << endl;
cin >> go_on;
cout << "\nHonda specs before using mutator method";
//honda.passengers = 5;
//honda.fuelcap = 17;
//honda.mpg = 32;
//honda.set(5, 17, 32);
return 0;
}
Vehicle::Vehicle() {
int *passengers = new int(4);
int *fuelcap = new int(13);
int *mpg = new int(36);
}
int Vehicle::rangeMiles() {
return *fuelcap * *mpg;
}