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;...
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...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h"...
- implement the Stack ADT using the linked list approach. Use C++ program language #include "StackLinked.h" template StackLinked::StackLinked (int maxNumber) { } template StackLinked::StackLinked(const StackLinked& other) { } template StackLinked& StackLinked::operator=(const StackLinked& other) { } template StackLinked::~StackLinked() {    clear(); } template void StackLinked::push(const DataType& newDataItem) throw (logic_error) {    } template DataType StackLinked::pop() throw (logic_error) { } template void StackLinked::clear() {    StackNode* t;    while ( top != NULL)    {        t = top;       ...
In this assignment, you’ll make an inventory system for a store’s items, including produce and books....
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce. 1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item. Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars. Modify the AddItemToInventory...