1. Homepage
  2. Programming
  3. CSC232 Data Structures HW01 - Extending the Bag ADT

CSC232 Data Structures HW01 - Extending the Bag ADT

Engage in a Conversation
USMSUMissouri State UniversityCSC232Data StructuresExtending the Bag ADTC++

HW01 - Extending the Bag ADT

In this assignment, the student shall add operations, i.e., extend the functionality of the VectorBag (first encountered in Lab 01) by adding operations above and beyond those found in the Bag interface. CourseNana.COM

HW01 Class Diagram CourseNana.COM

Due Date

This assignment is due by 11:59:59 PM on Saturday, 18 February 2023. CourseNana.COM

Background

As presented in Geeks for Geeks, CourseNana.COM

The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming. CourseNana.COM

Inheritance is a feature or a process in which, new classes are created from the existing classes. The new class created is called “derived class” or “child class” and the existing class is known as the “base class” or “parent class”. The derived class now is said to be inherited from the base class. CourseNana.COM

When we say derived class inherits the base class, it means, the derived class inherits all the properties of the base class, without changing the properties of base class and may add new features to its own. These new features in the derived class will not affect the base class. The derived class is the specialized class for the base class. CourseNana.COM

Advanced Reading

  • General > Files > Class Materials > Setting Up WSL for CSC232.pdW
  • Walls & Mirrors, Chapter 1: Data Abstraction
    • Exercises 6, 7 & 8
    • Programming Problem 6

Objectives

Upon successful completion of this assignment, the student has CourseNana.COM

  • gained experience in extending a class hierarchy
  • defined operations in terms of existing operations
  • learned how to easily encapsulate their classes in a custom namespace

Tasks

In this assignment, you'll be working on Programming Problem 6 from Chapter 1 of our textbook. This problem refers back to Exercises 6-8 in the same chapter and repeated below: CourseNana.COM

Set Operations with Bags

Union

The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method unionWith for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the method’s one argument. Include sufficient comments to fully specify the method. CourseNana.COM

Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.unionWith(bag2) returns a bag containing the strings a, b, b, b, c, d, and e. Note that unionWith does not affect the contents of bag1 and bag2. CourseNana.COM

Intersection

The intersection of two bags is a new bag containing the entries that occur in both of the original two bags. Design and specify a method intersectionWith for the ADT bag that returns as a new bag the intersection of the bag receiving the call to the method and the bag that is the method’s one argument. Include sufficient comments to fully specify the method. CourseNana.COM

Note that the intersection of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the intersection of these bags contains x two times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.intersectionWith(bag2) returns a bag containing only the string b. Note that intersection does not affect the contents of bag1 and bag2. CourseNana.COM

Difference

The difference of two bags is a new bag containing the entries that would be left in one bag after removing those that also occur in the second. Design and specify a method differenceWith for the ADT bag that returns as a new bag the difference of the bag receiving the call to the method and the bag that is the method’s one argument. Include sufficient comments to fully specify the method. CourseNana.COM

Note that the difference of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the difference of these bags contains x three times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.differenceWith(bag2) returns a bag containing only the strings a and c. Note that difference does not affect the contents of bag1 and bag2. CourseNana.COM

We shall develop these methods in a subclass of the VectorBag class with the following tasks: CourseNana.COM

  1. Task 1: Declare the SetOpVectorBag subclass of the VectorBag class
  2. Task 2: Define the unionWith( ) operation
  3. Task 3: Define instersectionWith( ) operation
  4. Task 4: Define differenceWith( ) operation
  5. Task 5: Encapsulate class hierarchy in the csc232 namespace

Task 1: Declaring the SetOpVectorBag class

In this task we declare our new subclass of the VectorBag class. CourseNana.COM

Discussion of Subclasses

This task requires a very specific syntax for a class declaration. The following rules must be adhered to in order for the first set of unit test suite to pass. CourseNana.COM

  1. Your template declaration (e.g., template <typename...) and class declaration (e.g., class foo...) are on two separate lines, with no blank space in between them. For example: CourseNana.COM

    template <typename T>
    class foo : public bar<T>
    {
    public:
        foo( ) = default; // explicitly define default construct
        ~f( ) = default;  // explicitly define default destructor
    };
  2. Your template parameter must be as shown above, simply T CourseNana.COM

  3. You must have one space character in between each element of the declarations CourseNana.COM

  4. You must match the spelling and case of the class you'll be asked to declare in this task. CourseNana.COM

