Question

In: Computer Science

Make a class whose objects each represent a battery. The only attribute an object will have...

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++

Solutions

Expert Solution

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.



Related Solutions

You will generate a People class of object and load an ArrayList with person objects, then...
You will generate a People class of object and load an ArrayList with person objects, then report the contents of that ArrayList. To do so, you must perform the following: (30 pts.) A) (15/30 pts. (line break, 11 pt) ) - Create a class file “People.java” (which will generate People.class upon compile). People.java will have eight(8) methods to 1) read a .txt file ‘people.txt’ 2) generate: ▪ List of all students AND teachers ▪ List of all students OR teachers...
: Design and implement class Radio to represent a radio object. The class defines the following...
: Design and implement class Radio to represent a radio object. The class defines the following attributes (variables) and methods: Assume that the station and volume settings range from 1 to 10. A private variable of type int named station to represent a station number. Set to A private variable of type int named volume to represent the volume setting. Set to 1. A private variable of type boolean named on to represent the radio on or off. Set to...
The assignment is to write a class called data. A Date object is intented to represent...
The assignment is to write a class called data. A Date object is intented to represent a particuar date's month, day and year. It should be represented as an int. -- write a method called earlier_date. The method should return True or False, depending on whether or not one date is earlier than another. Keep in mind that a method is called using the "dot" syntax. Therefore, assuming that d1 and d2 are Date objects, a valid method called to...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them by your own. A constructor that creates a table with the following: a list of data. ip address a integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the ip address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following...
Python Design a class named IP_address to represent IP address objects. The IP_addressclass contains the following A number of instance variables/fields to store a table of data. You can design them on your own. A constructor that creates a table with the following: a list of data. IP address an integer to indicate the number of elements in the sum_list/freq_list/average_list A get_ip_address() method that returns the IP address For example, consider the following code fragment: ip_key = '192.168.0.24' data_list =[(0,...
Write the class declarations for the following objects' descriptions. NO CODE. That means only properties and...
Write the class declarations for the following objects' descriptions. NO CODE. That means only properties and method declarations. Like... class Lamp { int mBulbs; void Explode(); }; 1) A Laptop has a color, brand, and processor speed. You can open, close, turn on, and turn off a laptop. 2) A monitor has a screen size and a brightness. 3) Oh wait, a laptop has a monitor itself. To do this, would you add a line to answer #1 or answer...
For each of the pairs of objects below, decide which object has the larger moment of...
For each of the pairs of objects below, decide which object has the larger moment of inertia, selecting from the following answers: A) the first B) the second C) neither D) can't tell E.g., if the answer for the first pair is B and for the rest, D, enter BDDDDD. The objects are of uniform density. Unless otherwise indicated, the flat objects rotate about an axis perpendicular to the plane in which they lie. 1.(A) A thin circular sheet of...
Explain what classes and objects are in object - oriented programming. Give an example of each...
Explain what classes and objects are in object - oriented programming. Give an example of each and explain how they work together in a computer program.
The application has class Magazine which describes one Magazine object. Class LLMagazineRec desribes linked list whose...
The application has class Magazine which describes one Magazine object. Class LLMagazineRec desribes linked list whose nodes have data of Magazine type and includes recursive method createArrayListRec which you have to implement. Class Driver has main method that creates myList as linked lists of magazines. It should invoke recursive method from class LLMagazineRec. WRITTEN IN JAVA 1.)code for magazine class: // Class Magazine describes magazine object that has title and number of pages public class Magazine { private int pages;...
Write a class whose instances represent a single playing card from a deck of cards. Playing...
Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Use an enum type to represent rank and another enum type to represent suit. Write another class whose instances represent a full deck of cards. Both these classes should have toString methods. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT