Overload istream

I am trying to overload istream >> using char*. Please take a look at the code below and try to complete the function containing the operator>>, I could not do it. Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;

class Stream
{
private:
	char* name;
	char* surname;

public:
	Stream();
	Stream(char* ,char*);
	void setName(char*);
	void setSurname(char*);
	friend ostream& operator<<(ostream &output,Stream obj);
	friend istream& operator>>(istream &input, Stream& obj);
};

Stream::Stream()
{
	name=new char[3];
	name="Dave";
	surname=new char[5];
	surname="Example";
}
Stream::Stream(char* n,char* s)
{
	name=new char[strlen(n-1)];
	strcpy(name,n);

	surname=new char[strlen(s-1)];
	strcpy(surname,s);
}

void Stream::setName(char* n)
{
	name=new char[strlen(n-1)];
	strcpy(name,n);
}
void Stream::setSurname(char* s)
{

	surname=new char[strlen(s-1)];
	strcpy(surname,s);
}

ostream &operator<<(ostream &output, Stream obj)
{
	
	output<<obj.name<<" "<<obj.surname<<endl;
	return output;
}
istream & operator>>(istream& input,Stream& obj)
{
//Need to complete here

	return input;
}

int main()
{
	Stream a;
	cout<<a<<endl;
	cout<<"enter name and surname"<<endl;
	cin>>a;
	cout<<a<<endl;


}
Why do both operators take either a reference or an object Stream? Should I assume you don't know about the this pointer?
I am ok with
this
pointer
You haven't even tried. We aren't going to do your homework for you -- especially for simple problems like this.

See if you can write a function that reads a full name using just cin. Once done, you know how to read the full name from the input stream.

By the way, your class is misnamed: it is not a stream -- it is a person's name. It should be something like one of the following:
class PersonsName class FullName class Person
etc.
The name of your class should reflect the nature or purpose for which it exists.

I've been shooting arrows today on my archery range. I could call the arrows "cars", then tell people I've been shooting cars today, but that would confuse people and I would not likely be happy with the consequences.

Hope this helps.
I've been shooting arrows today on my archery range. I could call the arrows "cars", then tell people I've been shooting cars today, but that would confuse people and I would not likely be happy with the consequences.


^this = win

Please do name your classes correctly, in BOTH areas...e.g.:

1
2
3
4
5
class Car { //instead of person = FAIL
//...
}

Car Apple; //also = FAIL 
I appreciate the comments, I really take it seriously, because I know you guys are very experienced. As far as classes names, I have written a bunch of them and I name it according to the item I am learning. The "stream" for example, is only for my reference, this is a class I built for learning iostream operator overload. Thank you guys again, I am learning C++ by myself and you are the only ones I am able to ask for help!! I am trying to follow Duoas advice, I will put here the result as soon as I have it!!
Topic archived. No new replies allowed.