In: Computer Science
Make a class whose objects each represent a battery. The only attribute an object will have is the battery type (one letter). Also, the class has to include the following services for its objects: -determine if two batteries are equal -change battery type -get battery type display battery information
the program must include: 1.All / Part 2.Overloading of operators 3.Builder 4.Copy constructor 5.Destroyer 6.Dynamic memory
C++
Complete code for the following question:-
#include <iostream>
using namespace std;
class battery
{char batteryType;
public:
battery(){batteryType='A';}//constructor(builder)
battery(battery &temp)//copy constructor
{
batteryType=temp.batteryType;
}
void checkEqual(battery b) //check if two batteries are
equal
{
if(batteryType==b.batteryType)
cout<<"Batteries are equal!!\n";
else
cout<<"Batteries are not equal!!\n";
}
void changeBatteryType(char type)//change battery
type
{
batteryType=type;
cout<<"Battery type changed to
"<<batteryType<<"\n";
}
void displayBatteryType()// display battery
type
{
cout<<"Battery Type= "<<batteryType<<"\n";
}
battery operator+(battery& s2) //operator(+)
overloading
{ battery s3;
if(this->batteryType>s2.batteryType)
s3.batteryType= this->batteryType;
else
s3.batteryType= s2.batteryType;
return s3; }
~battery(){}; //destructor(destroyer)
};
int main()//driver function to check our class
{ battery *b1= new battery; //dynamic memory of battery
type
battery *b2= new battery;
b2->changeBatteryType('D');
b2->checkEqual(*b1);
return 0;
}
Screenshot of IDE:
Screeshot of output using above driver code:-
*******************************************************************************************************************************
IN CASE OF ANY DOUBT ,FEEL FREE TO LEAVE A COMMENT.
PLEASE UPVOTE IF THIS ANSWER HELPED YOU.