c++11 - How to Calculate MPG miles per gallon using Classes in C++ -


hello need understanding c++ in dumb down term can 1 me understand how calculate mpg using classes. have far. want want better every program been kicking in class.

#include <iostream> using namespace std;  class cars{     double miles;     double gallons; private:     double miles[6] = {240.5, 300.0, 320.5, 280.7, 190.7, 265.3};     double gallons[6] = {16.4, 15.6, 17.2, 15.4, 13.7, 16.9};  public:     cars(double m, double g) : miles(m), gallons(g){}     double mpg(void) {return miles / gallons;} };   int main() {     int i;     cout << "index\tmiles\tgallons\tmpg\n";     (i=0; i<6; i++) {         cout << << "\t" << miles[i] << "\t" << gallons[i] << endl;     }     char z; cin >> z; } 

without classes got

#include <iostream> using namespace std;       double miles[6] = {240.5, 300.0, 320.5, 280.7, 190.7, 265.3};     double gallons[6] = {16.4, 15.6, 17.2, 15.4, 13.7, 16.9};     float mpg[6] = {0,0,0,0,0,0};     int i;      float mpgcal(){         cout << mpg = miles/gallons;         return mpg;      }  int main() {     cout << "index\tmiles\tgallons\tmpg\n";     (i=0; i<6; i++) {         cout << << "\t" << miles[i] << "\t" << gallons[i] << "\t" << mpg[i] << endl;     }     char z; cin >> z; } 

note: did not compile this, have no compiler handy atm.

you need array of cars, not of doubles. class needs expose way miles , gallons.

#include <iostream> using namespace std;  class car{ private:     double miles;     double gallons; public:     car(double m, double g) : miles(m), gallons(g){}     double getmpg(void) const {return miles / gallons;}     double getmiles(void) const {return miles;}     double getgallons(void) const {return gallons;} };  car cars[6]={      car(240.5, 16.4),      car(300.0, 15.6),      car(320.5, 17.2),      car(280.7, 15.4),      car(190.7, 13.7),      car(265.3, 16.9)};  int main() {     int i;     cout << "index\tmiles\tgallons\tmpg\n";     (i=0; i<6; i++) {         cout << << "\t" << cars[i].getmiles() << "\t" << cars[i].getmiles() << "\t" << cars[i].getmpg() << endl;     }     char z; cin >> z; } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -