Quiz 2 review | CMSC 240 Software Systems Development - Fall 2024

Quiz 2 review

Objective

Review the course topics and content that will be assessed on the second quiz.

Quiz Details

What to Study

Text book: Programming Principles and Practice Using C++, 2nd Edition by Bjarne Stroustrup

Lectures:

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.

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.

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.

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.