operator<< and >>

Discussion in 'C++' started by Dan, Jun 18, 2004.

  1. Dan

    Dan Guest

    Hi,
    I would just like to know if the istream operator takes only one
    parammeter(object) at a time (like z) ?
    istream operator>>(istream& in, Shape &z)
    Cause I keep getting error concerning the amount my operator has for bother
    cin , cout operator<< and >>

    thanks

    Dan



    #include <iostream>
    #include<conio.h>
    #include<cmath>
    #include <algorithm>
    using namespace std;

    const double PI = 3.14159;
    void Validate(float &);
    int Validate_positive(float &);

    float Abs(float Nbr)
    { if( Nbr >= 0 )
    return Nbr;
    else
    return -Nbr; }


    class Point {
    public:
    float x,y,c;
    Point(float x1, float y1, float c1) {x = x1; y = y1; c = c1;}
    Point() { x = y = c = 0;}
    void get(float &u, float &v, float &w) {
    u = x; v = y;w = c; }
    };

    class Shape {

    public:
    Point p;
    Point p1;
    Shape() { }
    Shape(Point p) : p1(p)
    { }

    void get(float &a, float &b, float &c) {
    p1.get(a,b,c); }

    virtual void Read(){};
    virtual float GetVolume(){};
    virtual void Display(){};
    };

    class Circle: public Shape
    {
    public:
    Circle(){};
    Circle(Point p, float r) : Shape(p) {radius = r; }

    // void SetArea() {area= PI * radius * radius; }

    float GetVolume(){ return PI * radius * radius;; }

    friend istream& operator >> (istream& in, Shape& z);
    friend ostream& operator << (ostream& out, const Shape* z);

    void Read();

    void display() {
    float x,y,c;
    get(x,y,c);
    cout <<"This circle has center :"<<x<<","<<y<< endl;
    cout<<" And color: " <<c<<" and radius :"<< radius <<endl; }
    private:
    float radius;

    float x,y,c, z;
    };

    private:
    Point p2;
    Point p3;
    float area;

    };

    void Circle::Read() {


    cout<<" Enter the Center Point of the circle, X-Axis: " ;
    cin>> x ;

    cout<<endl;
    cout<<" Enter the Center Point of the circle, Y-Axis: " ;
    cin>> y ;

    cout<<" Enter the Color of that point: " ;
    cin>> c ;

    cout<<" Enter the radius of the circle: " ;
    cin>> z;

    Point center(x,y,c);
    Circle(center, z);
    }


    void menu();


    int main()
    {
    menu();
    getch();
    return 0;
    }

    void menu()
    { char choice;
    int i;
    Shape* myShapes [10];
    // Shape* ptr;

    int countShapes = 0;

    Circle circle;


    do {
    cout << endl << endl;
    cout << " Shape Management System "<<endl;
    cout << "============================================== "<<endl;
    cout << " 1: Create a Circle "<<endl;
    cout << " 2: Create a Triangle "<<endl;
    cout << " 3: Create a Cylinder "<<endl;
    cout << " 4: Display all Shapes "<<endl;
    cout << " 5: Quit "<<endl;
    cout << "============================================== "<<endl;
    cout << " Your choice please: " ;
    cin >> choice;

    switch (choice)
    {
    case '1':
    cin >> circle;
    myShapes[countShapes++]= &circle;

    break;
    case '2': //ptr = new Triangle;
    // myShapes[countShapes]->CreateShape();
    // countShapes++;
    break;
    case '3': //ptr = new Cylinder;
    // myShapes[countShapes]->CreateShape();
    // countShapes++;
    break;
    case '4':
    cout << " --- Displaying Objects --- " << endl;
    for ( i=0; i < countShapes; i++)
    cout << myShapes;

    break;
    case '5': cout<<"Thank you for having used this system, Bye Bye!!!" ;
    break;
    default: cout<<"Error: Invalid option, Please try again" <<endl;
    }

    // myShapes[countShapes] = ptr;

    }while ( (choice != '5') ) ;

    }

    istream operator>>(istream& in, Shape &z)
    {
    z.Read();
    return in;
    }
    ostream operator <<(ostream& out, Shape *z)
    {
    z->Display();
    return out;
    }
    Dan, Jun 18, 2004
    #1
    1. Advertising

  2. On Fri, 18 Jun 2004 13:34:54 -0400, "Dan" <lerameur101 @ Yah00.com>
    wrote:

    [snip]
    >istream operator>>(istream& in, Shape &z)
    >{
    >z.Read();
    >return in;
    >}
    >ostream operator <<(ostream& out, Shape *z)
    >{
    >z->Display();
    >return out;
    >}


    You need to return references, not objects. Your forward declarations
    were correct -- just append the "&" to the return type.


    --
    Bob Hairgrove
    Bob Hairgrove, Jun 18, 2004
    #2
    1. Advertising

  3. Dan wrote:
    > I would just like to know if the istream operator takes only one
    > parammeter(object) at a time (like z) ?
    > istream operator>>(istream& in, Shape &z)
    > Cause I keep getting error concerning the amount my operator has for bother
    > cin , cout operator<< and >>


    What error?
    Victor Bazarov, Jun 18, 2004
    #3
  4. Dan

    Dan Guest

    "Victor Bazarov" <> wrote in message
    news:IVGAc.1433$...
    > Dan wrote:
    > > I would just like to know if the istream operator takes only one
    > > parammeter(object) at a time (like z) ?
    > > istream operator>>(istream& in, Shape &z)
    > > Cause I keep getting error concerning the amount my operator has for

    bother
    > > cin , cout operator<< and >>

    >
    > What error?


    These:

    assig4_1.obj : error LNK2001: unresolved external symbol "class
    std::basic_ostream<char,struct std::char_traits<char> > & __cdecl
    operator<<(class std::basic_ostream<char,struct std::char_traits<char> >
    &,class Shape const *)" (??6@YAAAV?$basic_ostr
    eam@DU?$char_traits@D@std@@@std@@AAV01@PBVShape@@@Z)
    assig4_1.obj : error LNK2001: unresolved external symbol "class
    std::basic_istream<char,struct std::char_traits<char> > & __cdecl
    operator>>(class std::basic_istream<char,struct std::char_traits<char> >
    &,class Shape &)" (??5@YAAAV?$basic_istream@DU
    ?$char_traits@D@std@@@std@@AAV01@AAVShape@@@Z)
    Debug/assig4_1.exe : fatal error LNK1120: 2 unresolved externals
    Dan, Jun 18, 2004
    #4
  5. "Dan" <lerameur101 @ Yah00.com> wrote in message news:<0JFAc.281$>...
    > Hi,
    > I would just like to know if the istream operator takes only one
    > parammeter(object) at a time (like z) ?
    > istream operator>>(istream& in, Shape &z)
    > Cause I keep getting error concerning the amount my operator has for bother
    > cin , cout operator<< and >>
    >
    > thanks
    >
    > Dan
    >
    >
    >
    > #include <iostream>
    > #include<conio.h>

    "conio.h"??? There is no such standard header;

    -- --
    To iterate is human, to recurse divine;
    -L. Peter Deutsch
    -- --
    Prateek R Karandikar, Jun 22, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Vinokur
    Replies:
    4
    Views:
    3,032
    Peter Koch Larsen
    Nov 26, 2004
  2. Alex Vinokur
    Replies:
    3
    Views:
    4,997
    Jeff Schwab
    Mar 20, 2005
  3. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    475
    Barry Schwarz
    Feb 8, 2005
  4. Replies:
    8
    Views:
    318
    Neil Cerutti
    Dec 22, 2005
  5. Hicham Mouline
    Replies:
    2
    Views:
    677
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page