Now on to the steps to take for this task. CourseNana.COM

  1. Open the file named set-op-vector-bag.h and look for the first TODO. At this point, just make a "bare-bones" declaration similar to the one shown in the rules above. That is, only a template declaration, class declaration with an empty body: CourseNana.COM

    template <typename T>
    class SetOpVectorBag : public VectorBag<T>
    {
    public:
        SetOpVectorBag( ) = default;
        ~SetOpVectorBag( ) = default;
    };
  2. When you have completed this task, open the csc232.h header file and toggle the SKIP_TESTING_TASK_1 macro from TRUE to FALSE. CourseNana.COM

  3. Build and execute the test_task1 target to test your solution (or run ctest). CourseNana.COM

  4. When all the tests for this task pass, commit and push your changes to GitHub. CourseNana.COM

Task 2: Define the unionWith( ) operation

In this task, we define the unionWith() operation. CourseNana.COM

Discussion of the union operation

In this task, we actually set about extending the VectorBag<T> template class. We begin with the unionWith() operation. Let's discuss the use of this operation first. CourseNana.COM

SetOpVectorBag<string> a{ };
SetOpVectorBag<string> b{ };
// ... populate bags with items
a.add( "a" ); a.add( "b" ); a.add( "c" );
b.add( "b" ); b.add( "b" ); b.add( "d" ); b.add( "e" );
SetOpVectorBag<string> c = a.unionWith( b );
// c = { "a", "b", "c", "b", "b", "d", "e" }

This is a new method to be added to the SetOpVectorBag<T> class declaration. Take a look at how it's used and note CourseNana.COM

  • The operation returns something; since whatever it is returning is getting assigned to an instance of SetOpVectorBag<T> (i.e., c), we can infer that the return type is SetOpVectorBag<T>.
  • The operation is given an argument (i.e., b); look at the argument's declaration, we can infer that the parameter's type is also SetOpVectorBag<T>.
  • There are two bags involved in this operation:
    • The receiver of the message (i.e., a)
    • The argument passed along with this message (i.e., b)

Note that this new method must be declared as public if we are to access it as shown above. All this analysis and design thus leads to the following change to our template class declaration: CourseNana.COM

// In set-op-vector-bag.h
template <typename T>
class SetOpVectorBag : public VectorBag<T>
{
public:
    SetOpVectorBag( ) = default;
    ~SetOpVectorBag( ) = default;

    SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
};

And we define this method in the set-op-vector-bag.cpp source file: CourseNana.COM

// In set-op-vector-bag.cpp
template <typename T>
SetOpVectorBag<T> SetOpVectorBag<T>::unionWith( SetOpVectorBag<T> rhs )
{
    SetOpVectorBag<T> result;

    // Use bag operations to populate result with appropriate values
    // Note: Directly referring to an inherited operation is invoking the
    // receiver object's method, e.g.,
    // int size = VectorBag<T>::getCurrentSize(); // this is the size of this bag
    // int otherSize = rhs.getCurrentSize();      // this is the size of the argument

    return result;
}

Let's get started on this task: CourseNana.COM

  1. Open the set-op-vector-bag.h header file and modify your class declaration by adding the new operation: CourseNana.COM

    template <typename T>
    class SetOpVectorBag : public VectorBag<T>
    {
    public:
        SetOpVectorBag( ) = default;
        ~SetOpVectorBag( ) = default;
    
        SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
    };
  2. Now open the set-op-vector-bag.cpp source file and implement this method using the skeleton presented in the task discussion above. CourseNana.COM

  3. When you have completed this task, open the csc232.h header file and toggle the SKIP_TESTING_TASK_2 macro from TRUE to FALSE. CourseNana.COM

  4. Build and execute the test_task2 target to test your solution (or run ctest). CourseNana.COM

  5. When all the tests for this task pass, commit and push your changes to GitHub. CourseNana.COM

Task 3: Define instersectionWith( ) operation

In this task, we define the intersectionWith() operation. CourseNana.COM

Discussion of the intersection operation

In this task, we continue to extend the VectorBag<T> template class by adding the intersectionWith() operation. Let's discuss the use of this operation first. CourseNana.COM

SetOpVectorBag<string> a{ };
SetOpVectorBag<string> b{ };
// ... populate bags with items
a.add( "a" ); a.add( "b" ); a.add( "c" );
b.add( "b" ); b.add( "b" ); b.add( "d" ); b.add( "e" );
SetOpVectorBag<string> c = a.intersectionWith( b );
// c = { "b" }

This is a new method to be added to the SetOpVectorBag<T> class declaration. Take a look at how it's used and note CourseNana.COM

  • The operation returns something; since whatever it is returning is getting assigned to an instance of SetOpVectorBag<T> (i.e., c), we can infer that the return type is SetOpVectorBag<T>.
  • The operation is given an argument (i.e., b); look at the argument's declaration, we can infer that the parameter's type is also SetOpVectorBag<T>.
  • There are two bags involved in this operation:
    • The receiver of the message (i.e., a)
    • The argument passed along with this message (i.e., b)

