Professional Documents
Culture Documents
Assessment 2 Module 5 C++
Assessment 2 Module 5 C++
Assessment 2 Module 5 C++
Development
Section 1
Question 1: Pointer to Objects
Code C++
#include <iostream>
using namespace std;
public:
// Constructor
HealthActivity(string name, int stepsCount, double distance) {
userName = name;
steps = stepsCount;
walkingRunningDistance = distance;
}
// Setter functions
void setName(string name) {
userName = name;
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
// Getter functions
string getName() {
return userName;
}
int getSteps() {
return steps;
}
double getWalkingRunningDistance() {
return walkingRunningDistance;
}
};
string name;
int steps;
double distance;
totalSteps += activities[i]->getSteps();
totalDistance += activities[i]->getWalkingRunningDistance();
}
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
cout << "Average steps of " << size << " users: " << averageSteps << " steps" << endl;
cout << "Average distance of walking + running for " << size << " users: " <<
averageDistance << " Kms" << endl;
}
int main() {
const int NUM_USERS = 5;
HealthActivity* activityArray[NUM_USERS]; // Array of pointers to HealthActivity objects
setFunction(activityArray, NUM_USERS);
getFunction(activityArray, NUM_USERS);
return 0;
}
OUTPUT
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
EXPLANATION:
The code is designed to handle health activity data for users of the Health app on an iPhone 12
Pro Max. It creates a class called HealthActivity to represent the activities of each user. The
class has attributes like userName, steps, and walkingRunningDistance, which store the user's
name, number of steps, and distance walked or run.
The program starts by asking the user to input the name, number of steps, and walking + running
distance for each user. It prompts the user five times to input this data. The information entered
by the user is stored in an array of pointers to HealthActivity objects.
The setFunction function is responsible for taking user input and creating the HealthActivity
objects. It iterates over the array and prompts the user to enter the details for each user. It creates
a new HealthActivity object using the provided input and assigns it to the corresponding
position in the array.
The getFunction function displays the user data for each HealthActivity object in the array. It
iterates over the array, retrieves the user data using getter functions, and prints it on the screen.
Additionally, it calculates the total steps and total distance for all the users.
Finally, it calculates the average steps and average distance by dividing the total steps and total
distance by the number of users. It displays the average values on the screen along with
appropriate units.
The main function serves as the entry point of the program. It declares the array of pointers to
HealthActivity objects, calls the setFunction to populate the array with user input, and then
calls the getFunction to display the user data and average values. It also takes care of
deallocating the memory allocated for the HealthActivity objects to avoid memory leaks.
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
PART 2:
Code C++:
#include <iostream>
public:
// Constructor to initialize the complex number
Complex(int r = 0, int i = 0) {
real = r;
imaginary = i;
}
int main() {
int real, imaginary;
// Get the values for the first complex number from the user
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
// Create the first complex number object using the values entered by the user
Complex c1(real, imaginary);
// Get the values for the second complex number from the user
cout << "\nEnter 2nd Complex number values:" << endl;
cout << "Enter Real value: ";
cin >> real;
cout << "Enter imaginary value: ";
cin >> imaginary;
// Create the second complex number object using the values entered by the user
Complex c2(real, imaginary);
int choice;
Complex result;
switch (choice) {
case 1:
// Perform addition of the two complex numbers
result = c1 + c2;
cout << "\nC1: ";
c1.display();
cout << "C2: ";
c2.display();
cout << "C3: ";
result.display();
break;
case 2:
// Perform subtraction of the two complex numbers
result = c1 - c2;
cout << "\nC1: ";
c1.display();
cout << "C2: ";
c2.display();
cout << "C3: ";
result.display();
break;
case 3:
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
case 4:
break;
default:
cout << "Invalid option. Please try again." << endl;
break;
}
} while (choice != 4);
return 0;
}
3. Inside the Complex class, there is a constructor that initializes the complex number with
default values of 0 for both the real and imaginary parts. The constructor can also be used
to initialize the complex number with specific values.
4. The Complex class overloads three arithmetic operators: +, -, and *. These operators are
used to perform addition, subtraction, and multiplication operations on complex numbers.
5. The operator+ function overloads the + operator and performs addition of two complex
numbers. It creates a new Complex object and sets its real part as the sum of the real
parts of the two complex numbers, and sets its imaginary part as the sum of the imaginary
parts of the two complex numbers. The result is then returned.
6. Similarly, the operator- function overloads the - operator for subtraction of complex
numbers, and the operator* function overloads the * operator for multiplication of
complex numbers. Both of these functions follow a similar logic to perform the
respective operations.
7. The display function is used to display the complex number in the format "real +
imaginary i".
8. In the main function, the user is prompted to enter the real and imaginary parts of the
first complex number. These values are then used to create a Complex object called c1.
9. Similarly, the user is prompted to enter the real and imaginary parts of the second
complex number. These values are used to create another Complex object called c2.
10. Inside the do-while loop, the user is presented with a menu where they can choose the
operation to perform on the complex numbers: addition, subtraction, multiplication, or
exit.
11. Based on the user's choice, the corresponding operation is performed using the
overloaded operators, and the result is stored in the result variable.
12. The display function is then called on the c1, c2, and result objects to display the
complex numbers before and after the operation.
13. The loop continues until the user chooses to exit by entering 4.
14. Finally, the program ends with a return 0 statement.
OUTPUT
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
Question 2: Polymorphism
Code C++
#include <iostream>
#include <string>
public:
// Constructor to initialize the title and price of the game
VideoGame(const std::string& gameTitle, double gamePrice)
: title(gameTitle), price(gamePrice) {}
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
public:
// Constructor to initialize the title, price, and OS type of the computer game
ComputerGame(const std::string& gameTitle, double gamePrice, const std::string&
gameOsType)
: VideoGame(gameTitle, gamePrice), osType(gameOsType) {}
private:
std::string consoleType;
public:
// Constructor to initialize the title, price, and console type of the console game
ConsoleGame(const std::string& gameTitle, double gamePrice, const std::string&
gameConsoleType)
: VideoGame(gameTitle, gamePrice), consoleType(gameConsoleType) {}
int main() {
const int MAX_GAMES = 100;
VideoGame* videoGamesArray[MAX_GAMES];
int numGames = 0;
std::string choice;
do {
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
std::string title;
double price;
std::string gameType;
if (gameType == "o") {
std::string osType;
numGames++;
videoGamesArray[i]->display();
}
return 0;
}
Code explanation:
This program allows the user to enter data for video games, either computer games or console
games. It uses object-oriented programming principles with inheritance and polymorphism.
Here's a breakdown of the code:
The VideoGame class is the base class representing a video game. It has member
variables title and price to store the game's title and price, respectively. It also has a
virtual function display() to display the game details.
The ComputerGame class is derived from VideoGame and represents a computer game.
It adds an additional member variable osType to store the operating system type. It
overrides the display() function to include the OS type in the output.
The ConsoleGame class is also derived from VideoGame and represents a console
game. It adds a member variable consoleType to store the console type. It overrides the
display() function to include the console type in the output.
In the main() function, an array of VideoGame pointers videoGamesArray is declared
to store the entered video games. numGames keeps track of the number of games
entered.
The user is prompted to enter data for a computer game or a console game using a do-
while loop. Inside the loop, the user is asked for the game type, and based on the choice,
the program prompts for the game details (title, price, and additional details specific to
computer or console game). The entered game is then dynamically allocated using new
and stored in the videoGamesArray.
After exiting the loop, the program displays the list of entered video games by calling the
display() function for each game in the array.
Diploma in Software Assessment 2 Ksenia Vyazalova
Development
Finally, memory allocated for the video games is cleaned up using delete to avoid
memory leaks.