Question

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


Instructions
    1. The code should contain the following
        -makefile - A simple makefile for this checkpoint
        -check11b.cpp - Contains a main function to test your Pair class. You should not need to change this function.
       -pair.h - This is where you will create your pair class. Please note that for template classes, you put the method bodies right in the .h file, rather than separating them out into a .cpp file.
    2. Create the Pair class. It should be a template class with two potentially different data types as the parameters.
    3. For the Pair class create the following methods:
        -getFirst() - returns the first item
        -setFirst(value) - sets the first item to the provided value.
        -getSecond() - returns the second item
        -setSecond(value) - sets the second item to the provided value
    4. Implement a display() function on the Pair class that displays the two parts of the object in the format "first - second"


Sample Output
The following is an example of output for this program:
Please enter a first name: Eliza
Please enter a last name: Snow
The first name is: Eliza
The last name is: Snow
The complete pair is: Eliza - Snow

Please enter a number: 42
Please enter a another number: 314
The first number is: 42
The second number is: 314
The complete pair is: 42 - 314

Please enter a name: James
Please enter a score: 97
The name is: James
The score is: 97
The complete pair is: James - 97

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

/*C++ program that prompts user to enter two names, two integer type values and one name and one integer values. Print the results on console window. */
//main.cpp
#include<iostream>
#include<string>
//include Pair.h header file
#include "Pair.h"
using namespace std;
// main Function
int main()
{
   //declare two string type variables
   string first_str;
   string second_str;
   // instantiation with two strings

   cout<<"Please enter a first name: ";
   cin>>first_str;

   cout<<"Please enter a last name: ";
   cin>>second_str;
  
   Pair <string, string> p1(first_str,second_str);
   cout<<"The first name is:"<<p1.getFirst()<<endl;
   cout<<"The last name is: "<<p1.getSecond()<<endl;


   cout<<"The complete pair is:";
   p1.display();

   fflush(stdin);

   //declare two integer type variables
   int first_int;
   int second_int;
  
   cout<<"Please enter a number: ";
   cin>>first_int;

   cout<<"Please enter another number: ";
   cin>>second_int;

   Pair <int, int> p2(first_int,second_int);

   cout<<"The first number is: :"<<p2.getFirst()<<endl;
   cout<<"The second number is: "<<p2.getSecond()<<endl;
   cout<<"The complete pair is:";
   p2.display();

   fflush(stdin);

   //declare one string and one integer type variables
   string first_name;
   int second_value;
  
   cout<<"Please enter a name: ";
   cin>>first_name;

   cout<<"Please enter score: ";
   cin>>second_value;
   // instantiation with two strings
   Pair <string, int> p3(first_name,second_value);
   cout<<"The name is: :"<<p3.getFirst()<<endl;
   cout<<"The score is: "<<p3.getSecond()<<endl;
   cout<<"The complete pair is:";
   p3.display();

   system("pause");
   return 0;
}

------------------------------------------------------------------------------------------------------------------------------
/*
The Pair template class that contains the constructor
that takes two generic data types in a class template.
*/
//Pair.h
#include<iostream>
using namespace std;
// Class template with two parameters
template<class M, class N>
class Pair
{
   //generic types
private:
   M first;
   N second;

public:
   /*Generic constructor
   that takes two generic arguments*/
   Pair(M x, N y)
   {
       first = x;
       second = y;
   }
   //Set method
   void setFirst(M x)
   {
       first = x;
   }
   //Get method
   M getFirst()
   {
       return first;
   }
   void setSecond(N y)
   {
       second = y;
   }
   N getSecond()
   {
       return second;
   }
   //Function,display that prints the first and second
   //generic data type values on console window
   void display()
   {
       cout << first << "- " <<second << endl;
   }
};

------------------------------------------------------------------------------------------------------------------------------

Sample Output:

Add a comment
Know the answer?
Add Answer to:
This checkpoint is intended to help you practice the syntax of class templates in C++. It...
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
  • Convert the OrderedPair class, which is provided below, into a templated class. Note that it will...

    Convert the OrderedPair class, which is provided below, into a templated class. Note that it will only work with types that have the operators + and < and << overloaded. But you should be able to try your templated class out with types string, MyString, double, FeetInches, Fraction, etc. I would encourage you to try these out. Also, create a programmer-defined exception class named "DuplicateMemberError" and add an if statement to each of the two mutators to throw this exception...

  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

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

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • help with C++ code The aim of this is to practice writing template classes. You must...

    help with C++ code The aim of this is to practice writing template classes. You must use SafeArray template class and implement a SafeMatrix template class that will allow you to work with two dimensional arrays of any type. The boundaries are checked. You must be able to create instances of SafeMatrix template class like, SafeMatrix<int> m(2,3); The above statement will create a 2 by 3 dimensional array of integers. You must be able to access and set values using...

  • In this practical task, you need to implement a class called MyTime, which models a time...

    In this practical task, you need to implement a class called MyTime, which models a time instance. The class must contain three private instance variables: hour, with the domain of values between 0 to 23. minute, with the domain of values between 0 to 59. second, with the domain of values between 0 to 59. For the three variables you are required to perform input validation. The class must provide the following public methods to a user: MyTime() Constructor. Initializes...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • I Need Help with this using Java Programming : Class name fname lname Class variable total...

    I Need Help with this using Java Programming : Class name fname lname Class variable total Number Constructor (no arguments) Constructor (takes two arguments, fname and lname) One getter method to return the fname and lname One setter method for fname and lname Inside Main: Create three objects with the following names Jill Doe John James Jack Smith When creating the first object (Should not be an anonymous object) use the argument-less constructor Then use the setter method to assign...

  • Design a restaurant class to help manage multiple restaurants and their menus equiremen The class...

    program is C# Design a restaurant class to help manage multiple restaurants and their menus equiremen The class contains: 1) A static int variable named storeCounter that starts at 1 and increments for each new restaurant item that is created 2) A string array called Menu 3) An int variable called StoreID 4) A Boolean variable called isOpen 5) A no-argument constructor that performs the following a. Sets storelD to storeCounter, then increments storeCounter b. Sets isOpen to true c....

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