Need Help with Dynamically Allocated and Pointer

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;

return 0;
}

Vehicle::Vehicle() {
passengers = 4;
fuelcap = 13;
mpg = 36;
}

Vehicle::Vehicle(int p, int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}

int Vehicle::rangeMiles() {
return fuelcap * mpg;
}

double Vehicle::rangeKm() {
return fuelcap * mpg*1.609;
}

void Vehicle::printSpecs() {
cout << "\nPassengers = " << passengers << "\nFuel tank capacity = "
<< fuelcap << "\nMiles per galon = " << mpg << " miles per galon" << endl;

}

void Vehicle::setSpecs(int p, int f, int m) {
set(p, f, m);
}

void Vehicle::set(int p,int f, int m) {
passengers = p;
fuelcap = f;
mpg = m;
}

NEW CODE
#include <iostream>
using namespace std;

class Vehicle {
public:
Vehicle();

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;


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

double Vehicle::rangeKm() {
return *fuelcap * *mpg*1.609;
}

void Vehicle::printSpecs() {
cout << "\nPassengers = " << *passengers << "\nFuel tank capacity = "
<< *fuelcap << "\nMiles per gallon = " << *mpg << " miles per galon" << endl;

}



1
2
3
4
5
Vehicle::Vehicle() {
    int *passengers = new int(4);
    int *fuelcap = new int(13);
    int *mpg = new int(36);
}


Those inside the constructor are local variables; you’re hiding your class properties.

Please, use code tags or your post could get no answers.
You also need to delete the allocated members in the destructor. And you need to think about what to do about class copy.
Thanks For the help but i have since went to see my prof and he was able to clear things up for me. I'll keep in mind to use code tags next time.
Topic archived. No new replies allowed.