operator<< and >>

D

Dan

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

Bob Hairgrove

[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.
 
V

Victor Bazarov

Dan said:
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?
 
D

Dan

Victor Bazarov said:
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
 
P

Prateek R Karandikar

Dan said:
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
-- --
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top