The last bullet is notable in that the definition of this operation will access either the receiver's methods or the arguments methods (or both) in order to fulfill the requirements of this method. CourseNana.COM

Finally, note that this new method must be declared as public if we are to access it as shown above. All this analysis and design thus leads to the following change to our template class declaration: CourseNana.COM

template <typename T>
class SetOpVectorBag : public VectorBag<T>
{
public:
    SetOpVectorBag( ) = default;
    ~SetOpVectorBag( ) = default;

    SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
    SetOpVectorBag<T> intersectionWith( SetOpVectorBag<T> rhs );
};

And we define this method in the set-op-vector-bag.cpp source file: CourseNana.COM

// In set-op-vector-bag.cpp
template <typename T>
SetOpVectorBag<T> SetOpVectorBag<T>::intersectionWith( SetOpVectorBag<T> rhs )
{
    SetOpVectorBag<T> result;

    // Use bag operations to populate result with appropriate values
    // Note: Directly referring to an inherited operation is invoking the
    // receiver object's method, e.g.,
    // int size = VectorBag<T>::getCurrentSize(); // this is the size of this bag
    // int otherSize = rhs.getCurrentSize();      // this is the size of the argument

    return result;
}

Let's get started on this task: CourseNana.COM

  1. Open the set-op-vector-bag.h header file and modify your class declaration by adding the new operation: CourseNana.COM

    template <typename T>
    class SetOpVectorBag : public VectorBag<T>
    {
    public:
        SetOpVectorBag( ) = default;
        ~SetOpVectorBag( ) = default;
    
        SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
        SetOpVectorBag<T> intersectionWith( SetOpVectorBag<T> rhs );
    };
  2. Now open the set-op-vector-bag.cpp source file and implement this method using the skeleton presented in the task discussion above. CourseNana.COM

  3. When you have completed this task, open the csc232.h header file and toggle the SKIP_TESTING_TASK_3 macro from TRUE to FALSE. CourseNana.COM

  4. Build and execute the test_task3 target to test your solution (or run ctest). CourseNana.COM

  5. When all the tests for this task pass, commit and push your changes to GitHub. CourseNana.COM

Task 4: Define differenceWith( ) operation

In this task, we define the differenceWith() operation. CourseNana.COM

Discussion of the difference operation

In this task, we continue to extend the VectorBag<T> template class by adding the differenceWith() operation. Let's discuss the use of this operation first. CourseNana.COM

SetOpVectorBag<string> a{ };
SetOpVectorBag<string> b{ };
// ... populate bags with items
a.add( "a" ); a.add( "b" ); a.add( "c" );
b.add( "b" ); b.add( "b" ); b.add( "d" ); b.add( "e" );
SetOpVectorBag<string> c = a.differenceWith( b );
// c = { "a", "c" }

This is a new method to be added to the SetOpVectorBag<T> class declaration. Take a look at how it's used and note CourseNana.COM

  • The operation returns something; since whatever it is returning is getting assigned to an instance of SetOpVectorBag<T> (i.e., c), we can infer that the return type is SetOpVectorBag<T>.
  • The operation is given an argument (i.e., b); look at the argument's declaration, we can infer that the parameter's type is also SetOpVectorBag<T>.
  • There are two bags involved in this operation:
    • The receiver of the message (i.e., a)
    • The argument passed along with this message (i.e., b)

The last bullet is notable in that the definition of this operation will access either the receiver's methods or the arguments methods (or both) in order to fulfill the requirements of this method. CourseNana.COM

Finally, note that this new method must be declared as public if we are to access it as shown above. All this analysis and design thus leads to the following change to our template class declaration: CourseNana.COM

template <typename T>
class SetOpVectorBag : public VectorBag<T>
{
public:
    SetOpVectorBag( ) = default;
    ~SetOpVectorBag( ) = default;

    SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
    SetOpVectorBag<T> intersectionWith( SetOpVectorBag<T> rhs );
    SetOpVectorBag<T> differenceWith( SetOpVectorBag<T> rhs );
};

And we define this method in the set-op-vector-bag.cpp source file: CourseNana.COM

// In set-op-vector-bag.cpp
template <typename T>
SetOpVectorBag<T> SetOpVectorBag<T>::differenceWith( SetOpVectorBag<T> rhs )
{
    SetOpVectorBag<T> result;

    // Use bag operations to populate result with appropriate values
    // Note: Directly referring to an inherited operation is invoking the
    // receiver object's method, e.g.,
    // int size = VectorBag<T>::getCurrentSize(); // this is the size of this bag
    // int otherSize = rhs.getCurrentSize();      // this is the size of the argument

    return result;
}

