Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This is my original code, please revise it: put the member function inside the struct using the following methods # include # include using namespace
This is my original code, please revise it: put the member function inside the struct using the following methods
#include
#include
using namespace std;
struct Square
{
float side;
};
void outputSquare(ostream&, const Square&);
int main() {
Square a; // declare the object
a.side = 9.9; // assigning a value to an attribute
outputSquare(cout, a);
}
void outputSquare(ostream& out, const Square& x)
{
out
out
out
cout.unsetf(ios::fixed);
cout.precision(6);
}
The "C language" struct simply "encapsulated" attributes, for the programming simplifications that offered. struct-based objects were not even called "objects" in C -- they were 'struct variables", or "instances of a struct". C++ extends C by adding object oriented-ness syntax and terminology and thinking. A key part of that is how functions work -- functions that we've called "supporting functions". Rather than simply recognizing the relationship between a struct and its supporting functions by coding the function prototypes right under the struct definition, the relationship is more formal than that. The big difference is that the prototypes are actually moved inside the struct definition instead of just outside and below it. (And if you were expecting the class keyword, don't worry -- we'll get to it later). To compare and contrast, consider these code blocks that show the familiar supporting function alongside what we'll call the "member function" (because it's now a "member of the struct ): // with supporting function(s) struct Student { string name; int ID; float gpa; char gender; // with member function(s) struct Student { string name; int ID; float gpa; char gender; void outputStudent(ostream&) const; }; void outputStudentCostream&, const Student&); New syntax is required in order to support this way of formalizing the relationship between the object and its functions. Again comparing and contrasting: //using supporting function(s) Student x; //using member function(s) Student x; outputStudent (cout, x); x.outputStudent (cout)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started