Implementing objects from one class in another - c++

I've been having trouble trying to implement objects from class Cart_Vector in class Cart_Point. My compiler has been listing the following errors and I can't seem to fix them:
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
'Cart_Vector' does not name a type
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
'Cart_Vector' does not have a type
x = p1.x + v1.x; request for member 'x' in 'v1', which is of non-class
type 'const int'
y = p1.y + v1.y; request for member 'y' in 'v1', which is of non-class
type 'const int'
return Cart_Vector(x,y); 'Cart_Vector' was not declared in this scope
#include <iostream>
#include <math.h>
using namespace std;
class Cart_Point
{
public:
double x;
double y;
friend class Cart_Vector;
Cart_Point (double inputx, double inputy);
friend Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2);
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
double Cart_distance(Cart_Point, Cart_Point);
};
Cart_Point::Cart_Point(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}
Cart_Point operator<<(const Cart_Point&p1, const Cart_Point&p2)
{
cout << "p1:(" << p1.x << ", " << p1.y << ")" << endl;
cout << "p2:(" << p2.x << ", " << p2.y << ")" << endl;
return p1,p2;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
double x,y;
x = p1.x + v1.x;
y = p1.y + v1.y;
return Cart_Point(x,y);
//this function should make a new Cart_Point
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
class Cart_Vector
{
public:
double x; //x displacement of vector
double y; //y displacement of vector
Cart_Vector(double inputx, double inputy);
friend Cart_Vector operator*(const Cart_Vector&v1, double d);
friend Cart_Vector operator/(const Cart_Vector&v1, double d);
Cart_Vector operator<<(const Cart_Vector&v1);
friend class Cart_Point;
};
Cart_Vector::Cart_Vector(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
double x,y;
x = v1.x*d;
y = v1.y*d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
double x,y;
if (d == 0)
{
x = v1.x;
y = v1.y;
}
x = v1.x/d;
y = v1.y/d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
Cart_Vector Cart_Vector::operator<<(const Cart_Vector&v1)
{
cout <<"v1: <" << v1.x << ", " << ">" << endl;
return v1;
//this function should just print v1
}
//TestCheckpoint1.cpp file below
int main()
{
//I haven't finished the main function to test all the functions yet
return 0;
}

split your code in different files but your implementation of operator << is wrong , consider following code , its just a hint to help
#include <iostream>
#include <math.h>
using namespace std;
class Cart_Vector
{
public:
double x; //x displacement of vector
double y; //y displacement of vector
Cart_Vector(double inputx, double inputy);
friend Cart_Vector operator*(const Cart_Vector&v1, double d);
friend Cart_Vector operator/(const Cart_Vector&v1, double d);
friend std::ostream& operator<<( std::ostream& out,const Cart_Vector&v1);
friend class Cart_Point;
};
Cart_Vector::Cart_Vector(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
Cart_Vector operator*(const Cart_Vector&v1, double d)
{
double x,y;
x = v1.x*d;
y = v1.y*d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector
}
Cart_Vector operator/(const Cart_Vector&v1, double d)
{
double x,y;
if (d == 0)
{
x = v1.x;
y = v1.y;
}
x = v1.x/d;
y = v1.y/d;
return Cart_Vector(x,y);
//this function should make a new Cart_Vector and dividing by zero creates v1
}
std::ostream& operator<<(std::ostream &out, const Cart_Vector&v1)
{
out <<"v1: <" << v1.x << ", " << ">" << endl;
return out;
//this function should just print v1
}
class Cart_Point
{
public:
double x;
double y;
friend class Cart_Vector;
Cart_Point (double inputx, double inputy);
friend std::ostream& operator<<(std::ostream& out , const Cart_Point&p2);
friend Cart_Point operator+(const Cart_Point&p1,const Cart_Vector&v1);
friend Cart_Point operator-(const Cart_Point&p1,const Cart_Point&p2);
double Cart_distance(Cart_Point, Cart_Point);
};
Cart_Point::Cart_Point(double inputx, double inputy)
{
x = inputx;
y = inputy;
}
double Cart_Point::Cart_distance(Cart_Point p1, Cart_Point p2)
{
double distance = (sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) ));
return distance;
//returns distance between p1 (point 1) and p2 (point 2)
}
std::ostream& operator<<(std::ostream &out, const Cart_Point&p1)
{
// Since operator<< is a friend of the Cart_Point class, we can access Point's members directly.
out << "p:(" << p1.x << ", " << p1.y << ")" << endl;
return out;
//this function should just print each point
}
Cart_Point operator+(const Cart_Point&p1, const Cart_Vector&v1)
{
double x,y;
x = p1.x + v1.x;
y = p1.y + v1.y;
return Cart_Point(x,y);
//this function should make a new Cart_Point
}
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
return Cart_Point(x,y);
//this function should make a new Cart_Vector
}
//TestCheckpoint1.cpp file below
int main()
{
Cart_Point point1(2.0, 3.0);
std::cout << point1;
//I haven't finished the main function to test all the functions yet
return 0;
}
Wandbox :

Do yourself a favor and split your code in different files,at least one .h for Cart_Vector, one .h for Cart_Point, and one .cpp for the test script (main). You can correct this code to make it work (just swap the order of the two classes), if you correct other errors too, like in the overloading of the operator "-".
The point here is that if you start coding in this way, when you start programming complex projects you will find yourself in great difficulties. Coding one public (non-inner) class-per file is like applying Single Responsibility Principle for files too. I would say that also a file should have only one reason to change, and this has great benefits in readability and when will you perform version control.

Cart_Vector and Cart_point need each other. So you need to implement these classes in separate header files. Do not forget include them. Also in here ;
Cart_Point operator-(const Cart_Point&p1, const Cart_Point&p2)
{
double x,y;
x = p1.x- p2.x;
y = p1.y - p2.y;
//return Cart_Vector(x,y);
return Cart_Pointer(x,y);
//this function should make a new Cart_Vector
}
You can not access non-const elements of const object and you can not call non-const functions. You can pass these objects by value if you want it.

Related

Why use a reference when returning an ostream class?

The full code looks like this:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Point {
double x;
double y;
public:
Point();
Point(int x, int y);
void setPoint(int x, int y);
int getX(void)const;
int getY(void)const;
Point operator + (const Point & point);
Point& operator=(const Point& point);
};
ostream& operator<<(ostream& os, const Point& point) {
os << point.getX() << " ," << point.getY();
return os;
}
Point::Point() {
x = y = 0;
}
Point::Point(int x, int y) {
this->x = x;
this->y = y;
}
void Point::setPoint(int x, int y) {
this->x = x;
this->y = y;
}
int Point::getX(void)const {
return this->x;
}
int Point::getY(void)const {
return this->y;
}
Point Point::operator+(const Point& point) {
Point result(this->x + point.getX(), this->y + point.getY());
return result;
}
Point& Point::operator=(const Point& point) {
this->x = point.getX();
this->y = point.getY();
return (*this);
}
int main() {
Point a(1, 2);
cout << a << endl;
}
In the following part
ostream& operator<<(ostream& os, const Point& point) {
os << point.getX() << " ," << point.getY();
return os;
}
If the return type is ostream, a red line appears. I am not sure what the difference between return type is ostream & and ostream. I'd appreciate it if you explained
*For reference, I forgot to return os, so I edited the post
If you would return an ostream then you would have slicing. In case the passed os was an ofstream then the returned ostream would only be the ostream part of the ofstream and not the full ofstream. When returning by reference you don't have slicing and don't lose information about the full type of the stream.
The ostream object cannot be copied as the copy constructor of ostream object is deleted.
That is why one needs to pass the ostream object by reference.
Pro tip: Ever wondered why we need to return ostream& if it was already passed by reference?

Assignment operator overloading

