Question

c++ please Create a my pair class class that uses an array to store pairs of...

c++ please

Create a my pair class

class that uses an array to store pairs of elements. The data members of the class are first and second. Use a template to allow the class to store pairs of any type so that a pair may be (int, int) or (int, double) or (int string), or any other combination you can think of. Create a member function to display the pair in the format "value, value" with a comma separating them. Also create getters and setters for the first and second data members.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::::

#include<iostream>
#include<string>
using namespace std;
template<class T, class P>
class MyPair
{
private:
   T data1;
   P data2;
public:
   MyPair(T r,P c)
   {
       data1 = r;
       data2 = c;
   }

   void setDataOne(T r){
       data1 = r;
   }

   void setDataTwo(P r){
       data2 = r;
   }


   T getDataOne(T r){
       return data1;
   }

   T getDataTwo(T r){
       return data1;
   }

   void displayPair(){
       cout<<data1<<","<<data2<<endl;
   }
};
int main()
{
   MyPair<int,string> pair(12,"hello");
   pair.displayPair();
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
c++ please Create a my pair class class that uses an array to store pairs of...
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
  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Using C#, Create a date class with integer data members for year, month, and day. Also...

    Using C#, Create a date class with integer data members for year, month, and day. Also include a string data member for the name of the month. Include a method that returns the month name as string as part of the date. Separate the day from the year with comma in the method. Include appropriate constructor, properties, and methods. Override the ToString() method to display the date formatted with slashes(/) separating the month, day, and year. Create a second class...

  • This checkpoint is intended to help you practice the syntax of class templates in C++. It...

    This checkpoint is intended to help you practice the syntax of class templates in C++. It is often convenient to have a pair of items stored together (e.g., you want to have a combination of a string and an integer as the key or value of a map). For this exercise, you will create a Pair class, that is a template class allowing any two data types to be the data type of the first and second items in the...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the...

    C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the following attributes: Data members (private): string: username int array: ratings Number of elements should be size int: numRatings Number of books in the database Int: size The capacity of the ratings array (50). Constant Member functions (public): Default constructor Sets username to an empty string, numRatings to 0, size to 50, and all the elements of ratings array to the value 0 Parameterized constructor...

  • Please do this in C++. Thank you in advance. Note that you have to carefully create...

    Please do this in C++. Thank you in advance. Note that you have to carefully create your class using the names and capitalization shown below. Put comments into your program, make sure you indent your program properly, and use good naming conventions. Create a class called entry. It should have two private data members of type std::string. One represents a name and the other represents an email address. Make sure you give them ,meaningful names. Create public getters and setters...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • IN C++ Create a class to act as a generic array (i.e. the user will be...

    IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

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