Quiz 2 review
Objective
Review the course topics and content that will be assessed on the second quiz.
Quiz Details
- Paper quiz
- 4 multi-part questions, you will be asked to review code and answer questions about the code, write code, or draw something like a UML diagram.
- Total of 50 points
- 30 minutes to complete the quiz
What to Study
Text book: Programming Principles and Practice Using C++, 2nd Edition by Bjarne Stroustrup
- Chapter 9 Classes, etc.
Lectures:
- Slides: Classes and OOP
- Classes
- Class syntax
- access specifiers (public, private, and protected)
- Abstraction
- Encapsulation
- Classes
- Slides: Enum, Static, Overloading
- Constructors
- Enumerations
- Static members
- Operator overloading
- Lecture Notes: Scope, Friends, Destructors, Composition, and UML
- Scope
- Friends
- Destructors
- Composition
- UML Diagrams
- Slides: Inheritance, Polymorphism, Virtual Functions, Abstract Classes
- Inheritance
- Polymorphism
- Virtual functions
Practice Questions
Access Specifiers (public, private, protected)
Book.cpp
class Book {
public:
Book(std::string t) : title(t) {}
std::string getTitle() const {
return title;
}
void summarize() {
std::cout << "This is a book titled: " << title << std::endl;
}
protected:
std::string title;
};
class Novel : public Book {
public:
Novel(std::string t, std::string g) : Book(t), genre(g) {}
void summarize() {
std::cout << "This is a " << genre << " novel titled: " << title << std::endl;
}
private:
std::string genre;
};
int main()
{
Novel mystery{"A Study in Scarlet", "Mystery"};
// Can you do this?
std::cout << mystery.title << std::endl;
return 0;
}
Consider the C++ program Book.cpp above.
- Describe the purpose of the
protected
access specifier in theBook
class, and explain how it contrasts with theprivate
access specifier. - In the
Novel
class, which inherits fromBook
, can the summarize method directly access thetitle
attribute? Provide a justification for your answer. - If you create an object (instance) of the
Novel
class within themain
function, can you directly access thetitle
attribute from that object? Elaborate on your answer.
Dynamic Memory Allocation and Deallocation
Particle.cpp
class Particle {
public:
Particle(float x, float y) : posX(x), posY(y) {}
void move(float dx, float dy) {
posX += dx;
posY += dy;
}
void printPosition() const {
std::cout << "Position: (" << posX << ", " << posY << ")" << std::endl;
}
private:
float posX, posY;
};
int main() {
Particle* p1 = new Particle(5.0, 7.5);
p1->move(2.0, -3.0);
p1->printPosition();
// Some code here...
return 0;
}
Consider the C++ program Particle.cpp above.
- What does the
new
keyword do in the main function? - What is returned from the expression
new Particle(5.0, 7.5)
? - As the code stands, there’s a potential issue related to dynamic memory management. Identify the issue and explain why it’s problematic.
- Provide a modification to the code to fix the potential memory management issue you identified.
Polymorphism and Virtual Functions
Media.cpp
class MultimediaContent {
public:
virtual void play() {
std::cout << "Playing generic multimedia content." << std::endl;
}
};
class Song : public MultimediaContent {
public:
void play() {
std::cout << "Playing a melodious song." << std::endl;
}
};
class Video : public MultimediaContent {
public:
void play() {
std::cout << "Streaming a captivating video." << std::endl;
}
};
int main() {
MultimediaContent* content1 = new Song();
MultimediaContent* content2 = new Video();
content1->play(); // ?
content2->play(); // ?
delete content1;
delete content2;
return 0;
}
Consider the C++ program Media.cpp above.
- What is the significance of the
virtual
keyword in theMultimediaContent
class regarding theplay
method? - Given the
main
function provided, what will the output be when the program is executed? - Describe what is meant by the term
polymorphism
in the context of the provided code.
Implementing the constructor and methods of a class
Consider the following C++ code in the file Calculator.h
.
#ifndef CALCULATOR_H
#define CALCULATOR_H
class Calculator {
public:
// Constructor: Initializes the calculator with a given value.
Calculator(double initialValue);
// Adds a value to the current result.
void add(double value);
// Subtracts a value from the current result.
void subtract(double value);
// Multiplies the current result by a given value.
void multiply(double value);
// Divides the current result by a given value. If the given value is zero, do nothing.
void divide(double value);
// Returns the current result.
double getResult() const;
private:
double result;
};
#endif
Use the C++ code above to answer the following questions.
-
Write the C++ code that you would put into the file
Calculator.cpp
to implement the constructor and methods for theCalculator
class found inCalculator.h
. Do your best to write code that will compile. Remember to use#include
as needed for external libraries or theCalculator.h
file. -
Draw a UML diagram that represents the
Calculator
class. Remember to include the name of the class, the member variables (attributes) and their type, and member functions (methods) and their return and parameter types. You should include the access specifiers for each attribute and method. If an attribute or method ispublic
indicate this with a+
sign. If an attribute or method isprivate
indicate this with a-
sign.