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...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines...
DO THIS IN C#,Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to create a rectangle with user-specified height and width. 4. Method getArea() that returns...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class...
IN C++!!! Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 0 in the default constructor. A non-argument constructor method to create a default rectangle. Another constructor method to create a rectangle with user-specified height and width. Method getArea() that returns the area. Method...
Python Please Define a class that will represent soccer players as objects. A soccer player will...
Python Please Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods: 1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored. 2. str method to return all data attributes as combined string object. 3. addToGoals that will accept an argument of int and...
1) Make a sketch of an object whose center of gravity is not located on the...
1) Make a sketch of an object whose center of gravity is not located on the object itself. This a
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...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT