12 Replies - 1172 Views - Last Post: 09 October 2013 - 11:00 AM Rate Topic: -----

#1 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

C++ Operator<< Override inheritance

Posted 08 October 2013 - 09:55 AM

main.cpp

#include "Point.h"
#include "Circle.h"

#include <iostream>

using namespace std;


int main()
{
    cout << "Create point with value 1,2" << endl;
    Point myPoint(1,2); //creates myPoint with xCoordinate 1 and yCoordinate 2
    
    
    cout << "Point: " << myPoint << endl << endl; // output myPoint coordinates
    
    cout << "Set the x-coordinate to 5 and the y-coordinate to 6" << endl;
    
    myPoint.setX(5);    //set myPoint's xCoordinate to 5
    myPoint.setY(6);    //set myPoint's yCoordinate to 6
    
    cout << "Point: " << myPoint << endl << endl; //Output myPoint coordinates
    
    cout << "The X-Coordinate is: " << myPoint.getX() << endl; //Print myPoint xCoordinate value
    cout << "The Y-Coordinate is: " << myPoint.getY() << endl << endl; //Print myPoint yCoordinate value
    
    cout << "Create a circle with a radius of 5.7 and a point of (1,2)" << endl;
    Circle myCircle(1,2,5.7);
    
    cout << "Circle: " << myCircle << endl << endl;
    
    cout << "Set the point to (4,6)" << endl;
    myCircle.setX(4);
    myCircle.setY(6);
    cout << "Circle: " << myCircle << endl << endl;
    
    return 0;
}




Circle.h

#ifndef Ch2HW_MichaelRauch_Circle_h
#define Ch2HW_MichaelRauch_Circle_h

#include <iostream>
#include "Point.h"

using namespace std;

class Circle: public Point
{
public:
    
    //Construtor
    Circle(double x, double y, double rad)
    {
        radius = rad; //Store the radius of the circle
        setX(x); //Stores the xCoordinate
        setY(y); //Stores the yCoordinate
    }
    
private:
    double radius; //Variable to define radius
};

#endif



Point.h

#ifndef Ch2HW_MichaelRauch_Point_h
#define Ch2HW_MichaelRauch_Point_h

#include <iostream>

using namespace std;

class Point
{
        //Overload << operator
    friend ostream& operator<< (ostream&, const Point&);
    
public:
        //Construtor
    Point(double x = 0, double y = 0)
    {
        xCoordinate = x; //Store the X value of the coordinate
        yCoordinate = y; //Store the Y value of the coordinate
    }
        
        //Function to set the y coordinate
    void setY(double y = 0)
    {
        yCoordinate = y; //Store new y value of the coordinate
    }
    
        //Function to set the x coordinate
    void setX(double x = 0)
    {
        xCoordinate = x; //Store new x value of the coordinate
    }
    
        //Function to get the y coordinate
    double getY()
    {
        return yCoordinate; //return the y value of the coordinate
    }
    
        //Function to get the x coordinate
    double getX()
    {
        return xCoordinate; //return the x value of the coordinate
    }
    
private:
    double xCoordinate; //Variable to define x coordinate
    double yCoordinate; //Variable to define y coordinate
};

    //Make << output "(x,y)"
ostream& operator<<(ostream& osObject, const Point& coordinate)
{
    osObject << "(";                        //Current output "("
    osObject << coordinate.xCoordinate;     //Current output "(x"
    osObject << ",";                        //Current output "(x,"
    osObject << coordinate.yCoordinate;     //Current output "(x,y"
    osObject << ")";                        //Current output "(x,y)"
    
    return osObject;    //return osObject which contains "(x,y)"
}

#endif




I am getting an output of:

Create point with value 1,2
Point: (1,2)

Set the x-coordinate to 5 and the y-coordinate to 6
Point: (5,6)

The X-Coordinate is: 5
The Y-Coordinate is: 6

Create a circle with a radius of 5.7 and a point of (1,2)
Circle: (1,2)

Set the point to (4,6)
Circle: (4,6)


but i want an output of:

Create point with value 1,2
Point: (1,2)

Set the x-coordinate to 5 and the y-coordinate to 6
Point: (5,6)

The X-Coordinate is: 5
The Y-Coordinate is: 6

Create a circle with a radius of 5.7 and a point of (1,2)
Circle: (1,2,5.7)

Set the point to (4,6)
Circle: (4,6,5.7)

I have tried getting Circle.h to do this:

#ifndef Ch2HW_MichaelRauch_Circle_h
#define Ch2HW_MichaelRauch_Circle_h

#include <iostream>
#include "Point.h"

using namespace std;

class Circle: public Point
{
public:
    friend ostream& operator<< (ostream&, const Circle&);
    //Construtor
    Circle(double x, double y, double rad)
    {
        radius = rad; //Store the radius of the circle
        setX(x); //Stores the xCoordinate
        setY(y); //Stores the yCoordinate
    }
    
private:
    double radius; //Variable to define radius
};

ostream& operator<<(ostream& osObject, const Circle& info)
{
    osObject << "(";                        
    osObject << info.getX();
    osObject << ",";                        
    osObject << info.getY();
    osObject << ",";                        
    osObject << info.radius;
    osObject << ")";                        
    
    return osObject;    //return osObject which contains "(x,y)"
}


#endif



But i still do not get the output that i want....

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Operator<< Override inheritance

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: C++ Operator<< Override inheritance

Posted 08 October 2013 - 11:04 AM

Your overload << operator for the circle class is private because it's in the private access section of the class.

*EDIT*:
Your inheritance is incorrect IMO. A Circle is a shape not a point. A shape is made up of points. Point is really a helper class to implement other shape classes.

This post has been edited by vividexstance: 08 October 2013 - 11:05 AM

Was This Post Helpful? 2
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: C++ Operator<< Override inheritance

Posted 08 October 2013 - 09:07 PM

I completely agree with vividexstance's assessment of the incorrect inheritance. But then I also believe in the modern view of OOP where code is supposed to reflect our perception of the problem domain.

As I recall, when I was first learning C++, inheritance was used to primarily to save on code duplication. (Yes, it was the time when C++ was being taught as C with classes.) I think that teaching OOP concepts has progressed a lot since then even if some teacher still continue to teach C++ as C with classes.
Was This Post Helpful? 0
  • +
  • -

#4 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 08:48 AM

Where should i look or what can i try to get the output i want? I tried just creating another operator<< override in circle.h but if i do that i can not the the x and yCoordinate from the Point.h?

I can get the radius from the class Circle. but i can not get the x and yCoordinates form the Point class. But the Circle class can get x and y from Point class....

i tried using << Point:xCoordinate and << getX(). Nothing seems to be going my way.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,407
  • Joined: 25-December 09

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 09:01 AM

Why not create a function in your Circle class that returns the points for output. Remember because Circle is derived from Point your Circle class has full access to all the members of Point.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 09:10 AM

You just have to move the declaration of the operator<<() function to the public access section of the class. Just look at your code and see the difference between the two classes. When you derive from a class and want to override member functions, the derived class has to have those member functions declared the same way as the base class.

In your base class, the operator<<() is defined in the public section which is correct because users need to be able to call the function. In the derived class, you have it in the private section of the class so only the implementation of the class can actually use that function. To allow the user to use the function and to override the base class' version of the function it should be public just like the base class.

Remember that the default access mode for classes is private!

View PostSkydiver, on 09 October 2013 - 12:07 AM, said:

I completely agree with vividexstance's assessment of the incorrect inheritance. But then I also believe in the modern view of OOP where code is supposed to reflect our perception of the problem domain.

As I recall, when I was first learning C++, inheritance was used to primarily to save on code duplication. (Yes, it was the time when C++ was being taught as C with classes.) I think that teaching OOP concepts has progressed a lot since then even if some teacher still continue to teach C++ as C with classes.

I learned that the best way to decide between using inheritance or composition is too decide whether there is a "is-a" or a "has-a" relationship.

For this example, a Shape has points, and in my opinion, a Circle is-a Shape which has-a center point and a radius.

So too me, I would define an abstract base class with some helper functions that are general to all Shapes and some pure virtual function(s) like draw(). I would define a Point class that is just a helper class and only deals with Points. The Circle class would obviously be inherited from the Shape class and contain a Point and a radius.
Was This Post Helpful? 2
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4019
  • View blog
  • Posts: 12,407
  • Joined: 25-December 09

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 09:24 AM

But remember "friends" are neither public or private, nor protected, they're special, it doesn't matter where you define them.

See this link.

And from that link:

Quote

Friend declarations are not affected by access specifiers.


Jim
Was This Post Helpful? 2
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,561
  • Joined: 19-February 09

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 09:31 AM

I moved the << operator function bodies from the header files (and put them in main) and that worked fine.

I also changed getY and GetX to const since they are used on a const object (and my compiler complained).

        //Function to get the y coordinate
    double getY() const
    {
        return yCoordinate; //return the y value of the coordinate
    }
    
        //Function to get the x coordinate
    double getX() const
    {
        return xCoordinate; //return the x value of the coordinate
    }


