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

Make a class whose objects each represent a box of bricks. Also, the class has to...
Make a class whose objects each represent a box of bricks. Also, the class has to include the following services for its objects: show what's in the box get the weight of the box determine if two boxes are equal
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
: 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...
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...
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...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View...
Create a class to represent a Mammal object that inherits from (extends) the Animal class. View javadoc for the Mammal class and updated Animal class in homework 4 http://comet.lehman.cuny.edu/sfakhouri/teaching/cmp/cmp326/s19/hw/hw4/ Use the description provided below in UML. Mammal - tailLength : double - numLegs : int + Mammal() + Mammal(double, int) + Mammal(String, int, double, double, char, double, int) //pass values to parent’s overloaded constructor //and assign valid values to tailLength, numLegs or -1 if invalid + setTailLength(double) : void //if...
IN C++!! Exercise #2: Design and implement class Radio to represent a radio object. The class...
IN C++!! Exercise #2: 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...
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
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT