Question
what would be the solution code to this problem in c++?
The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (t
Sample Output: D Microsoft Visual Studio Debug Console --Exanple 1: Set of integera-- Adding: 18 Adding: S. Addingi is Adding
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the screenshots of code & output:

Below is the C++ code for same. Besides implementing the set class with the required methods, it is also tested for the given data types as well as with simple class objects.

1.) set.h

#include<iostream>
using namespace std;
/**sample test class for testing set**/
class testClass{
   public:
   string data;
   testClass(){
       }
       testClass(string name){
           data = name;
       }
       void operator=(const testClass &t){
           data=t.data;
       }
       bool operator==(const testClass &t){
           return (data==t.data);
       }
};
/**templated set class**/
template<typename T>
class set{
   int size;/**storeds size of set**/
   T* elements;/**dynamic pointer to set items**/
   public:
       set(){
           size=0;/**initializing size**/
       }
       /**method to add item to set**/
       void add(T element){
           for(int i = 0 ; i < size ; i++)/**if this element is duplicate return**/
           if(elements[i]==element)
           return;
           T temp[size];/**temporary array to store previous values**/
           for(int i = 0 ; i < size; i++)
           temp[i]=elements[i];
           elements=new T[++size];/**reallocating new array of incremented size**/
           for(int i = 0 ; i < size-1; i++)
           elements[i]=temp[i];/**recopying the previous elements back**/
           elements[size-1]=element;/**adding new element**/
       }
       /**method to get size of set**/
       int getNumberOfElements(){
           return size;
       }
       /**method to get dynamic pointer to set**/
       T* getItemSet(){
           return elements;
       }
};

2.) main.cpp

#include"set.h"

int main(){
   /**Test case 1: testing for integers**/
   cout<<"---Example 1: Set of integers---\n";
   cout<<"================================\n";
   set<int> integerSet = set<int>();
   cout<<" "<<" Adding: 10\n";integerSet.add(10);
   cout<<" "<<" Adding: 5\n";integerSet.add(5);
   cout<<" "<<" Adding: 15\n";integerSet.add(15);
   cout<<" "<<" Adding: 25\n";integerSet.add(25);
   cout<<" "<<" Adding: 15\n";integerSet.add(15);
   cout<<"--------------------------------\n\n";
   int integerSetSize = integerSet.getNumberOfElements();
   int* integerSetPtr = integerSet.getItemSet();
   cout<<"The first set has "<<integerSetSize<<" items\nThey are:\n";
   for(int i =0; i < integerSetSize; i++)
   cout<<"\t"<<integerSetPtr[i]<<"\n";
   delete integerSetPtr;
   cout<<"\n================================\n";
   /**Test case 2: testing for doubles**/
   cout<<"---Example 2: Set of doubles---\n";
   cout<<"================================\n";
   set<double> doubleSet = set<double>();
   cout<<" "<<" Adding: 1.5\n";doubleSet.add(1.5);
   cout<<" "<<" Adding: 5.6\n";doubleSet.add(5.6);
   cout<<" "<<" Adding: 12.8\n";doubleSet.add(12.8);
   cout<<" "<<" Adding: 1.5\n";doubleSet.add(1.5);
   cout<<" "<<" Adding: 12.8\n";doubleSet.add(12.8);
   cout<<"--------------------------------\n\n";
   int doubleSetSize = doubleSet.getNumberOfElements();
   double* doubleSetPtr = doubleSet.getItemSet();
   cout<<"The second set has "<<doubleSetSize<<" items\nThey are:\n";
   for(int i =0; i < doubleSetSize; i++)
   cout<<"\t"<<doubleSetPtr[i]<<"\n";
   delete doubleSetPtr;
   cout<<"\n================================\n";
   /**Test case 3: testing for strings**/
   cout<<"---Example 3: Set of strings---\n";
   cout<<"================================\n";
   set<string> stringSet = set<string>();
   cout<<" "<<" Adding: John Smith\n";stringSet.add("John Smith");
   cout<<" "<<" Adding: John doe\n";stringSet.add("John doe");
   cout<<" "<<" Adding: John Smith\n";stringSet.add("John Smith");
   cout<<" "<<" Adding: John black\n";stringSet.add("John black");
   cout<<"--------------------------------\n\n";
   int stringSetSize = stringSet.getNumberOfElements();
   string* stringSetPtr = stringSet.getItemSet();
   cout<<"The third set has "<<stringSetSize<<" items\nThey are:\n";
   for(int i =0; i < stringSetSize; i++)
   cout<<"\t"<<stringSetPtr[i]<<"\n";
   delete stringSetPtr;
   cout<<"\n================================\n";
   /**Test case 1: testing for class objects**/
   cout<<"---Example 4: Set of test class objects---\n";
   cout<<"================================\n";
   set<testClass> testClassSet = set<testClass>();
   cout<<" "<<" Adding: testclass(\"object1\")\n";testClassSet.add(testClass(string("object1")));
   cout<<" "<<" Adding: testclass(\"object2\")\n";testClassSet.add(testClass(string("object2")));
   cout<<" "<<" Adding: testclass(\"object2\")\n";testClassSet.add(testClass(string("object2")));
   cout<<" "<<" Adding: testclass(\"object1\")\n";testClassSet.add(testClass(string("object1")));
   cout<<"--------------------------------\n\n";
   int testClassSetSize = testClassSet.getNumberOfElements();
   testClass* testClassSetPtr = testClassSet.getItemSet();
   cout<<"The fourth set has "<<testClassSetSize<<" items\nThey are:\n";
   for(int i =0; i < testClassSetSize; i++)
   cout<<"\t"<<testClassSetPtr[i].data<<"\n";
   delete testClassSetPtr;
   cout<<"\n================================\n";
}

Add a comment
Know the answer?
Add Answer to:
what would be the solution code to this problem in c++? The Problem Write program that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • How to solve this Problem in C++ . The Problem Write program that uses a class...

    How to solve this Problem in C++ . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the set...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • In-Class Assignment 4 (15 pts) Structured Programming, if Statements 1. Write a program that prompts the...

    In-Class Assignment 4 (15 pts) Structured Programming, if Statements 1. Write a program that prompts the user to enter a value. Print a message stating whether the input value is less than zero or greater than zero or equal to zero. Submit a screenshot of output for each case. See below for sample output Microsoft Visual Studio Debug Console Microsoft Visual Studio Debug Console Microsoft Visual Studio Debug Console Enter a value 5 Enter a value you entered zero. Enter...

  • Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839....

    Write the following program in C++. Review structures, pointers and dynamic memory allocation from CSIT 839. Also, review pointers and dynamic memory allocation posted here under Pages. For sorting, you can refer to the textbook for Co Sci 839 or google "C++ sort functions". I've also included under files, a sample C++ source file named sort_binsearch.cpp which gives an example of both sorting and binary search. The Bubble sort is the simplest. For binary search too, you can refer to...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Can someone help me with these C program problems? Thanks 1. Write a C program that...

    Can someone help me with these C program problems? Thanks 1. Write a C program that creates two arrays of integers, one on the stack and one on the heap. You will loop through these arrays and populate them with values (any value is fine). You will then loop through these arrays again and print each value. Your output should look something like “Value in index 1 is 100 from stack array and 100 from heap array.” Do not forget...

  • Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in...

    Following the instruction This is c++ programming Lab Tasks: 1. Define a dynamic array class in DynamicArray .h and DynamicArray.cpp files, according to the following UML class diagram: DynamicArray - int arrySize; - int currentSize; int* arrayPtr; + DynamicArray(int size) // Explicit constructor, which you define- allocate space in dynamic memory for an integer array of the given size. + DynamicArray) // Explicit destructor, which you define-de allocate dynamic memory. + additem(int item): bool // Set the value of the...

  • CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification:...

    CS 241 Program 03 Due: Thursday, October 18th Main topics: Arrays& Pointers Memory allocation ram Specification: A stack is a container that can be defined in terms of an array where all adds are preformed at the end of the sequence of existing values, and all removes are also preformed at end of the sequence of existing values. An empty stack is one that has no existing values in the array at all. We use the notion of top of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT