OSTREAM and ISTREAM overloading

Hi guys is this code is good or i still need getter and setter functions
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <string>
using namespace std;
class VEHICLE{ // Class Vehicle defination
	
public:
	
	VEHICLE();
	// Declaration of Public funtions and members
	void display(VEHICLE*);
	void get_input(string,string,int,int);
	friend istream& operator >>(istream &,VEHICLE &);
	friend ostream& operator <<(ostream&,VEHICLE&);
	
	~VEHICLE()
	{
		
		cout<<"Object Deleted"<<endl;
	}
	// Private Section of the class Vehicale 
	private:
    string company;
    string model;	
	int year;
	int	engin;
	
};
VEHICLE::VEHICLE() // default Class constructor's defination
{
	company = "";
	model = "";
	year = 0;
	engin = 0;
}
istream& operator >>(istream& in,VEHICLE& V)
{
	    cout<<"Please Enter The Name Of Vehicle Maker Company: ";
		in>>V.company;
		cout<<endl;
		cout<<"Please Enter the Model Of The Vehicle: ";
		in>>V.model;
		cout<<endl;
		cout<<"Please Enter The Year Of Registration: ";
		in>>V.year;
		cout<<endl;
		cout<<"Please Enter The Engin Capacity: ";
		in>>V.engin;
		cout<<endl;
		return in;

}
ostream& operator << (ostream& os,VEHICLE& V)
{
	os<<"Company: "<<V.company<<'\n'<<endl;
	os<<"Model: "<<V.model<<'\n'<<endl;
	os<<"Year of Manufacture: "<<V.year<<'\n'<<endl;;
	os<<"Engin Capacity in CC: "<<V.engin<<'\n'<<endl;
	return os;
}
int main()
{
	string comp;
	string mod;
	int y = 0;
	int eng = 0;
    int vnum = 0;
	VEHICLE* vehiptr;
	cout<<"Please Enter The Number Of Vehicle: ";
	cin>>vnum;
	cout<<endl;	
	
	vehiptr = new VEHICLE[vnum]; // Creating a pointer array according to the number of Vehicles

	for(int i=0;i<vnum;i++)
	{
	cout<<"VEHICLE "<<i+1<<endl;
	cout<<endl;
	cin>>vehiptr[i];
	
	}
	for(int j=0;j<vnum;j++)
	{
	cout<<"VEHICLE ["<<j+1<<']'<<endl;
	cout<<endl;
	cout<<vehiptr[j];

	}
	cout<<endl;
	delete [] vehiptr; // Calling the distructor to delete the objects
	cout<<endl;
	system("pause");
}

i didn't comment the code so please ignore the commenting heheh :D
closed account (3TXyhbRD)
It's still bad. Your operator>>() overload outputs to the standard output some messages. Imagine you want to use the same operator when reading from a file. When you will do that a bunch of messages will show on screen (or whichever is the standard output).

In your output operator you have things like <<'\n'<<endl. Use <<endl<<endl as this will work accordingly (e.g.: for Unix endl means '\n' while for Windows it means "\r\n").

PS: when you code either indent by tabs only or by spaces only. Tabs don't always render the same in all editors (e.g.: 1 tab doesn't always mean 4 spaces). When copying and pasting code that has mixed indents (tabs + spaces) in some editors it will look "right" while in others it won't.
Topic archived. No new replies allowed.