Lecture 12: Midterm exam review | CMSC 240 Software Systems Development - Fall 2023

Lecture 12: Midterm exam review

Objective

Review the course topics and content that will be assessed on the midterm exam.

Lecture Topics

Exam Details

What to Study

Other resources:
Unix Tutorial

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

Lectures:

Practice Questions

Pass by value, pass by pointer, pass by reference

Consider the following C++ code.

passby.cpp

#include <iostream>
using namespace std;

void passByValue(float copyOfValue) 
{
    copyOfValue = 1234.567;
}

void passByPointer(float* addressOfValue) 
{
    *addressOfValue = 1234.567;
}

void passByReference(float& referenceToValue) 
{
    referenceToValue = 9876.543;
}

int main() 
{
    float value = 5551.212;

    passByValue(value);

    cout << value << endl;

    passByPointer(&value);

    cout << value << endl;

    passByReference(value);

    cout << value << endl;

    return 0;
}

Memory layout

MemoryLayout

Command line arguments

args.cpp

#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
    if (argc != 2) // argc counts the num of CLPs
    {
        cerr << "Usage: " << argv[0]
             << " <first name>" << endl;
        exit(1);
    }

    cout << "Hello " << argv[1] << endl;

    return 0;
}

Consider the C++ program args.cpp above.

Draw the State of Stack Memory

swap.cpp

#include <iostream>
using namespace std;

// Swap two int values.
void swap(int* a, int* b)
{
    int temp = *a;   // store contents of a in temp
    *a = *b;         // put contents of b into a
    *b = temp;       // put temp a into b
    
    // Draw the state of memory here <----

}

int main()
{
    int x = 12;
    int y = 33;
    swap(&x, &y);  // pass by pointer
    cout << "x == " << x << "  y == " << y << endl;
    return 0;
}

MemoryGrid

Consider the C++ program swap.cpp above.

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;
};

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.