I was trying to overload assignment operator. Given
Point p1(1,2,3);
Point p2(1,2,3);
Point p3 = p1 + p2;
Point p4 = 22;
cout<< p4;
Here is my full code:
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
class Point{
private:
int m_x, m_y, m_z;
public:
Point(int x=0, int y = 0, int z = 0):m_x(x), m_y(y), m_z(z)
{
}
friend Point operator+(Point &p1, Point &p2);
friend ostream& operator<<(ostream &out, Point &p);
Point operator=(int val);
};
Point operator+(Point &p1, Point &p2){
return Point(p1.m_x+p2.m_x , p1.m_y+ p2.m_y , p1.m_z+p2.m_z);
}
ostream& operator<<(ostream &out, Point &p){
out<<"output: "<<p.m_x<<" "<<p.m_y<<" "<< p.m_z<<'\n';
return out;
}
Point Point::operator=(int val){
return Point(val, val, val);
}
int main(){
Point p1(1,2,3);
Point p2(1,2,3);
Point p3 = p1 + p2;
Point p4 = 22;
cout<< p4;
}
I can't insert the value 22 or any value in m_x, m_y ,m_z. How can I solve the line:
Point p4 = 22;
The are 2 different problems here.
Point p4 = 22;
This is not an assignment, it's actually a call to a constructor. Since you declared your constructor that takes 3 ints with default values, it can be called with 1, 2 or 3 values.
So it's equivalent of doing either of these two
Point p4(22, 0, 0);
Point p4(22);
If you want to use the assignment operator you need to write
Point p4;
p4 = 22;
But here we run in to the second problem, your assignment operator creates a new Point and returns it by value. What you want to do is modify the existing one.
Point& Point::operator=(int val){ // Return by reference
m_x = m_y = m_z = val;
return *this;
}
And you don't need to make the + operation a friend function.
using namespace std;
class Point {
public:
Point() {}
Point(int x , int y , int z ) :m_x(x), m_y(y), m_z(z)
{
}
Point& operator+(const Point &p1);
friend ostream& operator<<(ostream &out, Point &p);
Point& operator=(int val);
private:
int m_x, m_y, m_z;
};
Point& Point::operator+(const Point& p1)
{
Point temp;
temp.m_x = this->m_x + p1.m_x;
temp.m_y = this->m_y + p1.m_y;
temp.m_z = this->m_z + p1.m_z;
return temp;
}
ostream& operator<<(ostream &out, Point &p) {
out << "output: " << p.m_x << " " << p.m_y << " " << p.m_z << '\n';
return out;
}
Point& Point::operator=(int val) { // Return by reference
m_x = m_y = m_z = val;
return *this;
}
int main() {
Point p1(1, 2, 3);
Point p2(1, 2, 3);
Point p3 = p1 + p2;
Point p4;
p4 = 22;
cout << p4;
}

Inheritance of operator overloads