Let's get started on this task: CourseNana.COM

  1. Open the set-op-vector-bag.h header file and modify your class declaration by adding the new operation: CourseNana.COM

    template <typename T>
    class SetOpVectorBag : public VectorBag<T>
    {
    public:
        SetOpVectorBag( ) = default;
        ~SetOpVectorBag( ) = default;
    
        SetOpVectorBag<T> unionWith( SetOpVectorBag<T> rhs );
        SetOpVectorBag<T> intersectionWith( SetOpVectorBag<T> rhs );
        SetOpVectorBag<T> differenceWith( SetOpVectorBag<T> rhs );
    };
  2. Now open the set-op-vector-bag.cpp source file and implement this method using the skeleton presented in the task discussion above. CourseNana.COM

  3. When you have completed this task, open the csc232.h header file and toggle the SKIP_TESTING_TASK_4 macro from TRUE to FALSE. CourseNana.COM

  4. Build and execute the test_task4 target to test your solution (or run ctest). CourseNana.COM

  5. When all the tests for this task pass, commit and push your changes to GitHub. CourseNana.COM

Task 5: Encapsulate class hierarchy in the csc232 namespace

Looking back at the UML Class Diagram at the beginning of this README, we note that each of the classes in the class hierarchy actually appear to be members of the csc232 namespace. In this task, we make that happen. CourseNana.COM

Discussion of namespaces

While unlikely in today's programming exercise, it is possible that we come up with a class name that is common enough that it might clash with another similarly named class pulled in by some dependency in our project. To help mitigate this name clash, we can encapsulate our classes within a namespace. For us, we'll create a namespace named csc232 to do just that. CourseNana.COM

Creating a namespace is easy; just use the keyword namespace followed by an identifier (of your choice) and then provide a pair of parentheses to define the scope of this namespace: CourseNana.COM

namespace csc232
{

}

If we declare a class in that namespace, as in CourseNana.COM

namespace csc232
{
    class Foo {};
}

Then, when we want to create an instance of that class, we must provide context: CourseNana.COM

csc232::Foo bar{ }; // creates an instance of csc232::Foo named bar

We can shorten our typing with a using clause as well: CourseNana.COM

using csc232::Foo;

Foo bar{ }; // creates an instance of csc232::Foo named bar
  1. Open up the bag.h header file and put the Bag<T> template class declaration inside a namespace named csc232. CourseNana.COM

  2. Next, do the same thing to the VectorBag<T> template class declaration found in vector-bag.h. CourseNana.COM

  3. So that the definitions found in vector-bag.cpp are still properly defined, add a using clause to the beginning of the file (on a line after the #include "vector-bag.h"): CourseNana.COM

    using csc232::VectorBag;
  4. Next, do the same thing to the SetOpVectorBag<T> template class declaration found in set-op-vector-bag.h. CourseNana.COM

  5. So that the definitions found in set-op-vector-bag.cpp are still properly defined, add a using clause to the beginning of the file (on a line after the #include "set-op-vector-bag.h"): CourseNana.COM

    using csc232::SetOpVectorBag;
  6. When you have completed this task, open the csc232.h header file and toggle the SKIP_TESTING_TASK_5 macro from TRUE to FALSE. CourseNana.COM

  7. Build and execute the test_task5 target to test your solution (or run ctest). CourseNana.COM

  8. When all the tests for this task pass, commit and push your changes to GitHub. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
US代写,MSU代写,Missouri State University代写,CSC232代写,Data Structures代写,Extending the Bag ADT代写,C++代写,US代编,MSU代编,Missouri State University代编,CSC232代编,Data Structures代编,Extending the Bag ADT代编,C++代编,US代考,MSU代考,Missouri State University代考,CSC232代考,Data Structures代考,Extending the Bag ADT代考,C++代考,UShelp,MSUhelp,Missouri State Universityhelp,CSC232help,Data Structureshelp,Extending the Bag ADThelp,C++help,US作业代写,MSU作业代写,Missouri State University作业代写,CSC232作业代写,Data Structures作业代写,Extending the Bag ADT作业代写,C++作业代写,US编程代写,MSU编程代写,Missouri State University编程代写,CSC232编程代写,Data Structures编程代写,Extending the Bag ADT编程代写,C++编程代写,USprogramming help,MSUprogramming help,Missouri State Universityprogramming help,CSC232programming help,Data Structuresprogramming help,Extending the Bag ADTprogramming help,C++programming help,USassignment help,MSUassignment help,Missouri State Universityassignment help,CSC232assignment help,Data Structuresassignment help,Extending the Bag ADTassignment help,C++assignment help,USsolution,MSUsolution,Missouri State Universitysolution,CSC232solution,Data Structuressolution,Extending the Bag ADTsolution,C++solution,