#include<iostream>
using namespace std;
class Vector
{
public:
int x;
int y;
Vector(int x, int y) : x(x), y(y) {}
Vector operator+(const Vector &right)
{
Vector result(x + right.x, y + right.y);
return result;
}
};
std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
Str << "[" << v.x << "," << v.y << "]";
return Str;
}
int main()
{
Vector x(2, 3);
Vector y(4, 5);
cout << "Sum of vectors " << x << " and " << y << " is " << x + y << endl;
return 0;
}
Can someone please explain to me how this program works:
#include<iostream>
using namespace std;
class Vector
{
public:
int x;
int y;
Vector(int x, int y) : x(x), y(y) {}
Vector operator+(const Vector &right)
{
Vector result(x + right.x, y + right.y);
return result;
}
};
std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
Str << "[" << v.x << "," << v.y << "]";
return Str;
}
int main()
{
Vector x(2, 3);
Vector y(4, 5);
cout << "Sum of vectors " << x << " and " << y << " is " << x + y << endl;
return 0;
}
Its based on operator overloading-but my knowledge of C++ is a bit rusty as i haven't been using it for a long time.So any explanation would have been really helpful
Vector operator+(const Vector &right)
{
Vector result(x + right.x, y + right.y);
return result;
}
std:: ostream &operator<<(std:: ostream &Str, Vector const &v)
{
Str << "[" << v.x << "," << v.y << "]";
return Str;
}
It represents the RHS operand of that operator. There is no LHS because the current class is considered the LHS operand.Whats the purpose of using the const Vector &right as the parameter?
It's a bit confusing since you have named your vectors x and y. Suppose if you named themAre the variables x and y being initialized with 2 and 3 respectively,and then 4 and 5 are added to them later via (x + right.x, y + right.y)?
v1
and v2
, then v1 + v2
will be equals to [(v1.x + v2.x),(v1.y + v2.y)]. Therefore, in this case, suppose if v3 = v1 + v2
, then v3
will be (2+4, 3+5) = (6, 8)Here you are overloading the insertion operator. Here the operands are an output character stream and a Vector user defined type. Basically, this operator will format your Vector (right operand) in this format: [x,y] and insert it into the character stream variable in the left operand.Also i am having a hard time understanding the significance of this :
Can someone please explain to me how this program works:
~ coded snipped out ~
Its based on operator overloading-but my knowledge of C++ is a bit rusty as i haven't been using it for a long time.So any explanation would have been really helpful
Mod edit: Fixed code formatting using code blocks.
char '5'
int 53
5 | 05 | 005 | ^E | Enquiry (ENQ) |
Characters are just variants of integers, encoded in ASCII.is the same value asCode:char '5'
. The number 5 refers to the character ENQ ( Enquiry character - Wikipedia ).Code:int 53
In C++, you can write to files in text mode, or binary mode. When writing in text mode, all writing is done in characters.
^indeed,when i entered 65,it did write A to the file.
Why then is it writing some sort of unrecognizable character if i try to write an int variable containing the value 5?Is it writing the ASCII character that corresponds to the value of 5?
Then write a wrapper that efficiently handles the problem.there are other functions like fprintf() which can accomplish this more elegantly,but i am stuck with a project where the requirement is i must write an int value to a file using fputc () only-therein lies the problem.
i must write an int value to a file using fputc
int a = '0'+1;