Question

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

1.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
2.Make a class whose objects each represent a box of batteries (use dynamic memory and draw the class diagram). Also, the class has to include the following services for its objects:
-show what's in the box
-get how many kinds of batteries are in the box
-determine if two boxes are equal
C++. 

Homework Answers

Answer #1

Code :

#include <iostream>

using namespace std;

const int MAX_SIZE = 10;

class Battery

{

private:

char m_cBattryType;

public:

char getBattryType()

{

return m_cBattryType;

}

void setBatteryType(char cBatteryType)

{

m_cBattryType = cBatteryType;

}

void DisplayInfo()

{

cout << m_cBattryType << endl;

}

bool operator ==(Battery battery)

{

return m_cBattryType == battery.getBattryType();

}

Battery(char cBatteryType)

{

m_cBattryType = cBatteryType;

}

};

class BatteryBox

{

private:

Battery* m_batteryArr[MAX_SIZE];

int m_iCount;

public:

char getCount()

{

return m_iCount;

}

BatteryBox()

{

m_iCount = 0;

for (int i = 0; i < MAX_SIZE; i++)

m_batteryArr[i] = NULL;

}

void DisplayInfo()

{

for (int i = 0; i < m_iCount; i++)

{

m_batteryArr[i]->DisplayInfo();

cout << i << endl;

}

}

void AddBattery(Battery *b)

{

m_batteryArr[m_iCount++] = b;

}

bool operator ==(BatteryBox batteryBox)

{

if (this->m_iCount != batteryBox.getCount())

return false;

bool bResult = false;

for (int i = 0; i < m_iCount; i++)

{

bResult = (*this->m_batteryArr[i] == *batteryBox.m_batteryArr[i]);

if (bResult == false)

break;

}

return bResult;

}

~BatteryBox()

{

for (int i = 0; i < m_iCount; i++)

{

Battery *ptr = m_batteryArr[i];

if (ptr)

{

delete ptr;

ptr = NULL;

}

}

}

};

using namespace std;

int main()

{

Battery* b1 = new Battery('a');

Battery* b2 = new Battery('b');

Battery* b3 = new Battery('a');

Battery* b4 = new Battery('b');

cout << "Show battery b1 information ";

b1->DisplayInfo();

cout << endl;

cout << "Show battery b3 information ";

b3->DisplayInfo();

cout << endl;

if (*b1 == *b3)

{

cout << "batteries are same" << endl;;

}

else

{

cout << "batteries are different" << endl;

}

BatteryBox bx1;

BatteryBox bx2;

bx1.AddBattery(b1);

bx1.AddBattery(b2);

bx2.AddBattery(b1);

bx2.AddBattery(b2);

cout << "Show battery box bx1 information ";

bx1.DisplayInfo();

cout << endl;

cout << "Show battery box bx2 information ";

bx2.DisplayInfo();

cout << endl;

if (bx1 == bx2)

{

cout << "battery boxes are same" << endl;;

}

else

{

cout << "different" << endl;

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
Practice using EXCEL – Part of your Orientation Assignment to prepare for class on the first...
Practice using EXCEL – Part of your Orientation Assignment to prepare for class on the first day. Step by Step instructions on completing PR1-5B. BEFORE STARTING TO WORK THE PROBLEM YOU NEED TO WRITE ALL BALANCE FORMULAS. To do so do the following in order. Click on Cell D39. In D39 you will write a formula to add rows D37 and D38. To do so do the following: a)    Start in Cell D39 and press = sign b)    Highlight cell...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Question 1 2 pts Let x represent the height of first graders in a class. This...
Question 1 2 pts Let x represent the height of first graders in a class. This would be considered what type of variable: Nonsensical Lagging Continuous Discrete Flag this Question Question 2 2 pts Let x represent the height of corn in Oklahoma. This would be considered what type of variable: Discrete Inferential Distributed Continuous Flag this Question Question 3 2 pts Consider the following table. Age Group Frequency 18-29 9831 30-39 7845 40-49 6869 50-59 6323 60-69 5410 70...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
1) Describe an example of each of the following that may be found of your kitchen:...
1) Describe an example of each of the following that may be found of your kitchen: Explain how your choice falls into this category, and if there is a chemical name or symbol for it, provide that as well. Provide a photo of your example with your ID card in it. a) a compound b) a heterogeneous mixture c) an element (symbol) Moving to the Caves… Lechuguilla Caves specifically. Check out this picture of crystals of gypsum left behind in...
As you saw from the lab PowerPoint slides last week, you will be doing a research...
As you saw from the lab PowerPoint slides last week, you will be doing a research study looking at ‘Aggression Priming” for your first paper. For this week’s discussion, I want you to discuss with your group what you think this study is about. What is the hypothesis? What theory does it come from? What do you predict will happen (do you expect something different than the hypothesis in the researcher instructions? If so, what and why?)? Do you think...
What role could the governance of ethics have played if it had been in existence in...
What role could the governance of ethics have played if it had been in existence in the organization? Assess the leadership of Enron from an ethical perspective. THE FALL OF ENRON: A STAKEHOLDER FAILURE Once upon a time, there was a gleaming headquarters office tower in Houston, with a giant tilted "£"' in front, slowly revolving in the Texas sun. The Enron Corporation, which once ranked among the top Fortune 500 companies, collapsed in 2001 under a mountain of debt...