11 Replies - 4485 Views - Last Post: 05 October 2010 - 02:05 AM Rate Topic: -----

#1 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

program using class and member functions

Posted 04 October 2010 - 06:19 PM

Write a class car with the following properties: A car has a certain fuel efficiency measured in miles/gallon and a certain amount of fuel in the tank. the efficiency is specified in the contructor and the initial fuel level of a car is 0.0. Provide a member function drive(double miles) that simulates driving the car for a certain distance, reducing the fuel level in the tank, and member functions get_fuel(), returning the current fuel level, and add_fuel(double gallons) to fuel up.

here is what I have so far.. but I am stuck on the actual implementations of the member functions

#include <iostream>

using namespace std;

class car
{
  private:
    double fuel_efficiency;
    double fuel_capacity;
    double fuel_tank;
  public:
    car(double efficiency, double capacity);
    double get_fuel() const;
    void add_fuel(double gallons);
    void drive(double miles);
};

int main()
{
  car c1(15.0, 20.0), c2(29.5, 30.0);
  c2.add_fuel(10.0);
  c2.drive(82.5);
  c1.add_fuel(25.0);
  c2.add_fuel(20.0);
  c2.drive(65.5);
  c1.drive(55.7);
  c2.add_fuel(8.5);
  c2.drive(18.4);
  cout << "c1 fuel remaining: " << c1.get_fuel() << endl;
  cout << "c2 fuel remaining: " << c2.get_fuel() << endl;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 04 October 2010 - 07:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: program using class and member functions

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 04 October 2010 - 10:08 PM

Well, start with your constructor:

car(double efficiency, double capacity);


What would you do with the arguments efficiency and capacity?

This post has been edited by n8wxs: 04 October 2010 - 10:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

Re: program using class and member functions

Posted 04 October 2010 - 10:59 PM

View Postn8wxs, on 04 October 2010 - 09:08 PM, said:

Well, start with your constructor:

car(double efficiency, double capacity);


What would you do with the arguments efficiency and capacity?


car(double efficiency, double capacity){
fuel_efficiency = efficiency;
fuel_capacity = capacity;
}


??
Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 04 October 2010 - 11:07 PM

Ok...

So the fuel tank has a capacity. What do you need to do to add fuel to the tank?
Was This Post Helpful? 0
  • +
  • -

#5 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

Re: program using class and member functions

Posted 04 October 2010 - 11:20 PM

View Postn8wxs, on 04 October 2010 - 10:07 PM, said:

Ok...

So the fuel tank has a capacity. What do you need to do to add fuel to the tank?


void add_fuel(double gallons){
};


?? I am really bad with code.. sorry
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 04 October 2010 - 11:51 PM

Ok, that's the method. What do you expect the method to do with it's argument gallons?
Was This Post Helpful? 0
  • +
  • -

#7 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

Re: program using class and member functions

Posted 05 October 2010 - 12:02 AM

View Postn8wxs, on 04 October 2010 - 10:51 PM, said:

Ok, that's the method. What do you expect the method to do with it's argument gallons?


void add_fuel(double gallons){
fuel_tank = gallons;
}

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 05 October 2010 - 12:14 AM

void add_fuel(double gallons){
fuel_tank = gallons;
}



You need to think about how a car works. It's fuel tank has a capacity. When you add fuel to a real car you have to careful not to overfill the tank. So before adding the new fuel to the tank we need to check if it will fit:

void add_fuel(double gallons){
   if ((fuel_tank + gallons) < fuel_capacity)
        fuel_tank += gallons;
}



This leads to an error because in the constructor we didn't assign a value, such as zero, to the fuel tank variable. Here, we are trying to add gallons to an uninitialized variable. This is easy to fix by adding a line to the constructor code:

car(double efficiency, double capacity){
fuel_efficiency = efficiency;
fuel_capacity = capacity;
fuel_tank = 0.0;
}


Was This Post Helpful? 1
  • +
  • -

#9 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

Re: program using class and member functions

Posted 05 October 2010 - 12:37 AM

View Postn8wxs, on 04 October 2010 - 11:14 PM, said:

void add_fuel(double gallons){
fuel_tank = gallons;
}



You need to think about how a car works. It's fuel tank has a capacity. When you add fuel to a real car you have to careful not to overfill the tank. So before adding the new fuel to the tank we need to check if it will fit:

void add_fuel(double gallons){
   if ((fuel_tank + gallons) < fuel_capacity)
        fuel_tank += gallons;
}



This leads to an error because in the constructor we didn't assign a value, such as zero, to the fuel tank variable. Here, we are trying to add gallons to an uninitialized variable. This is easy to fix by adding a line to the constructor code:

car(double efficiency, double capacity){
fuel_efficiency = efficiency;
fuel_capacity = capacity;
fuel_tank = 0.0;
}



#include <iostream>

using namespace std;

class car
{
      private:
              double fuel_efficiency;
              double fuel_capacity;
              double fuel_in_tank;
      public:
              car(double efficiency, double capacity);
              double get_fuel() const;
              void add_fuel(double gallons);
              void drive(double miles);
};

car::car(double efficiency, double capacity):fuel_efficiency(efficiency),fuel_capacity(capacity)
{
           fuel_efficiency = efficiency;
           fuel_capacity = capacity;
           fuel_in_tank = 0.0;
}

void car::add_fuel(double gallons)
{
     if ((fuel_in_tank + gallons) < fuel_capacity)
     {
        fuel_in_tank += gallons;
     }  
}

int main()
{
    car c1(15.0, 20.0), c2(29.5, 30.0);
    c2.add_fuel(10.0);
    c2.drive(82.5);
    c1.add_fuel(25.0);
    c2.add_fuel(20.0);
    c2.drive(65.5);
    c1.drive(55.7);
    c2.add_fuel(8.5);
    c2.drive(18.4);
    cout << "c1 fuel remaining:  " << c1.get_fuel() << endl;
    cout << "c2 fuel remaining:  " << c2.get_fuel() << endl;
}



ok this is what I have, when I compile it gives me linker error for drive and get_fuel??
I had to tweak the code you gave me a little to not get errors for those parts...
Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 05 October 2010 - 12:46 AM

Quote

when I compile it gives me linker error for drive and get_fuel??


So write those methods. You've declared them, you need to implement them.
Was This Post Helpful? 0
  • +
  • -

#11 tigereye1760  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-October 10

Re: program using class and member functions

Posted 05 October 2010 - 01:53 AM

THANKS!! got it working.. appreciate your help and time
Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: program using class and member functions

Posted 05 October 2010 - 02:05 AM

Good! Glad to help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1