How to overload stream operator without using friend function?

I'm studing about operator overloading.
I've written a simple program to show operator overloading by using friend function.
How can I write this without friend function?

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
#include<iostream>
#include<string>
using namespace std;
class Student
{
    string name;
    int roll;
    int age;
    string branch;
public:
    Student()
    {
        name= "INVALID NAME";
        roll= 0;
        age = 0;
        branch= "INVALID";
    }
    friend ostream &operator <<( ostream &out, Student &st);
    friend istream &operator >> (istream &input, Student &st);
};
ostream &operator <<( ostream &out, Student &st)
{
    cout << "Details Of The Student: "<<endl;
    out << "NAME : "<<st.name<<endl;
    out << "Roll No : "<<st.roll<<endl;
    out << "Age : "<< st.age<<endl;
    out << "Branch : "<<st.branch<<endl;
    return out;
}
istream &operator >> (istream &input, Student &st)
{
    cout << "\nEnter The Name Of The Student : ";
    input>>st.name;
    cout<<"\nEnter The Roll No. : ";
    input>>st.roll;
    cout<<"\nEnter The Age : ";
    input>>st.age;
    cout<<"\nEnter The Branch Of The Student : ";
    input>>st.branch;
    return input;
}
int main()
{
    Student S1;
    cout<<"Operator overload"<<endl;
    cin >> S1;
    cout<< S1;
    return 0;
}


And why I can't able to enter a full name the program is ended after typing a blank space.

and Why can't use char in the constructor

1
2
3
4
5
6
7
Student()
    {
        name= "INVALID NAME";
        roll= 0;
        age = 0;
        branch= "INVALID";
    }


when i write like this it gives the following error:

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
#include<iostream>
#include<string>
using namespace std;
class Student
{
    char name[40];
    int roll;
    int age;
    char branch[40];
public:
    Student()
    {
        name= "INVALID NAME";
        roll= 0;
        age = 0;
        branch= "INVALID";
    }
    friend ostream &operator <<( ostream &out, Student &st);
    friend istream &operator >> (istream &input, Student &st);
};
ostream &operator <<( ostream &out, Student &st)
{
    cout << "Details Of The Student: "<<endl;
    out << "NAME : "<<st.name<<endl;
    out << "Roll No : "<<st.roll<<endl;
    out << "Age : "<< st.age<<endl;
    out << "Branch : "<<st.branch<<endl;
    return out;
}
istream &operator >> (istream &input, Student &st)
{
    cout << "\nEnter The Name Of The Student : ";
    input>>st.name;
    cout<<"\nEnter The Roll No. : ";
    input>>st.roll;
    cout<<"\nEnter The Age : ";
    input>>st.age;
    cout<<"\nEnter The Branch Of The Student : ";
    input>>st.branch;
    return input;
}
int main()
{
    Student S1;
    cout<<"Operator overload"<<endl;
    cin >> S1;
    cout<< S1;
    return 0;
}



F:\RESHEARCH\CPP\aa2.cpp||In constructor 'Student::Student()':|
F:\RESHEARCH\CPP\aa2.cpp|13|error: incompatible types in assignment of 'const char [13]' to 'char [40]'|
F:\RESHEARCH\CPP\aa2.cpp|16|error: incompatible types in assignment of 'const char [8]' to 'char [40]'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
How can I write this without friend function?

If you don't want to use a friend function you'll first need to create some public functions to provide access to the private data. Then use these functions in the overloaded operators that are not part of the class.
can you tell me why i m getting the error.
Why are you using C-strings instead of std::strings?

Do you realize that you can't use assignment with C-strings?

Topic archived. No new replies allowed.