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

Quiz 4 review

Objective

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

Quiz Details

What to Study

Lectures:

Practice Questions

C++ Smart pointers

Consider the following C++ code.

main.cpp

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

class Item 
{
public:
    Item() { cout << "Item constructed" << endl; }
    ~Item() { cout << "Item destroyed" << endl; }
};

void process(shared_ptr<Item>& sharedItem) 
{
    shared_ptr<Item> localShared = sharedItem;

    cout << "Reference count inside function = " << localShared.use_count() << endl;
}

int main() 
{
    shared_ptr<Item> myItem{new Item()};

    cout << "Reference count in main, before function call = " << myItem.use_count() << endl;

    process(myItem);

    cout << "Reference count in main, after function call = " << myItem.use_count() << endl;

    return 0;
}

Unit Testing

Consider the following C++ code.

test.cpp

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include <stdexcept>
#include <vector>
#include "doctest.h"


using namespace std;

// Function prototype
int maxElement(vector<int> nums); 

TEST_CASE("Testing the maxElement function") 
{
    CHECK(maxElement({1, 2, 3, 4, 5}) == 5);
    CHECK(maxElement({-10, -20, -30, -40, -5}) == -5);
    CHECK(maxElement({100, 200, 300, 400, 500}) == 500);
    CHECK(maxElement({42}) == 42);
    CHECK_THROWS_AS(maxElement({}), invalid_argument);
}

Unit Testing

Consider the following C++ code.

test.cpp

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include <stdexcept>
#include "doctest.h"

using namespace std;

string numberClassifier(int number) 
{
    if (number == 0) 
    {
        throw invalid_argument("Number must not be zero");
    } 
    else if (number > 0) 
    {
        if (number % 2 == 0) 
        {
            return "Even Positive";
        } 
        else 
        {
            return "Odd Positive";
        }
    } 
    else 
    { 
        if (number % 2 == 0) 
        {
            return "Even Negative";
        } 
        else 
        {
            return "Odd Negative";
        }
    }
}

In the space below write a unit test that will provide 100% statement coverage of the numberClassifier function.

Git

Consider the following Git commands.

$ git init
$ git branch -m main
$ echo "Line One of Readme File" > README.md 
$ git add README.md
$ git commit -m "Initial Commit"
$ echo "Line Two of Readme File" >> README.md 
$ git add README.md
$ git commit -m "Second Commit"
$ git branch my_feature_branch
$ git checkout my_feature_branch
$ echo "Line Three of Readme File" >> README.md
$ git add README.md
$ git commit -m "Initial Feature Branch Commit"
$ git checkout main
$ echo "New file" > anotherfile.txt
$ git add anotherfile.txt
$ git commit -m "Third Commit"
$ git merge my_feature_branch -m "Merging Feature"

Object-oriented programming

What are the four most important concepts in object-oriented programming? Describe them in detail.