Please can some one explain each line, i am really confused. Explain what each l
ID: 639751 • Letter: P
Question
Please can some one explain each line, i am really confused. Explain what each line does, from the top to bottom. thanks.
// Car_Class.cpp
// Vova Programs Home Work.
#include <iostream>
#include <string>
using namespace std;
class car
{
string maker;
string model;
int year;
double tax;
public:
car()
{
maker = " ";
model = " ";
year = 0;
tax =0;
}
car(string ma,string mo, int y, double t)
{
maker = ma;
model = mo;
year = y;
tax = t;
}
car(car& obj)
{
this -> maker = obj.maker;
this -> model = obj.model;
this -> year = obj.year;
this -> tax = obj.tax;
cout << "COPY CONSTROCTOR"<<endl;
}
void setMaker(string ma)
{
maker = ma;
}
void setModel(string mo)
{
model = mo;
}
void setYear(int y)
{
year =y;
}
void setTax(double t)
{
tax = t;
}
string getMaker()
{
return maker;
}
string getModel()
{
return model;
}
int getYear()
{
return year;
}
double getTax()
{
return tax;
}
double operator+(car &obj)
{
return this ->tax + obj.tax;
}
car operator++()
{
tax ++;
return *this;
}
car operator++(int)
{
car temp = *this;
tax++;
return temp;
}
};
int main()
{
car my("t", "tt", 2015, 100);
car your("s", "ss", 2014, 50);
your++;
cout << (my++ + your) << endl;
cout << my.getTax() <<endl;
cout << your.getTax() <<endl;
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
// creating class
class car
{
// this is private section(default section)
// It has four variables
string maker;
string model;
int year;
double tax;
// this is public section
public:
// constructors are always created in public section
// constructorr function has same name as the class, and return nothing(not even void)
car()
{
// In this constructor we initialize our private variables
// Constructor is called during the creation of the object
// inititialize maker and model to empty strings
maker = " ";
model = " ";
// initialize year and tax to 0
year = 0;
tax =0;
}
// This is a parameterized constructor
// please note that this is just a prototype
// constructors are always created in public section
// constructorr function has same name as the class, and return nothing(not even void)
car(string ma,string mo, int y, double t)
{
// assign these parameters to private variables
maker = ma;
model = mo;
year = y;
tax = t;
}
// this is called copy constructor
// copying values of an object to another object
car(car& obj)
{
// this is another way of assigning values to private variables
// parameter is also an object of car class
// please note that this parameter obj should be taken by reference
// (I mean car &obj not car obj)
// maker = obj.maker and this->maker = obj.maker
// these both gives same results
this -> maker = obj.maker;
this -> model = obj.model;
this -> year = obj.year;
this -> tax = obj.tax;
cout << "COPY CONSTROCTOR"<<endl;
}
// these are setters
// which set values to private variables
// individually(seperate function) for each private variable
// this function is used to set maker
void setMaker(string ma)
{
maker = ma;
}
// this function sets the value of Model
void setModel(string mo)
{
model = mo;
}
// this function sets the value of Year
void setYear(int y)
{
year =y;
}
// this function sets the value of Tax
void setTax(double t)
{
tax = t;
}
// these are getters
// which returns private variable values
// individually(seperate function) for each private variable
// this function is returns the value of Maker
string getMaker()
{
return maker;
}
// this function is returns the value of Model
string getModel()
{
return model;
}
// this function is returns the value of Year
int getYear()
{
return year;
}
// this function is returns the value of Tax
double getTax()
{
return tax;
}
// this is overloading operator +
// Ex: int tax = obj1 + obj2;
// when the above statement is executed
// it calls obj1.operator+(obj2)
// which adds taxes and both values and returns sum of these two taxes
double operator+(car &obj)
{
return this ->tax + obj.tax;
}
// this is overloading operator ++
// Ex: obj1 = ++obj2;(pre increment)
// when the above statement is executed
// it calls obj1 = obj2.operator++()
// which increments tax in obj2 by one
// returns new incremented obj2
// which further we can assign it to obj1
car operator++()
{
tax ++;
return *this;
}
// this is overloading operator ++
// Ex: obj1 = obj2++;(post increment)
// when the above statement is executed
// it calls obj1 = obj2.operator++(int)
// which increments tax in obj2 by one
// returns old obj2
// which further we can assign it to obj1
car operator++(int)
{
car temp = *this;
tax++;
return temp;
}
};
int main()
{
// creating two objects my and your
car my("t", "tt", 2015, 100);
car your("s", "ss", 2014, 50);
your++;
// incrementing tax variable in your object by one.
// tax of your is 51
cout << (my++ + your) << endl;
// increments tax of my from 100 to 101, but returns 100(old value)
// then adds 100 with 51 to print 151
cout << my.getTax() <<endl;
// prints 101
cout << your.getTax() <<endl;
// prints 51
}