Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,367 questions

11,628 answers

573 users

How to print object with overloading ostream operator in C++

1 Answer

0 votes
#include <iostream>
#include <fstream>

using std::cout;
using std::endl;
using std::cin;
using std::istream;
using std::ostream;

class CPhone {
	char name[30];
	char number[8];
public:
	CPhone() { };
	CPhone(char *_name, char *_number) {
		strcpy(name, _name);
		strcpy(number, _number);
	}
	friend ostream &operator << (ostream &stream, CPhone o);
	friend istream &operator >> (istream &stream, CPhone &o);
};

ostream &operator << (ostream &stream, CPhone o) {
	stream << o.name << " ";
	stream << o.number << endl;
	return stream; 
}

istream &operator >> (istream &stream, CPhone &o) {
	cout << "Enter name: ";
	stream >> o.name;
	cout << "Enter number: ";
	stream >> o.number;
	cout << endl;
	return stream;
}

int main()
{
	CPhone po;

	cin >> po;
	cout << po;

	return 0;
}


/*
run:

Enter name: Tom
Enter number: 987563

Tom 987563

*/

 




answered Jul 5 by avibootz
...