Docsity

ClassesObject-Object Oriented Programming-Lecture Handout, Exercises of Object Oriented Programming

Toggle sidebar
bg1
1c:\documents and settings\usman.younis\my documents... studio 2010\Projects\Classes\Classes\Classes.cpp
# include < iostream >
using namespace std ;
float fuel _ price = 73 . 4 ;
class Car
{
private :
float fuel ;
float mileage ;
float distance ;
public :
Car () : fuel ( 0 . 0 ) , mileage ( 0 . 0 ) , distance ( 0 . 0 ) { }
~ Car () { }
void re _ fuel ( float a ) ;
void check _ fuel () ;
void set _ mileage ( float b ) ;
void drive ( float dist ) ;
void check _ distance () ;
void dump _ fuel ( Car ) ;
};
void Car :: re _ fuel ( float a )
{
fuel = a / fuel _ price ;
}
void Car :: check _ fuel ()
{
cout << " Your car has : " << fuel << " litres left " << endl ;
}
void Car :: set _ mileage ( float b )
{
mileage = b ;
}
void Car :: drive ( float dist )
{
if (( dist / mileage ) > fuel )
{
cout << " You need to re ‐ fuel your car " << endl ;
return ;
}
fuel ‐= ( dist / mileage ) ;
distance += dist ;
}
void Car :: dump _ fuel ( Car a )
{
fuel += a . fuel ;
a . fuel += fuel ;
}
void Car :: check _ distance ()
{
cout << " Your car has travelled : " << distance << " km " << endl ;
}
docsity.com
pf2

Partial preview of the text

Download ClassesObject-Object Oriented Programming-Lecture Handout and more Exercises Object Oriented Programming in PDF only on Docsity!

c:\documents and settings\usman.younis\my documents... studio 2010\Projects\Classes\Classes\Classes.cpp 1 #include using namespace std; float fuel_price = 73. 4 ; class Car { private: float fuel; float mileage; float distance; public: Car() : fuel( 0. 0 ), mileage( 0. 0 ), distance( 0. 0 ) { } ~Car() { } void re_fuel(float a); void check_fuel(); void set_mileage(float b); void drive(float dist); void check_distance(); void dump_fuel(Car); }; void Car::re_fuel(float a) { fuel = a / fuel_price; } void Car::check_fuel() { cout<<"Your car has : "<<fuel<<" litres left"<<endl; } void Car::set_mileage(float b) { mileage = b; } void Car::drive(float dist) { if((dist/mileage) > fuel) { cout<<"You need to re‐fuel your car"<<endl; return; } fuel ‐= (dist/mileage); distance += dist; } void Car::dump_fuel(Car a) { fuel += a.fuel; a.fuel += fuel; } void Car::check_distance() { cout<<"Your car has travelled : "<<distance<<" km"<<endl; }

docsity.com

c:\documents and settings\usman.younis\my documents... studio 2010\Projects\Classes\Classes\Classes.cpp 2 int main (void) { Car Maserati, Toyota; Maserati.set_mileage( 5. 3 ); Toyota.set_mileage( 13. 4 ); Maserati.re_fuel( 5000 ); Toyota.re_fuel( 5000 ); Maserati.check_fuel(); Toyota.check_fuel(); Maserati.drive( 100 ); Toyota.drive( 100 ); Maserati.check_fuel(); Toyota.check_fuel(); Maserati.drive( 200 ); Toyota.drive( 200 ); Maserati.check_fuel(); Toyota.check_fuel(); Car Toyota 2 (Toyota); Toyota 2 .check_fuel(); Car Maserati 2 = Maserati; Maserati 2 .check_fuel(); Maserati 2 .dump_fuel(Toyota 2 ); Maserati 2 .check_fuel(); Toyota 2 .check_fuel(); return 0 ; }

docsity.com