Question

My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...

My assignment:

Triplet Template Class Directions:

Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:

 default constructor

 explicit constructor: initialize the data member using parameters

 three accessors (three get functions) which will return the value of each individual element of the array data member

 one mutator (set function) which will assign values to the data member using paramenters

 find_max function which will find the maximum value of the array and return it

 find_min function which will find the minimum value of the array and return it

 sort_ascending function which will sort the array data member into ascending order

 sort_descending function which will sort the array data member into descending order

 an overloaded comparison operator == which will compare if two triplets are exactly the same

 an overloaded output operator to output items in the triplet.

Write a driver to test your class. It should test each function for

o a triplet of strings

o a triplet of integers

For credit: Show your code and execute the test program.

Here is what I have so far:

Header.h

#include <iostream>
#include <string>
using namespace std;
template <class T>
class triplet
{
public:
   T A, B, C;
   triplet()
       //Purpose: default constructor
       //Precondition: array
       //Postcondition: array is declared as empty
   {
       T a = 0;
       T b = 0;
       T c = 0;
   };
   triplet(T a, T b, T c)
   //Purpose: explicit constructor
   //Precondition: array
   //Postcondition: initalizes the data member with parameters
   {
       A = a;
       B = b;
       C = c;
   };
   void get_A()
   //Purpose: returns A value
   //Precondition: A value
   //Postcondition: returns A to array
   {
       return A;
   };
   void get_B()
   //Purpose: returns B value
   //Precondition: B value
   //Postcondition: returns B to array
   {
       return B;
   };
   void get_C()
   //Purpose: returns C value
   //Precondition: C value
   //Postcondition: returns C to array
   {
       return C;
   };
   void set(T a, T b, T c)
   //Purpose: assigns values to the data member
   //Precondition: data members
   //Postcondition: values are assigned to the data member
   {
       A = a;
       B = b;
       C = c;
   };
   T find_max()
   //Purpose: finds maximum number
   //Precondition: filled array
   //Postcondition: returns max number
   {
       return max(A, max(B, C));
   };
   T find_min()
   //Purpose: finds minimum number
   //Precondition: filled array
   //Postcondition: returns min number
   {
       return min(A, min(B, C));
   };
   void sort_ascending()
   //Purpose: sorts the array in ascending order
   //Precondition: filled array
   //Postcondition: returns array in ascending order
   {
       for (i = 0; i <= n; i++)
       {
           for (j = 0; j <= n - i; j++)
           {
               if (a[j]>a[j + 1])
               {
                   temp = Arr[j];
                   Arr[j] = Arr[j + 1];
                   Arr[j + 1] = temp;
               }
           }
       }
       cout << "\nData after sorting: ";
       for (j = 0; j<n; j++)
       {
           cout << Arr[j];
       }
   };
   void sort_descending()
   //Purpose: sorts array in descending order
   //Precondition: filled array
   //Postcondition: returns array in descending order
   {
       for (i = 0; i <= n; i++)
       {
           for (j = 0; j <= n - i; j++)
           {
               if (a[j]<a[j + 1])
               {
                   temp = Arr[j];
                   Arr[j] = Arr[j + 1];
                   Arr[j + 1] = temp;
               }
           }
       }
       cout << "\nData after sorting: ";
       for (j = 0; j<n; j++)
       {
           cout << Arr[j];
       }
   };
   bool operator==(const triplet&t)
   //Purpose: compares two triplets to see if they're the same
   //Precondition: filled array
   //Postcondition: returns true or false
   {
       if (t.a == this->a&&t.b == this->b&&t.c == this->c)
           return true;
       return false;
   };
   ostream&operator<<(ostream&out, const triplet<T>& list)
   //Purpose: overloaded output operator
   //Precondition: filled array
   //Postcondition: outputs items in the triplet
   {
       output << "A B C are: " << list.A << " " << list.B << " " << list.C << endl;
       return output;
   }
};

Main.cpp

#include "Header.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
   triplet<int> one(12, 9, 16);
   cout << one << endl;
   cout << "Maximum is: " << one.find_max() << endl;
   cout << "Minimum is: " << one.find_min() << endl;
   triplet<string> two(hello, hi, bye);
   cout << two << endl;
   return 0;
}

Am I on the right path?

Homework Answers

Answer #1

yes your almost done

check my modification sbelow

#include <iostream>

#include<conio>

