Question

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 seSample Output: Microsoft Visual Studio Debug Console integers--- -Example 1: Set of Adding: 10 Adding: 5 Adding: 15 Adding: 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//set.h

#include<iostream>
using namespace std;

template<typename T>
class set{
   private:
       T *arr;
       int size;
      
   public:
       set(int capacity){
           arr = new T[capacity];
           size = 0;
       }
       ~set(){
           delete [] arr;
           size = 0;
       }
      
       void add(T data){
           cout<<" Adding : "<<data<<endl;
           int i;
           for(i=0;i<size;i++){
               if(arr[i] == data) break;
           }
           if(i==size)arr[size++] = data;
       }
      
       int getCount(){
           return size;
       }
      
       T* getArray(){
           return arr;
       }
};

//main.cpp

#include "set.h"

int main(){
   set<int> set1(10);
   set<double> set2(10);
   set<string> set3(10);
  
   cout<<"--------------------------------\n";
   cout<<"---Example 1: Set of integers---\n";
   cout<<"--------------------------------\n";
   set1.add(10);
   set1.add(5);
   set1.add(15);
   set1.add(25);
   set1.add(15);
   cout<<"--------------------------------\n\n";
   cout<<"The first set has "<<set1.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set1.getCount();i++){
       cout<<"\t"<<set1.getArray()[i]<<"\n";
   }
  
   cout<<"\n--------------------------------\n";
   cout<<"---Example 2: Set of doubles---\n";
   cout<<"--------------------------------\n";
   set2.add(1.5);
   set2.add(5.6);
   set2.add(12.8);
   set2.add(1.5);
   set2.add(12.8);
   cout<<"--------------------------------\n\n";
   cout<<"The second set has "<<set2.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set2.getCount();i++){
       cout<<"\t"<<set2.getArray()[i]<<"\n";
   }
  
   cout<<"\n--------------------------------\n";
   cout<<"---Example 3: Set of strings---\n";
   cout<<"--------------------------------\n";
   set3.add("John Smith");
   set3.add("Jane doe");
   set3.add("John Smith");
   set3.add("Jack black");
   cout<<"--------------------------------\n\n";
   cout<<"The third set has "<<set3.getCount()<<" items\nThe are: \n";
   for(int i=0;i<set3.getCount();i++){
       cout<<"\t"<<set3.getArray()[i]<<"\n";
   }
  
   return 0;
}

//sample output

--Example 1: Set of integers- Adding : 10 Adding : 5 Adding : 15 Adding : 25 Adding : 15 The first set has 4 items The are: 1

Add a comment
Know the answer?
Add Answer to:
Write a program in C++ that uses a class template to create a set of items....
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...

  • what would be the solution code to this problem in c++? The Problem Write program that...

    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 (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...

  • 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”....

  • 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...

  • In this project you will create a console C++ program that will have the user to...

    In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation. Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature...

  • 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...

  • Create the program in C++ please. The new operator as outlined in 10.9 . Create a...

    Create the program in C++ please. The new operator as outlined in 10.9 . Create a Test class that includes the following Data members • First Name, last name, test1, test2, test3 o Methods • Accessor methods for each of the data members' • Mutator methods for each of the data members . A Default Constructor . A constructor that will accept arguments for each of the 5 data members • A method that will calculate the average of the...

  • Write an object-oriented C++ program (i.e. a class and a main function to use it), using...

    Write an object-oriented C++ program (i.e. a class and a main function to use it), using pointer variables, that program that performs specific searching and sorting exercises of an array of integers. This program has six required outputs. Start by initializing an array with the following integers, in this order: 23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, and 49. Your array input may be hardcoded...

  • In c++ Write a program that contains a class called Player. This class should contain two...

    In c++ Write a program that contains a class called Player. This class should contain two member variables: name, score. Here are the specifications: You should write get/set methods for all member variables. You should write a default constructor initializes the member variables to appropriate default values. Create an instance of Player in main. You should set the values on the instance and then print them out on the console. In Main Declare a variable that can hold a dynamcially...

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