I am a beginner at C++ and I am trying to make a program that uses 2 points, adds them together to a line. Then adds a line together with a point, and gets a polygon.
I am trying, unsuccessfully, to overload the operator << so that I can print out my line:
#include <iostream>
using namespace std;
class Line {
private:
OnePoint onevalue;
OnePoint twovalue;
public:
Line(OnePoint a, OnePoint b) {
onevalue = a;
twovalue = b;
}
ostream& operator<<(ostream& print, Line& linje){ // Error right here.
print << linje.onevalue << ',' << linje.twovalue; // Too many
return print; // parameters for this type of function
}
};
class OnePoint {
private:
double xvalue;
double yvalue;
public:
OnePoint(double x = 0.0, double y = 0.0) {
xvalue = x;
yvalue = y;
}
friend ostream& operator<<(ostream& printh, OnePoint& cPoint) {
printh << "(" << cPoint.xvalue << ',' << cPoint.yvalue << ")";
return printh;
}
void Plus(OnePoint a) {
xvalue = xvalue + a.xvalue;
yvalue = yvalue + a.yvalue;
}
void Minus(OnePoint b) {
xvalue = xvalue + b.xvalue;
yvalue = yvalue + b.yvalue;
}
OnePoint Plustwo(OnePoint a) {
return (xvalue + a.xvalue, yvalue - a.yvalue);
}
void Change(double a, double b) {
xvalue += a;
yvalue += b;
}
void Print(OnePoint b) {
cout << xvalue << "," << yvalue << endl;
}
OnePoint operator+(OnePoint a) {
OnePoint temp;
temp.xvalue = xvalue + a.xvalue;
temp.yvalue = yvalue + a.yvalue;
return temp;
}
};
//--------------------------------------------------------------------
int main(){
OnePoint a(3.0, 3.0);
OnePoint b(1.0, 1.0);
OnePoint d(1.0, 4.0);
OnePoint c;
c = a + b + d;
c.Print(c);
}
Edit:
I can now cout my OnePoint, but I cannot cout Line.
As a member function, the operator gets an extra hidden parameter for the this pointer.
You can make it a free function by declaring it a friend of the class:
class Line {
// other members
public:
friend ostream& operator<<(ostream& print, Line& linje){
print << linje.onevalue << ',' << linje.twovalue;
return print;
}
};
Related
#include<iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
Here operator + is overloaded and it is accesing the private member of res class
Some more examples are
ex1 -
struct Edge {
int a,b,w;
};
bool operator<(const Edge& x, const Edge& y) { return x.w < y.w; }
ex2-
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int a,b,w;
bool operator<(const Edge& y) { return w < y.w; }
};
int main() {
int M = 4;
vector<Edge> v;
for (int i = 0; i < M; ++i) {
int a,b,w; cin >> a >> b >> w;
v.push_back({a,b,w});
}
sort(begin(v),end(v));
for (Edge e: v) cout << e.a << " " << e.b << " " << e.w << "\n";
}
ex1 and ex2 are from the usaco.guide and the first example was from the geeks for geeks
Can anyone explain how it works ?
How here private data members are accessed from the class instance. Can anyone explain how it works ?
First things first, in ex1 and ex2 you're using a struct and so by default every member is public. So any user of the class has access to its members.
Now, even if those data members were private, you've overloaded operator+ and operator< as member functions. And a member function has full access to any(private, public or protected) member of the corresponding class type.
I created a class named test having 2 integers. I am trying to overload operator+ for it. But the output is wrong and I can't understand why.
#include <conio.h>
#include <iostream>
using namespace std;
class test
{
int a, b;
public:
test()
{
a = 0;
b = 0;
}
test(int x, int y)
{
a = x;
b = y;
}
test operator+(test t);
void disp()
{
std::cout << "a is =" << a << "\n b is = " << b;
}
};
test test::operator+(test t)
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
}
int main()
{
test b1, b2, b3;
b1 = test(10, 20);
b2 = test(30, 40);
b3 = b1 + b2;
b3.disp();
}
You have undefined behavior because your operator+ is not returning temp. You need to do:
test test::operator+(test t)
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
return temp; // <- return here
}
Enable all your warnings with something like -Wall, and the compiler will tell you about mistakes like this.
Note that the canonical way of implementing operator+ as a member function is to take the argument by const&, and to make the member function const as well:
test test::operator+(test const &t) const
{
test temp;
temp.a = a + t.a;
temp.b = b + t.b;
return temp;
}
The task is in the above code to write member function that calculate new point, which is amount of two other points. And i dont know how to return object or what should i do. Here is the code, and the function is marked with three !!!. The function must return something, i cant make it void because reference to void is unallowed.
class point {
private:
float x;
float y;
public:
point();
point(float xcoord, float ycoord);
void print();
float dist(point p1, point p2);
!!! float &add(point p1, point p2);
float &X();
float &Y();
~point();
};
float & point::X() { return x; }
float & point::Y() { return y; }
point::point() {
cout << "Creating POINT (0,0)" << endl;
x = y = 0.0;
}
point::point(float xcoord, float ycoord) {
cout << "Creating POINt (" << xcoord << "," << ycoord << ")" << endl;
x = xcoord;
y = ycoord;
}
void point::print() {
cout << "POINT (" << x << "," << y << ")";
}
float point::dist(point p1, point p2) {
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
!!!// float & point::add(point p1, point p2) {
point z;
z.X() = p1.X() + p2.X();
z.Y() = p1.Y() + p2.Y();
z.print();
}
point::~point() {
cout << "Deleting ";
print();
cout << endl;
}
int main() {
point a(3, 4), b(10, 4);
cout << "Distance between"; a.print();
cout << " and "; b.print();
cout << " is " << a.dist(a, b) << endl;
}
i make it ! here is what must be add function
//prototype
point &add(point& p1, point& p2);
//function itself
point & point::add(point& p1, point& p2) {
point z;
z.x = p1.X() + p2.X();
z.y = p1.Y() + p2.Y();
z.print();
return z;
}
Many thanks to ForceBru!! and all of you
What to do
You can return a point as well:
point point::add(const point& p1, const point& p2) {
point result;
result.x = p1.x + p2.x;
result.y = p1.y + p2.y;
return result;
}
Note that there's no need to use X() and Y() functions here since this method already has access to the private members.
It's also possible to do an operator overload
/*friend*/ point operator+ (const point& one, const point& two) {
// the code is the same
}
How to use it
int main() {
point one(2,5), two(3,6), three;
three.add(one, two);
std::cout << "Added " << one.print() << " and " << two.print();
std::cout << " and got " << three.print() << std::endl;
return 0;
}
Edit: as it was said in the comments, you shouldn't return a reference to an object created inside your add function since in such a situation you're allowed to return references to class members and to static variables only.
You can use Operator overloading here:
point point::operator+(const point & obj) {
point obj3;
obj3.x = this->x + obj.x;
return obj3;
}
returning object with Addition of two points.
for simple example :
class Addition {
int a;
public:
void SetValue(int x);
int GetValue();
Addition operator+(const Addition & obj1);
};
void Addition::SetValue(int x)
{
a = x;
}
int Addition::GetValue()
{
return a;
}
Addition Addition::operator+(const Addition &obj1)
{
Addition obj3;
obj3.a = this->a + obj1.a;
return obj3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Addition obj1;
int Temp;
std::cout<<"Enter Value for First Object : "<<std::endl;
std::cin>>Temp;
obj1.SetValue(Temp);
Addition obj2;
std::cout<<"Enter Value for Second Object : "<<std::endl;
std::cin>>Temp;
obj2.SetValue(Temp);
Addition obj3;
obj3 = obj1 + obj2;
std::cout<<"Addition of point is "<<obj3.GetValue()<<std::endl;
return 0;
}
I have made class which represents Line Linia (aX+bY=c) and I overloaded + operator so now it now returns new Linia object which has c = c + b/argument. But the problem is that when I use this operator all the fields of the given Line object become 0
#include <iostream>
using namespace std;
struct Q{
public:
double x,y;
Q(double x, double y){
this->x = x;
this->y = y;
}
friend ostream& operator<< (ostream &wyjscie, Q const& ex){
wyjscie<<"("<<ex.x<<","<<ex.y<<")";
return wyjscie;
}
};
class Linia{
public:
double a,b,c;
Linia (double a, double b, double c){
this->a = a;
this->b = b;
this->c = c;
}
Linia operator+ (double i){
return Linia(a, b, c + i/b);
}
Linia operator- (double i){
return Linia(a, b, c - i/b);
}
Q operator* (const Linia& i){
double w = a*i.b - b*i.a;
double wx = -c*i.b + i.c*b;
double wy = a*(-i.c) + i.c*c;
double x = wx/w, y = wy/w;
cout<<*this<<endl;
cout<<i<<endl;
return Q(x,y);
}
friend ostream& operator<< (ostream &wyjscie, Linia const& ex){
wyjscie<<ex.a<<"x + "<<ex.b<<"y = "<<ex.c;
return wyjscie;
}
};//podwyzszenie przez ile/B
int main()
{
Linia* pionowa = new Linia(0,1,0);
Linia* l = new Linia(1,1,3);
// Q q = (*l) * (*pionowa);
cout<<"linia przed podniesieniem "<<*l<<endl;
// cout<<"punkt przeciecia przed podniesieniem: "<<q<<endl;
l = l+3;
cout<<"Line highered"<<*l<<endl;
l = l-3;
cout<<"Line Lowered "<<*l<<endl;
// q = (*l) * (*pionowa);
// cout<<"punkt przeciecia po podniesieniu: "<<q<<endl;
cout << "Hello world!" << endl;
return 0;
}
You are doing pointer arithmetic here. That means that l ends up pointing to some address where is shouldn't:
Linia* l = new Linia(1,1,3);
l = l+3; // l is a pointer!!!
If you stop using new and raw pointers everywhere it might just work.
int main()
{
Linia pionowa(0,1,0);
Linia l(1,1,3);
l = l+3;
cout<<"Line highered"<< l <<endl;
l = l-3;
cout<<"Line Lowered "<< l <<endl;
}
I have a test next week for c++ and I'm preparing myself for it. I'm confused when I have 2 classes as shown below. I have to walk through the execution of the code, line by line, and I'm confused about the marked lines (x = ... and y = ... inside class two) - where does execution go from there?
#include <iostream>
using namespace std;
class one {
int n;
int m;
public:
one() { n = 5; m = 6; cout << "one one made\n"; }
one(int a, int b) {
n = a;
m = b;
cout << "made one one\n";
}
friend ostream &operator<<(ostream &, one);
};
ostream &operator<<(ostream &os, one a) {
return os << a.n << '/' << a.m << '=' <<
(a.n/a.m) << '\n';
}
class two {
one x;
one y;
public:
two() { cout << "one two made\n"; }
two(int a, int b, int c, int d) {
x = one(a, b); //here is my problem
y = one(c, d); //here is my problem
cout << "made one two\n";
}
friend ostream &operator<<(ostream &, two);
};
ostream &operator<<(ostream &os, two a) {
return os << a.x << a.y;
}
int main() {
two t1, t2(4, 2, 8, 3);
cout << t1 << t2;
one t3(5, 10), t4;
cout << t3 << t4;
return 0;
}
from the line x = one(a, b);
it jumps to line
one(int a, int b)
and executes the parameterized constructor of one
same for line y = one(c, d);
x = one(a, b); //here is my problem
y = one(c, d); //here is my problem
What this code does is that it calls the constructor of the class one and assigns the newly created instance of this class to the variables x and y.
The constructor of class one is in line 9.
Current approach works only if you have a default constructor in one class.
It is better to initialize members in constructor initialization list:
two(int a, int b, int c, int d)
: x(a,b), y(c,d)
{
cout << "made one two\n";
}