Was This Post Helpful? 2
  • +
  • -

#9 qpMONKEYMIKEqp  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 16-October 12

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 09:40 AM

I have figured it out now! With your help!

I moved the operator<< into public and then my compiler was giving me some error about constants. So I took const out of the operator<< and changed it all from info.Variable to info.getVariable. This let me compile with no errors and get the product i was looking for.

Once again thank you all for the help. I love that the people here give me hits to the correct answers instead of just taking my code fixing it and pasting it into the chat!

My changed code is:

Circle.h


#ifndef Ch2HW_MichaelRauch_Circle_h
#define Ch2HW_MichaelRauch_Circle_h

#include <iostream>
#include "Point.h"

using namespace std;

class Circle: public Point
{
public:
    
    friend ostream& operator<< (ostream&, const Circle&);
    friend ostream& operator<< (ostream&, const Point&);
    //Construtor
    Circle(double x, double y, double rad)
    {
        radius = rad; //Store the radius of the circle
        setX(x); //Stores the xCoordinate
        setY(y); //Stores the yCoordinate
    }
    
    double getRadius()
    {
        return radius;
    }
    
private:
    double radius; //Variable to define radius
};

ostream& operator<<(ostream& osObject, Circle& info)
{
    osObject << "(";                        
    osObject << info.getX();
    osObject << ",";                        
    osObject << info.getY();
    osObject << ",";                        
    osObject << info.getRadius();
    osObject << ")";                        
    
    return osObject;    //return osObject which contains "(x,y)"
}


#endif




Point.h


#ifndef Ch2HW_MichaelRauch_Point_h
#define Ch2HW_MichaelRauch_Point_h

#include <iostream>

using namespace std;

class Point
{
public:
    //Overload << operator
    friend ostream& operator<< (ostream&, const Point&);
    
        //Construtor
    Point(double x = 0, double y = 0)
    {
        xCoordinate = x; //Store the X value of the coordinate
        yCoordinate = y; //Store the Y value of the coordinate
    }
        
        //Function to set the y coordinate
    void setY(double y = 0)
    {
        yCoordinate = y; //Store new y value of the coordinate
    }
    
        //Function to set the x coordinate
    void setX(double x = 0)
    {
        xCoordinate = x; //Store new x value of the coordinate
    }
    
        //Function to get the y coordinate
    double getY()
    {
        return yCoordinate; //return the y value of the coordinate
    }
    
        //Function to get the x coordinate
    double getX()
    {
        return xCoordinate; //return the x value of the coordinate
    }
    
private:
    double xCoordinate; //Variable to define x coordinate
    double yCoordinate; //Variable to define y coordinate
};

    //Make << output "(x,y)"
ostream& operator<<(ostream& osObject, const Point& coordinate)
{
    osObject << "(";                        //Current output "("
    osObject << coordinate.xCoordinate;     //Current output "(x"
    osObject << ",";                        //Current output "(x,"
    osObject << coordinate.yCoordinate;     //Current output "(x,y"
    osObject << ")";                        //Current output "(x,y)"
    
    return osObject;    //return osObject which contains "(x,y)"
}

#endif



This post has been edited by qpMONKEYMIKEqp: 09 October 2013 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#10 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 10:09 AM

With the operator<<() function, the object that is passed needs to be const so it will accept all objects of the class. If you don't change the argument to a const-reference, and then try to output a const object of the class, you will get an error.
Was This Post Helpful? 1
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 10:17 AM

Get rid of the friends as #define suggests. There are rare cases where you might be able to justify the use of friend, but this isn't one of them.

e.g.
class Point {
public:
	Point(double x = 0, double y = 0) : xCoordinate(x), yCoordinate(y) { }
	void setY(double y) { yCoordinate = y; }
	void setX(double x) { xCoordinate = x; }
	double getY() const { return yCoordinate; }
	double getX() const { return xCoordinate; }
private:
	double xCoordinate, yCoordinate;
};

ostream& operator<<(ostream& out, const Point& pt) {
	out << '(' << pt.getX() << ',' << pt.getY() << ')';    
	return out;
}



Also, implementation code should NOT be in a header file.
Was This Post Helpful? 2
  • +
  • -

#12 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 10:21 AM

View Postbaavgai, on 09 October 2013 - 01:17 PM, said:

Also, implementation code should NOT be in a header file.

Unless it's a template class.
Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: C++ Operator<< Override inheritance

Posted 09 October 2013 - 11:00 AM

Of course, but that isn't in play.

OP clarification: you have no template classes. So you, um, have no excuse. ;)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1