0% found this document useful (0 votes)
33 views

Program No 4

The C++ program defines a Car class with attributes like make, year, mileage, petrol amount, tank size, and owner. The class contains methods to get user input, fill petrol, drive, and display car details. An object is created and the methods are called to get input, drive, refill petrol, and display updated details. The owner name can also be changed by calling the change method.

Uploaded by

ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Program No 4

The C++ program defines a Car class with attributes like make, year, mileage, petrol amount, tank size, and owner. The class contains methods to get user input, fill petrol, drive, and display car details. An object is created and the methods are called to get input, drive, refill petrol, and display updated details. The owner name can also be changed by calling the change method.

Uploaded by

ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program no 4:

#include<iostream>

#include<string.h>

using namespace std;

class car

private:

char make[30];

int year;

float mpg,dis,petrol;

float tanksize;

char owner[30];

public:

void get()

cout<<"enter the name of owner";

cin>>owner;

cout<<"enter the company name";

cin>>make;

cout<<"model of car is";

cin>>year;

cout<<"petrol in the car is";

cin>>petrol;

cout<<"size of the tank is";


cin>>tanksize;

cout<<"milage is";

cin>>mpg;

cout<<"enter the distance covered";

cin>>dis;

float fillpetrol(float n_o_g)

cout<<"petrol before filling"<<petrol<<"\n";

petrol=petrol+n_o_g;

if(petrol>tanksize)

petrol=tanksize;

return petrol;

float drive(float distance)

dis=dis+distance;

return dis;

void display()

cout<<"*************************************************"<<endl;

cout<<"the name of the owner is"<<owner<<endl;


cout<<"name of the company is"<<make<<endl;

cout<<"model of car is"<<year<<"model"<<endl;

cout<<"petrol in the car is"<<petrol<<"liter"<<endl;

cout<<"size of the tank is"<<tanksize<<"liters"<<endl;

cout<<"milage is"<<mpg<<"mpg"<<endl;

cout<<"*************************************************"<<endl;

car()

strcpy(make," ");

strcpy(owner," ");

petrol=0;

dis=0;

mpg=0;

tanksize=0;

year=0;

car(char man[],char own[],float p,float mlg,float tksize,float dist,int y)

strcpy(make ,man);

strcpy(owner,own);

petrol=p;

mpg=mlg;

tanksize=tksize;

year=y;
dist=dis;

cout<<"constructor X called"<<endl;

void change(char onr[])

strcpy(owner,onr);

cout<<"new owner is"<<owner;

};

int main()

float j,d,pf,pfil,k,driv,a;

car c;

c.get();

c.display();

cout<<"enter distance travelled";

cin>>k;

driv=c.drive(k);

cout<<"total distance covered is"<<driv;

cout<<"enter amount of petrol to be refilled";

cin>>pf;cc

pfil=c.fillpetrol(j);

cout<<"petrol in car before fill is"<<pfil<<endl;

cout<<"total petrol in the car is"<<pfil+pf<<endl;


cout<<"1change owner"<<"2 dont change owner";

cin>>d;

char a[50];

cout<<"enter the new owner name=";

cin>>a[50];

c.change(a);

You might also like