Question

In: Computer Science

Complete the C++ class Triple below so that it represents a vector with 3 elements: (a,...

Complete the C++ class Triple below so that it represents a vector with 3 elements: (a, b, c)

Most of these function bodies can be written in only a few lines. Error checking is not required.

Test your class using the test.h file. The triple.cpp file is the code unit tested on submission.

main.cpp

#include
#include
#include

#include "triple.h"

#include "test.h"

using namespace std;

int main() {

myTest();

return 0;
}


triple.h

#ifndef TRIPLE_H
#define TRIPLE_H

#include
#include

using namespace std;

class Triple {
private:
   int a, b, c;
public:
   Triple();               // all elements have value 0
   Triple(int k);               // all elements have value k
   Triple(int x, int y, int z);       // specifies all three elements
   Triple(string s);            // string representation is "(a,b,c)"
   string toString(); // create a string representation of the vector
   void fromString(string s); // change the vector to equal the string representation
   int fetch(int index);           // return the chosen element
void assign(int index, int k);   // update chosen element to value k
   int maxElement();            // return the largest element value
   int sum();                 // return sum of the element values
   int product();                // return product of the element values
   Triple operator+(Triple t) const;   // add the vectors
   void operator+=(Triple t);
   Triple operator*(int k) const;       // multiply vector elements by int value
   void operator*=(int k);
   Triple operator*(Triple t) const;   // vector cross product
   void operator*=(Triple t);
   int dotProduct(Triple t);       // vector dot product
   Triple reverse() const;        // reverse the order of the vector elements
   void reverseInPlace();
   bool operator==(Triple t);       // do both vectors contain the same values?
   bool operator!=(Triple t);
   friend ostream& operator<<(ostream& os, Triple t);
   friend istream& operator>>(istream& is, Triple& t);
};

#endif

triple.cpp

#include
#include
#include

#include "triple.h"

using namespace std;

// all elements have value 0
Triple::Triple() {
}

// all elements have value k
Triple::Triple(int k) {
}

// specifies all three elements
Triple::Triple(int x, int y, int z) {
}

// string representation is "(a,b,c)"
// write fromString() first and use it
Triple::Triple(string s) {
}

// create a string representation of the vector
// use ostringstream
string Triple::toString() {

return "";
}

// change the vector to equal the string representation
// use istringstream
void Triple::fromString(string s) {
}

// return the chosen element
// index 1 is a, 2 is b, 3 is c
int Triple::fetch(int index) {
return 0;
}

// update chosen element to value k
void Triple::assign(int index, int k) {

}

// return the largest element value
int Triple::maxElement() {
return 0;
}

// return sum of the element values
int Triple::sum() {

return 0;
}

// return product of the element values
int Triple::product() {

return 0;
}

// add the vectors
Triple Triple::operator+(Triple t) const {

return Triple();
}

void Triple::operator+=(Triple t) {
}

// multiply vector elements by int value
Triple Triple::operator*(int k) const {

return Triple();
}

void Triple::operator*=(int k) {
}

// vector cross product
Triple Triple::operator*(Triple t) const {

return Triple();
}

void Triple::operator*=(Triple t) {
}

// vector dot product
int Triple::dotProduct(Triple t) {

return 0;
}

// reverse the order of the vector elements and return it
Triple Triple::reverse() const {

return Triple();
}

// reverse the order of the vector elements
void Triple::reverseInPlace() {
}

// do both vectors contain the same values?
bool Triple::operator==(Triple t) {

return false;
}

bool Triple::operator!=(Triple t) {

return false;
}

// write the string representation of the vector to the stream
// the stream representation is the same as the string
// use toString()
ostream& operator<<(ostream& os, Triple t) {

return os;
}

// create a vector by reading from the stream
// the stream representation is the same as the string
// use fromString()
istream& operator>>(istream& is, Triple& t) {

return is;
}

Solutions

Expert Solution

#include<vector>
#include<iostream>
#include<sstream>

#include "triple.h"

using namespace std;

// all elements have value 0
Triple::Triple() {
        a = 0;
        b = 0;
        c = 0;
}

// all elements have value k
Triple::Triple(int k) {
        a = k;
        b = k;
        c = k;
}

// specifies all three elements
Triple::Triple(int x, int y, int z) {
        a = x;
        b = y;
        c = z;
}

// string representation is "(a,b,c)"
// write fromString() first and use it
Triple::Triple(string s) {
        fromString(s);
}

// create a string representation of the vector
// use ostringstream
string Triple::toString() {
        stringstream ss;
        ss << "(" << a << "," << b << "," << c << ")";
        return ss.str();
}

// change the vector to equal the string representation
// use istringstream
void Triple::fromString(string s) {
        s = s.substr(1, s.length()-1); // remove paranthesis on both ends.
        
        stringstream ss(s);
        ss >> a;
        if (ss.peek() == ',')
                ss.ignore();
        ss >> b;
        if (ss.peek() == ',')
                ss.ignore();
        ss >> c;
}

// return the chosen element
// index 1 is a, 2 is b, 3 is c
int Triple::fetch(int index) {
        switch(index) {
                case 1: return a;
                case 2: return b;
                case 3: return c;
        }
        return 0;
}

// update chosen element to value k
void Triple::assign(int index, int k) {
        
        switch(index) {
                case 1: a = k; break;
                case 2: b = k; break;
                case 3: c = k; break;
        }
}

// return the largest element value
int Triple::maxElement() {
        return (a > b) ? (a > c ? a : c) : (b > c ? b : c);
}

// return sum of the element values
int Triple::sum() {
        return a + b + c;
}

// return product of the element values
int Triple::product() {
        return a * b * c;
}

// add the vectors
Triple Triple::operator+(Triple t) const {
        return Triple(a + t.a, b + t.b, c + t.c);
}

void Triple::operator+=(Triple t) {
        a += t.a;
        b += t.b;
        c += t.c;
}

// multiply vector elements by int value
Triple Triple::operator*(int k) const {
        return Triple(a * k, b * k, c * k);
}

void Triple::operator*=(int k) {
        a *= k;
        b *= k;
        c *= k;
}

// vector cross product
Triple Triple::operator*(Triple t) const {
        return Triple(b*t.c - c*t.c, c*t.a - a*t.a, a*t.b - b*t.b);
}

void Triple::operator*=(Triple t) {
        int a1 = b*t.c - c*t.c;
        int b1 = c*t.a - a*t.a;
        int c1 = a*t.b - b*t.b;
        a = a1;
        b = b1;
        c = c1;
}

// vector dot product
int Triple::dotProduct(Triple t) {
        return (a*t.a + b *t.b + c*t.c);
}

// reverse the order of the vector elements and return it
Triple Triple::reverse() const {
        return Triple(c, b, a);
}

// reverse the order of the vector elements
void Triple::reverseInPlace() {
        int t = a;
        a = c;
        c = t;
}

// do both vectors contain the same values?
bool Triple::operator==(Triple t) {
        return (a == t.a && b == t.b && c == t.c);
}

bool Triple::operator!=(Triple t) {
        return !(*this == t);
}

// write the string representation of the vector to the stream
// the stream representation is the same as the string
// use toString()
ostream& operator<<(ostream& os, Triple t) {
        os << t.toString();
        return os;
}

// create a vector by reading from the stream
// the stream representation is the same as the string
// use fromString()
istream& operator>>(istream& is, Triple& t) {
        is >> t.a >> t.b >> t.c;
        return is;
}
**************************************************
You have not given the test class, so i can not test the code.. so let me know if any issues.

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable...
Elevator (C++) Following the diagram shown below, create the class Elevator. An Elevator represents a moveable carriage that lifts passengers between floors. As an elevator operates, its sequence of operations are to open its doors, let off passengers, accept new passengers, handle a floor request, close its doors and move to another floor where this sequence repeats over and over while there are people onboard. A sample driver for this class is shown below. Each elevator request translates into just...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a...
(USE C ++ AND class STRING ONLY!!!! No java, No cstring and No vector) Write a program that can be used to train the user to use less sexist language by suggesting alternative versions of sentences given by the user. The program will ask for a sentence, read the sentence into a string variable, and replace all occurrences of masculine pronouns with genderneutral pronouns. For example, it will replace "he" with "she or he". Thus, the input sentence See an...
Complete the Vec class to make it similar to the vector. sample code here. #include #include...
Complete the Vec class to make it similar to the vector. sample code here. #include #include #include #include #include using namespace std; int get_mode( vector vec ) { int numbers[101] = {0}; for ( int e : vec ) numbers[e]++; int greatest = 0, n = 0; for ( int i = 0; i <= 100; i++ ) { if ( numbers[i] > greatest ) { greatest = numbers[i]; n = i; } } return n; } const double g...
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This...
THIS IS IMPLEMENTED IN C++ The SetInt class (Annex B1) represents a set of integers. This class contains a constructor without parameters which creates an empty set and a constructor which receives as parameters an array of integers as well as its size and which creates a set of integers containing the elements of this array. A test program is given in Annex B2. The class also contains a destructor and the copy constructor, as well as methods that have...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet...
Code in C++ Objectives Use STL vector to create a wrapper class. Create Class: Planet Planet will be a simple class consisting of three fields: name: string representing the name of a planet, such as “Mars” madeOf: string representing the main element of the planet alienPopulation: int representing if the number of aliens living on the planet Your Planet class should have the following methods: Planet(name, madeOf, alienPopulation) // Constructor getName(): string // Returns a planet’s name getMadeOf(): String //...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static...
USING JAVA: complete the method below in the BasicBioinformatics class. /** * Class BasicBioinformatics contains static methods for performing common DNA-based operations in * bioinformatics. * * */ public class BasicBioinformatics { /** * Calculates and returns the number of times each type of nucleotide occurs in a DNA sequence. * * @param dna a char array representing a DNA sequence of arbitrary length, containing only the * characters A, C, G and T * * @return an int array...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...
FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c: You should provide the following methods (1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three parameters public QuadraticExpression(double a, double b, double c) (3) a toString() method that returns the expression as a string. (4) evaluate method that returns the value of the expression at x public double evaluate(double x) (5) set method of a, b, c...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT