Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am unsure if my answer to my Assignment is correct. A simple response will suf

ID: 3821400 • Letter: I

Question

I am unsure if my answer to my Assignment is correct. A simple response will suffice.

Write the code for an operator<< function to add to the Triangle class (this class is provided in a document inside this Quiz folder). You must write the entire function as it would be written in the implementation file, so it must include a heading and a body. The operator<< should display the triangle use the following format: [base, height].

Now most of my examples from my courses would have had a seperate getBase(); and getHeight(); functions, but this one has the members in the same function of getTriangle();. I am unsure how to properly break up the members from inside the function to display as the question states.

This is what I have as an answer thus far:
ostream& operator<<(ostream& cout, Triangle right)

{

cout << "[" << right.getTriangle(b) << "," << right.getTriangle(h) << "]";

}

Any help would be greatly appreciated!

Explanation / Answer

You can directly call b and h on it (base and height) rather than using getTriangle(b) as it is a friend function.

Also am not sure if getTriangle can fetch you both base and height with the code you have provided.

Here is the function along with an example code to demonstrate.

#include <iostream>
using namespace std;

class Triangle {
private:
int b;
int h;
public:
// required constructors
Triangle(){
b = 0;
h = 0;
}
Triangle(int f, int i){
b = f;
h = i;
}
  
friend ostream& operator<<( ostream &cout, Triangle right );
};

ostream& operator<<( ostream &cout, Triangle right ) {
cout << "[" << right.b << ", " << right.h << "]";
return cout;
}


int main() {
Triangle T(11, 10);
cout << "Triangle : " << T << endl;


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote