class Point2D
{
protected:
int x;
int y;
public:
Point2D () {x=0; y=0;}
Point2D (int a, int b) {x=a; y=b;}
void setX (int);
void setY (int);
int getX ();
int getY ();
};
class Line2D
{
friend ostream& operator<< (ostream&, const Line2D&);
private:
Point2D pt1;
Point2D pt2;
double length;
public:
Line2D () {pt1 = Point2D(); pt2 = Point2D();}
Line2D (Point2D ptA, Point2D ptB) {pt1=ptA; pt2=ptB;}
void setPt1 (Point2D);
void setPt2 (Point2D);
Point2D getPt1 ();
Point2D getPt2 ();
};
ostream& operator<< (ostream &out, const Line2D &l)
{
//out << l.length << endl; //Reading length works perfectly
out << l.getPt1().getX() << endl; //But how to read x from pt1 ?
return out;
}
When I run these codes, I get error saying:
no matching function for call to Line2D::getPt1() const and
note: candidates are: Point2D Line2D::getPt1() <near match>.
If I am only trying to display length by overloading << operator , it works perfectly. But when I try to printx and y of Class::Point2D, I get error.
So what should be the proper way to print out x and y ?
Your operator (rightly) takes a const reference. So any methods called via that reference must be const. For example,
Point2D getPt1 () const;
^^^^^
You should also make the Point2D class getters const.
Related
Having trouble with overloading operator<. I am trying to overload the operator< so that I can sort my Line2D object based on pt1's x. But I am having trouble figuring out how to declare the function.
I am getting the error:
object has type qualifiers that are not compatible with the member function "Point2D::getX".
What I have tried: removing const, putting Point2D &l2dobj instead.
class Line2D
{
private:
Point2D pt1;
Point2D pt2;
public:
bool operator<( const Line2D &l2dobj)
{
return (pt1.getX() < l2dobj.pt1.getX());
}
}
class Point2D
{
protected:
int x;
int y;
public:
int getX();
int getY();
}
Point2D::getX doesn't accept const instance, you cannot apply it on l2dobj while it is a const reference, change getX (and a priori getY) to :
class Point2D
{
protected:
int x;
int y;
public:
int getX() const;
int getY() const;
};
As a general way declare the methods const the more you can, and same for their parameters
I am trying to call the assignment operator from the base class (shape) in the derived class (point). I get an unresolved external linker error and I don't understand why. I put "virtual" in front of the base assignment operator and placed the base assignment operator inside of my derived class at the end. Where did I go wrong?
#ifndef SHAPE_H
#define SHAPE_H
#include <iostream>
using namespace std;
namespace Joe
{
namespace CAD
{
class Shape
{
private:
int m_id;
public:
Shape(); //default constructor
~Shape(); //destructor
Shape(const Shape& s); //copy constructor
virtual Shape& operator = (const Shape& source); //assignment operator
string ToString() const; //returns id as a string
friend ostream& operator << (ostream& os, const Shape& sh); //global function that can access private members
int ID() const;
};
inline int Shape::ID() const //retrieve ID of shape
{
return m_id;
}
}
}
#endif
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include "shape.h"
namespace Joe
{
namespace CAD
{
class Point: public Shape
{
private:
double m_x;
double m_y;
public:
Point(); //default constructor
~Point(); //destructor
Point(const Point& pt); //copy constructor
Point(double newX, double newY) //constructor accepting x and y coordinates
{
m_x = newX;
m_y = newY;
std::cout << "Point(" << m_x <<","<< m_y <<")" << std::endl;
}
Point(double val); //constructor accepting one double
void X(double newXval) {m_x = newXval;} //default inline setter
void Y(double newYval) {m_y = newYval;} //default inline setter
double X() const; //getter pre-inline
double Y() const; //getter pre-inline
std::string ToString() const; //returns a string description
//distance functions
double Distance() const; //calculate the distance to the origin (0,0)
double Distance(const Point& p) const; //calculate the distance between two points
//operator overloading
Point operator - () const; //negate the coordinates
Point operator * (double factor) const; //scale the coordinates
Point operator + (const Point& p) const; //add coordinates
bool operator == (const Point& p) const; //equally compare operator
Point& operator = (const Point& source); //assignment operator
Point& operator *= (double factor); //scale the coordinates and assign
friend std::ostream& operator << (std::ostream& os, const Point& p); //send to ostream (friend)
Shape& operator = (const Shape& source); // call assignment operator of base class
};
inline double Point::X() const //normal inline getter
{
return m_x;
}
inline double Point::Y() const //normal inline getter
{
return m_y;
}
}
}
#endif
Shape& Shape::operator = (const Shape& source) //assignment operator
{
if (this == &source) //avoid self-assignment
return *this;
cout << "shape assignment" << endl;
m_id = source.m_id;
return *this;
}
Point& Point::operator = (const Point& source) //assign
{
if (this == &source) //avoid self-assignment
return *this;
m_x = source.m_x;
m_y = source.m_y;
return *this;
}
Your problem is that when you call operator = and pass it a Shape, you are calling to Point::operator=(const Shape&), which you do not define, you only define Shape::operator=(const Shape&) and Point::operator=(const Point&). You need to add Point::operator=(const Shape&), for example like this:
Shape& Point::operator = (const Shape& shape) //assign
{
const Point& source = static_cast<Point&>(shape); // or use dynamic_cast here, if you want to be safe
if (this == &source) //avoid self-assignment
return *this;
m_x = source.m_x;
m_y = source.m_y;
return *this;
}
Your problem might be that you define the class in a namespace, but you define the operators outside that namespace without specifying it. Because of this, the compiler can't connect the definition to the declaration.
Ishamael pointed out that you don't specify what happens when a non-Point Shape is assigned to a Point. This is necessary for a virtual assignment operator; see his answer. But this virtual assignment operator could end up doing a lot of unexpected things, like cutting off parts of objects if the wrong types are assigned to each other.
You don't need the virtualness to ensure that the Shape operator also gets called when Point is assigned. Just call the Shape operator inside the Point operator. Making the operator virtual would actually have the opposite effect; the Point operator would override the Shape operator even if the instance you're calling it with is referred to as a Shape.
Point& Point::operator = (const Point& source) //assign
{
if (this == &source) //avoid self-assignment
return *this;
Shape::operator=(source); // also call the base class operator
m_x = source.m_x;
m_y = source.m_y;
return *this;
}
#include <iostream>
using namespace std;
class family
{
private:
double weight;
double height;
public:
family(double x,double y);
~family();
double getWeight();
double getHeight();
double setWeight();
double setHeight();
bool operator==(const family &)const;
};
bool family::operator ==(const family &b)const
{
return weight==b.weight;
}
family::family(double x, double y)
{
weight = x;
height = y;
}
double family::getWeight()
{
return weight;
}
double family::getHeight()
{
return height;
}
family::~family(){}
int main()
{
family a(70.0,175.2);
family b(68.5,178.2);
if(a==b)
cout << "A is bigger than B" << endl;
else
cout << "A is smaller than B" << endl;
return 0;
}
Above the code, I can implement operator overloading with member function. However, I failed to implement operator overloading with non member function. How should i modify this code b.b
Please help me..
Basically, the only difference between a member function and a non-member function is that it's passed an implicit this pointer as well as any other arguments and it has access to private/protected members. So to transform any member function to a non-member function is to simply factor it out of the class definition. make it a friend of that class and add a parameter that's a reference to that class. Pass in an object of that class when you call it. You can also do a const& of the function.
You can use Friend Function and use objects as parameter of that function like we use in << operator.
we also can use operator without friend function :
bool operator==(const family first, const family second)
{
if(first.getWeight( ) == second.getWeight( )){
return True;
else
return False;
}
class family
{
private:
double weight;
double height;
public:
family( double x, double y );
~family( );
// getters should be const
double getWeight( ) const;
double getHeight( ) const;
double setWeight( );
double setHeight( );
};
// no reason to make it friend of class
// b/c it does not work with private/protected members
bool operator==( const family & first, const family & second );
// better to move into cpp file
bool operator==( const family & first, const family & second )
{
return first.getWeight( ) == second.getWeight( );
}
Im new at OOP and I keep having this error in this task. Maybe you can help me out.
This right here is the Class header file:
class Distance : public Magnitude
{
private:
double Cantidad;
char* Unidad;
public:
Distance(double c, char* u);
Distance(const Distance& d);
double getDistance(){return Cantidad;}
void setDistance(double val) {Cantidad=val;}
char* getUnidad(){return Unidad;}
void setUnidad(char* uni) {Unidad=uni;}
virtual ~Distance();
Distance& operator =(const Distance & d);
Distance operator +(const Distance & d);
Distance operator -(const Distance & d);
Distance operator *(const Distance & d);
Distance operator /(const Distance & d);
friend ostream& operator << (ostream &o,const Distance &d);
friend istream& operator >> (istream &o, Distance &d);
};
This over here is the cpp file, where I made the definitions:
#include "Distance.h"
Distance::Distance(double c, char* u)
{
Cantidad=c;
Unidad=u;
}
Distance::Distance(const Distance& d)
{
cout << "[***] NumComplejo -> Constructor por copia " << endl;
Cantidad = d.Cantidad;
Unidad = d.Unidad;
}
Distance::~Distance()
{
//dtor
}
And finally, this is where the error appears, in main, where I try to declare an object from the class Distance.
int main(int argc, char *argv[])
{
Distance d1; **/*right here*/**
EDIT:
If I typed: Distance d1=Distance(1231,"CSDVS"); it does work, but I need an empty objet so I can use the overload on >> operator
The error is caused by the fact that you are trying to initialize an object with a default constructor that you haven't defined.
[...] but I need an empty objet so I can use the overload on >> operator.
You can do this by simply defining a default constructor:
// …
Distance::Distance()
: Cantidad(0)
, Unidad(nullptr)
{}
// …
You, also, probably want to use std::string for strings.
Why doesn't this work?
I use friendly function in my code but there is an error so I can not find it. please help.
#include<iostream>
#include<cstdlib>
using namespace std;
class Circle{
private:
int x;
public:
Circle(int x1=5){
x=x1;
friend std:ostream & operator<<(const Circle & c, std::ostream & os)
{
return os<<c.x
}
}
};
int main()
{
Circle s;
cout<< s;
system("pause");
return 0;
}
Four problems:
You've defined the friend function inside the constructor. Move it outside so that it's its own function.
Replace std:ostream with std::ostream
Swap the order of the parameters.
Add a semicolon after return os<<c.x
Final result:
class Circle{
private:
int x;
public:
Circle(int x1=5){
x=x1;
}
friend std::ostream & operator<<(std::ostream & os, const Circle & c)
{
return os<<c.x;
}
};
friend std:ostream & operator<<(const Circle & c, std::ostream & os)
{
return os<<c.x
}
you should declare this function outside the constructor.
The friend function needs to be declared at the same level as the constructor, not inside it.