I'm having an issue with the inheritance of my operator overloading functions in my derived class.
#pragma once
#include "Math.hpp"
using namespace Math;
class Euler
{
public:
float x, y, z;
Euler();
Euler(const float &, const float &, const float &);
float &operator[](const char &) const;
Euler &operator=(const Euler &);
Euler &operator+=(const Euler &);
Euler &operator-=(const Euler &);
Euler &operator*=(const float &);
Euler &operator/=(const float &);
Euler operator+(const Euler &) const;
Euler operator-(const Euler &) const;
Euler operator*(const float &) const;
Euler operator/(const float &) const;
bool operator==(const Euler &) const;
bool operator!=(const Euler &) const;
bool IsValid() const;
void Clear();
};
inline Euler::Euler()
{
x = y = z = 0.f;
}
inline Euler::Euler(const float &_x, const float &_y, const float &_z)
{
x = _x;
y = _y;
z = _z;
}
inline float &Euler::operator[](const char &c) const
{
return ((float *)this)[c];
}
inline Euler &Euler::operator=(const Euler &e)
{
x = e.x;
y = e.y;
z = e.z;
return *this;
}
inline Euler &Euler::operator+=(const Euler &e)
{
x += e.x;
y += e.y;
z += e.z;
return *this;
}
inline Euler &Euler::operator-=(const Euler &e)
{
x -= e.x;
y -= e.y;
z -= e.z;
return *this;
}
inline Euler &Euler::operator*=(const float &e)
{
x *= e;
y *= e;
z *= e;
return *this;
}
inline Euler &Euler::operator/=(const float &e)
{
x /= e + M_FLT_EPSILON;
y /= e + M_FLT_EPSILON;
z /= e + M_FLT_EPSILON;
return *this;
}
inline Euler Euler::operator+(const Euler &e) const
{
return Euler(x + e.x, y + e.y, z + e.z);
}
inline Euler Euler::operator-(const Euler &e) const
{
return Euler(x - e.x, y - e.y, z - e.z);
}
inline Euler Euler::operator*(const float &f) const
{
return Euler(x * f, y * f, z * f);
}
inline Euler Euler::operator/(const float &f) const
{
return Euler(x / (f + M_FLT_EPSILON), y / (f + M_FLT_EPSILON), z / (f + M_FLT_EPSILON));
}
inline bool Euler::operator==(const Euler &e) const
{
return e.x == x && e.y == y && e.z == z;
}
inline bool Euler::operator!=(const Euler &e) const
{
return e.x != x || e.y != y || e.z != z;
}
inline bool Euler::IsValid() const
{
using namespace std;
return isfinite(x) && isfinite(y) && isfinite(z);
}
inline void Euler::Clear()
{
x = y = z = 0.f;
}
class Vector : public Euler
{
public:
using Euler::Euler;
void Rotate(const Angle &);
void Rotate2D(const float &);
float Length() const;
float LengthSqr() const;
float Length2D() const;
float Length2DSqr() const;
float DistTo(const Vector &) const;
float DistToSqr(const Vector &) const;
};
inline float Vector::Length() const
{
return Sqrt((x * x) + (y * y) + (z * z));
}
inline float Vector::LengthSqr() const
{
return (x * x) + (y * y) + (z * z);
}
inline float Vector::Length2D() const
{
return Sqrt((x * x) + (y * y));
}
inline float Vector::Length2DSqr() const
{
return (x * x) + (y * y);
}
inline float Vector::DistTo(const Vector &v) const
{
return (*this - v).Length();
}
inline float Vector::DistToSqr(const Vector &v) const
{
return (*this - v).LengthSqr();
}
The code shown below contains an error in the distto and DistToSqr function wherein it calculates (*this - v) as the superclass Euler and therefore cannot find the length function.
I was wondering why this would be as this code compiles on my laptop and not my desktop.
I'd be grateful if anyone could show me why this code doesn't work and what is the best course in fixing it.
It seems like doing this is a plausible way to fix it.
inline float Vector::DistTo(const Vector &v) const
{
Vector tmp = (*this - v);
return tmp.Length();
}
Still wondering if this is the best option to fix it though.
I'm going to assume that it is likely that Euler is always used as a base type of a more specific type? If so, you can might be able to solve this using the CRTP pattern. Here is a subset of your code in this style:
#include <cmath>
template <typename Derived>
class Euler
{
public:
float x, y, z;
Euler();
Euler(const float &, const float &, const float &);
template <typename T>
Derived & operator-=(Euler<T> const &);
template <typename T>
Derived operator-(Euler<T> const &) const;
private:
Derived & derived() const {
return static_cast<Derived &>(*this);
}
Derived const & derived() {
return static_cast<Derived const &>(*this);
}
};
template <typename Derived>
Euler<Derived>::Euler() : x(0), y(0), z(0)
{}
template <typename Derived>
Euler<Derived>::Euler(const float &_x, const float &_y, const float &_z)
: x(_x), y(_y), z(_z)
{}
template <typename Derived>
template <typename T>
Derived & Euler<Derived>::operator-=(Euler<T> const & e)
{
x -= e.x;
y -= e.y;
z -= e.z;
return derived();
}
template <typename Derived>
template <typename T>
Derived Euler<Derived>::operator-(Euler<T> const & e) const
{
return Derived(x - e.x, y - e.y, z - e.z);
}
class Vector : public Euler<Vector>
{
public:
using Euler<Vector>::Euler;
float Length() const;
float DistTo(const Vector &) const;
};
inline float Vector::Length() const
{
return std::sqrt(x * x + y * y + z * z);
}
inline float Vector::DistTo(const Vector & v) const
{
return (*this - v).Length();
}

Return object from class member function in C++

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;
}

Weird behaviour of overloaded + operator

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;
}