#include <string>
using namespace std;
template <class T>
class triplet
{
public:
   T A, B, C;
   triplet()
       //Purpose: default constructor
       //Precondition: array
       //Postcondition: array is declared as empty
   {
       T a = 0;
       T b = 0;
       T c = 0;
   };
   triplet(T a, T b, T c)
   //Purpose: explicit constructor
   //Precondition: array
   //Postcondition: initalizes the data member with parameters
   {
       A = a;
       B = b;
       C = c;
   };
   void get_A()
   //Purpose: returns A value
   //Precondition: A value
   //Postcondition: returns A to array
   {
       return A;
   };
   void get_B()
   //Purpose: returns B value
   //Precondition: B value
   //Postcondition: returns B to array
   {
       return B;
   };
   void get_C()
   //Purpose: returns C value
   //Precondition: C value
   //Postcondition: returns C to array
   {
       return C;
   };
   void set(T a, T b, T c)
   //Purpose: assigns values to the data member
   //Precondition: data members
   //Postcondition: values are assigned to the data member
   {
       A = a;
       B = b;
       C = c;
   };
   T find_max()
   //Purpose: finds maximum number
   //Precondition: filled array
   //Postcondition: returns max number
   {
       return max(A, max(B, C));
   };
   T find_min()
   //Purpose: finds minimum number
   //Precondition: filled array
   //Postcondition: returns min number
   {
       return min(A, min(B, C));
   };
   void sort_ascending()
   //Purpose: sorts the array in ascending order
   //Precondition: filled array
   //Postcondition: returns array in ascending order
   {
       for (i = 0; i <= n; i++)
       {
           for (j = 0; j <= n - i; j++)
           {
               if (a[j]>a[j + 1])
               {
                   temp = Arr[j];
                   Arr[j] = Arr[j + 1];
                   Arr[j + 1] = temp;
               }
           }
       }
       cout << "\nData after sorting: ";
       for (j = 0; j<n; j++)
       {
           cout << Arr[j];
       }
   };
   void sort_descending()
   //Purpose: sorts array in descending order
   //Precondition: filled array
   //Postcondition: returns array in descending order
   {
       for (i = 0; i <= n; i++)
       {
           for (j = 0; j <= n - i; j++)
           {
               if (a[j]<a[j + 1])
               {
                   temp = Arr[j];
                   Arr[j] = Arr[j + 1];
                   Arr[j + 1] = temp;
               }
           }
       }
       cout << "\nData after sorting: ";
       for (j = 0; j<n; j++)
       {
           cout << Arr[j];
       }
   };
   bool operator==(const triplet&t)
   //Purpose: compares two triplets to see if they're the same
   //Precondition: filled array
   //Postcondition: returns true or false
   {
       if (t.a == this->a&&t.b == this->b&&t.c == this->c)
           return true;
       return false;
   };
   ostream&operator<<(ostream&out, const triplet<T>& list)
   //Purpose: overloaded output operator
   //Precondition: filled array
   //Postcondition: outputs items in the triplet
   {
       output << "A B C are: " << list.A << " " << list.B << " " << list.C << endl;
       return output;
   }
};

Main.cpp

#include "Header.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
   triplet<int> one(12, 9, 16);
   cout << one << endl;
   cout << "Maximum is: " << one.find_max() << endl;
   cout << "Minimum is: " << one.find_min() << endl;
   triplet<string> two(hello, hi, bye);
   cout << two << endl;
   return 0;
}

Know the answer?
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
Similar Questions
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity  ...
I'm having a warning in my visual studio 2019, Can anyone please solve this warning. Severity   Code   Description   Project   File   Line   Suppression State Warning   C6385   Reading invalid data from 'DynamicStack': the readable size is '(unsigned int)*28+4' bytes, but '56' bytes may be read.   Here is the C++ code were I'm having the warning. // Sstack.cpp #include "SStack.h" // Constructor SStack::SStack(int cap) : Capacity(cap), used(0) {    DynamicStack = new string[Capacity]; } // Copy Constructor SStack::SStack(const SStack& s) : Capacity(s.Capacity), used(s.used)...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik //...
It is N queens problem please complete it use this code //*************************************************************** // D.S. Malik // // This class specifies the functions to solve the n-queens // puzzle. //*************************************************************** class nQueensPuzzle { public: nQueensPuzzle(int queens = 8);     //constructor     //Postcondition: noOfSolutions = 0; noOfQueens = queens;     // queensInRow is a pointer to the array     // that store the n-tuple.     // If no value is specified for the parameter queens,     // the default value, which is 8, is assigned to it. bool...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it...
I'm trying to figure out why my program has an infinite loop. I'm pretty sure it has something to do with the catch(bad_alloc) function or maybe the while (true) loop above it but I'm not sure. Can you help me figure out why i have an infinite loop and help me fix it? Thanks ---------------------------------- main.cc ---------------------------- #include #include #include #include "numbers.h" using namespace std; int main() { Numbers N1, N2;       for(size_t i = 2; i < 16;...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
we defined, implemented, and tested a class Time. In this lab, we will continue working with...
we defined, implemented, and tested a class Time. In this lab, we will continue working with the Time class. A. Separate the class definition and class implementation from the client program. Move the class definition into a header file named Time.h and move the class implementation into a cpp file named Time.cpp. Use preprocessor directive to include header files as needed. B. Modify the Time class as follows: 1. Replace the default constructor and overloaded constructor with a constructor with...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits...
Need to get the following output by Editing ChekingAccount.h ,ChekingAccount.cpp CheckingAccount Derived class CheckingAccount that inherits from base class Account and include an additional data member of type double that represents the fee charged per transaction (transactionFee). Write Checking- Account’s constructor that receives the initial balance, as well as a parameter indicating a transaction fee amount. If transaction fee is less than zero, the transactionFee will be set to zero. Write the chargeFee member function that updates the balance by...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write...
Create a C++ project. Download and add the attached .h and .cpp to the project. Write an implementation file to implement the namespace declared in the attached CSCI361Proj5.h. Name the implementation file as YourNameProj5.cpp and add it to the project. Run the project to see your grade. .h file: // Provided by: ____________(your name here)__________ // Email Address: ____________(your email address here)________ // FILE: link.h // PROVIDES: A toolkit of 14 functions for manipulating linked lists. Each // node of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT