0% found this document useful (0 votes)
4 views4 pages

Programming

The document outlines a programming assignment focused on object-oriented software development using C++. It requires the creation of a HomeStereo class that manages an array of Switch objects representing output channels, along with a templated function for dynamically inserting elements into an array. Additionally, it includes the implementation of a Guitar class that holds a dynamic array of MusicStrings, emphasizing proper memory management and C++ best practices.

Uploaded by

andreevelascof
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Programming

The document outlines a programming assignment focused on object-oriented software development using C++. It requires the creation of a HomeStereo class that manages an array of Switch objects representing output channels, along with a templated function for dynamically inserting elements into an array. Additionally, it includes the implementation of a Guitar class that holds a dynamic array of MusicStrings, emphasizing proper memory management and C++ best practices.

Uploaded by

andreevelascof
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object-Oriented Software Development Using C++

Midterm Programming Practice

Inspect the following type definition below which is used to store an on/off switch.

enum class StateType


{
boolean,
character,
number
};

typedef struct
{
char name[20];
StateType sType;
union
{
bool stateAsBool;
char stateAsChar;
int stateAsNum;
} sState;
} Switch;

A switch is considered ON if the value satisfies one of the conditions below.


State Type:

sState (“on” value)

boolean

true

character

‘O’

number

Use the Switch definition above to build a class that models a home stereo system. Such a system
will have output channels such as TV, DVD, Bluetooth device, Computer, etc. Each Switch object
represents the current state of an output channel.

Basic Details
Your HomeStereo class has the following data members

• an array of dynamically allocated Switch-typed values


• a non-negative integer that stores the number of values in the array

A HomeStereo object can be created using a 2-argument Constructor.

Public Member Functions


• displayOutputState(): receives an ostream reference (defaults to standard output)
and returns an ostream reference. This function inserts the state of all switches into
the given output stream. The output should be in the following form.

Channel Name: xxxxxx - State [on/off]< endl >


Channel Name: xxxxxx - State [on/off]< endl >
...
Other Features
Include in your design all special member functions required to manage your objects.

Misc
You are allowed to add as many private members as your design requires!

Put in the answer box the content of your header file and implementation file. Both files must be
properly created according to C++ standard and best practices.

Define a family of functions (a templated function) named insertAtEnd that inserts in a dynamically-
allocated array of elements of any type another element at the end (resize the array). The function
should receive as parameters:

• the dynamically-allocated array


• the size of the array
• the element to insert

The function should return the resulted array.

Specialize the function for the type char. In this specialization the array must be null-terminated; the size
parameter doesn’t count the null-byte.

The client code listed below uses your templated function, and should not contain memory leaks. The
comments next to each statement shows the content the array should have after the statement is
executed.
// assume all necessary headers have been included

int main()
{
{
int* arrI = nullptr;
arrI = insertAtEnd(arrI, 0, 1); // 1
arrI = insertAtEnd(arrI, 1, 5); // 1, 5
arrI = insertAtEnd(arrI, 2, -3);// 1, 5, -3
delete[] arrI;
}
{
double* arrD = nullptr;
arrD = insertAtEnd(arrD, 0, 1.2);// 1.2
arrD = insertAtEnd(arrD, 1, 2.3);// 1.2, 2.3
arrD = insertAtEnd(arrD, 2, 3.4);// 1.2, 2.3, 3.4
delete[] arrD;
}
{
char* arrC = nullptr;
arrC = insertAtEnd(arrC, 0, 'a');// a\0
arrC = insertAtEnd(arrC, 1, 'b');// ab\0
arrC = insertAtEnd(arrC, 2, 'c');// abc\0
cout << arrC;
Implement delete[]
a class named
arrC; Guitar that holds a dynamic array of MusicStrings. A MusicString is defined
by the }following structure:
}
struct MusicString{
std::string material{};
bool tuned{false};
};

The Guitar class has the following private data members:

• A dynamically allocated array of MusicStrings.


• A nonnegative number representing the number of MusicStrings on this Guitar

You might also like