Question

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

Sample Output: Microsoft Visual Studio Debug Console -Example 1: Set of integers--- Adding: 10 Adding: 5 Adding: 15 Adding: 2

Other assignment Requirements: 1. You have to use classes and functions in your solution (encapsulation). 2. This assignment

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

Comment for any problem or questions if you have and Thumbs Up!

#pragma once
#include<string>
#include<cmath>
#include<iostream>
using namespace std;

template<class T>
class Set
{
   private:
       T* set;
       int size;
       int current;
      
   public:
       Set();
       Set(int s);

       void setSize(int s);

       bool insert(T value);

       int getCurrent();

       void displaySet();

       ~Set();


};

//cpp

#include "Set.h"

template<class T>
Set<T>::Set()
{
   size = 4;
   set = new T[size];
   current = 0;


}

template<class T>

Set<T>::Set(int s)
{
   current = 0;
   size = s;
   set = new T[size];

}

template<class T>
void Set<T>::setSize(int s)
{
   size = s;
}

template<class T>
bool Set<T>::insert(T value)
{
   for (int i = 0; i < current; i++)
   {
       if (set[i] == value)
       {
           cout << "This value already exist in set" << endl;
           return false;
       }

   }

   set[current] = value;
   current++;


}


template<class T>
int Set<T>::getCurrent()
{
   return current;
}

template<class T>
void Set<T>::displaySet()
{
   cout << "Values of Set are" << endl;
   for (int i = 0; i < current; i++)
   {
       cout << "Element#" << i + 1 << "=" << set[i] << endl;
   }

}

template<class T>

Set<T>::~Set()
{
   delete[] Set;
}

//Main Cpp

#include"Set.h"
#include<string>
#include<iostream>
using namespace std;

int main()
{
   int value;
   int countSet = 1;
   Set<int> obj1(5);
   for (int i = 0; i < 5; i++)
   {
       cout << "Enter the value of Set#"<<countSet << endl;
       cin >> value;
       obj1.insert(value);
   }

   obj1.displaySet();
}

Comment for any problem or questions if you have and Thumbs Up!

Add a comment
Know the answer?
Add Answer to:
Please write below code in C++ using Visual Studio. Write program that uses a class template...
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
  • 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...

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

  • using visual studio 2) For each of the following, write C++ statements that perform the specified...

    using visual studio 2) For each of the following, write C++ statements that perform the specified task.. (Ref: classwork named pointer2) a) Declare a built-in array of type unsigned int called values with five elements, and initialize the elements to the even integers from 2 to 10. b) Declare a pointer vPtr that points to a variable of type unsigned int. c) Write two separate statements that assign the starting address of array values to pointer variable vPtr. d) Use...

  • C++ visual studio program...Write your own version of a class template that will create a dynamic...

    C++ visual studio program...Write your own version of a class template that will create a dynamic stack of any data type. The pop function must return a bool; it should return a false if it was not able to pop an item off the stack. Otherwise it returns true. The parameter to the pop function is passed by reference and should be the item on the list if it was able to pop something. Create a driver program (main) that...

  • Exercise 2 Using javascript (visual studio code) (a) Write code that creates 5 item objects (records)...

    Exercise 2 Using javascript (visual studio code) (a) Write code that creates 5 item objects (records) (b) Place the five item objects into an array (c) Write a function that prints an array of your items it has one parameter an array of items (d) Write a function that counts items that match some attribute value. (e) Write a function that filters an array of items. It has three parameter an array of item, an attribute name and a value....

  • Write the C program, to achieve as shown in the sample code execution, using the given...

    Write the C program, to achieve as shown in the sample code execution, using the given struct and using the comments in the given main program below: typedef struct{ char first[20]; char last[20]; float gpa; int score; } student; int main(void){ student *ptr; //first name //last name //student gpa //student score } //ask a user to enter the number of students, num //dynamically allocate memory for an array of students of the appropriate size (i.e. //num that the user just...

  • Write the c++ code that will run with visual studio With the square function below as...

    Write the c++ code that will run with visual studio With the square function below as an example, write a max function that has two int parameters and will return back the larger of the two. Also write a main which can be used to test your function. ex ) int square(intnum); // funcprototype int main() {    inta = 5, b; a = square(a);    b = square(4);cout<< square( 6 ) << endl;    ....        // func...

  • ***PLEASE AVOID USING A "CLASS" IN SOLUTION*** thank you. Please use Visual Studio to write a...

    ***PLEASE AVOID USING A "CLASS" IN SOLUTION*** thank you. Please use Visual Studio to write a C# program to allow user to store contact information. 1. User should be able to type in name, E-mail and phone number in the text boxes and click Add button to save the contact record. Every time when user add record, user should be able to see all the information displayed in the right side display text box. (allow up to 10 records) 2....

  • Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code...

    Using Microsoft Visual Studio. 1) Complete the following C++ program by adding more line of code for 8-bit signed array, 16-bit unsigned array, 16-bit signed array, 32-bit signed array and 32-bit signed array. 2) Fill in all the blanks in Table 1 using your completed code, following the hints provided within the table. 3) Fill in all the blanks in Table 2 using your completed code, following the hints provided within the table. C++ Program #include <stdio.h> #include <iostream> int...

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