In: Computer Science
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;
}
#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.