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

Quiz 3 review

Objective

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

Quiz Details

What to Study

Lectures:

Practice Questions

Makefiles

Consider the following C++ code.

main.cpp

// main.cpp - Main function for the Library Management System

#include "Library.h"

int main() 
{
    Library library;
    library.addBook(Book("1984"));
    library.addBook(Book("Brave New World"));

    library.showBooks();
    return 0;
}

Book.h

// Book.h - Header file for the Book class

#ifndef BOOK_H
#define BOOK_H

#include <string>

class Book 
{
public:
    Book(const std::string& title);
    std::string getTitle() const;
private:
    std::string title;
};

#endif // BOOK_H

Book.cpp

// Book.cpp - Implementation of the Book class

#include "Book.h"

Book::Book(const std::string& title) : title(title) {}

std::string Book::getTitle() const 
{
    return title;
}

Library.h

// Library.h - Header file for the Library class

#ifndef LIBRARY_H
#define LIBRARY_H

#include <vector>
#include "Book.h"

class Library 
{
public:
    void addBook(const Book& book);
    void showBooks() const;
private:
    std::vector<Book> books;
};

#endif // LIBRARY_H

Library.cpp

// Library.cpp - Implementation of the Library class

#include <iostream>
#include "Library.h"

using namespace std;

void Library::addBook(const Book& book) 
{
    books.push_back(book);
}

void Library::showBooks() const 
{
    for (Book& book : books) 
    {
        cout << book.getTitle() << endl;
    }
}

C++ Exception handling with try/catch

Consider the following C++ code.

main.cpp

// Will throw an exception on bad input.
int area(int length, int width)     
{
    // Validate the inputs.
    if(length <= 0 || width <= 0)
    { 
        // Throw an invalid argument exception.
        throw invalid_argument{"Bad argument to area()"};
    }

    int result = length * width;

    // Check for an overflow in the result.
    if (result / length != width)
    {
        // Throw an overflow error exception.
        throw overflow_error{"Overflow occurred in area()"};
    }

    return result;
}


int main()
{
    int l;
    int w;
    cout << "Enter values for length and width:" << endl;
    cin >> l >> w;
    
    int result = area(l, w);
    cout << "Area == " << result << endl;

    return 0;
}

C++ Templates

Consider the following C++ code.

main.cpp

#include <iostream>
#include <vector>
using namespace std;

double averageVector(vector<double> vec) 
{
    if (vec.empty()) 
    {
        // Return 0 for empty vector
        return 0.0; 
    }

    double sum = 0.0;

    for (double num : vec) 
    {
        sum += num;
    }

    return sum / vec.size();
}

int main() 
{
    vector<double> vec = {1.5, 2.5, 3.5, 4.5, 5.5};
    cout << "Average of vector elements is " << averageVector(vec) << endl;
    return 0;
}

Regular Expressions

Consider the following C++ code.

main.cpp

/*
Vehicle Registration Number Validation:
Validate vehicle registration numbers for a specific format. 
Here are the rules for validating the vehicle registration numbers:

    The registration number must start with 2 to 3 uppercase letters.
    This is followed by a dash ('-').
    After the dash, there should be a sequence of 4 digits.
    This is followed by a dash ('-')
    After the second dash, it ends with 1 to 2 uppercase letters.

Add a regex pattern that matches the vehicle registration number validation rules above as valid.
*/

#include <iostream>
#include <regex>
#include <string>
using namespace std;

int main()
{
    string registrationNumber;
    cout << "Enter a vehicle registration number: ";
    cin >> registrationNumber;

    // Define a regular expression pattern for vehicle registration number validation
    regex pattern{R"( YOUR REGEX PATTERN HERE )"};

    if (regex_match(registrationNumber, pattern))
    {
        cout << "Valid vehicle registration number!" << endl;
    }
    else
    {
        cout << "Invalid vehicle registration number." << endl;
    }

    return 0;
}

Serialization

Be able to answer the following questions:

  1. What is serialization?

  2. What is deserialization?

  3. Give an example of when you would use serialization/deserialization.