C++ Programming Tutorial 94 - Overloading Insertion and Extraction Operators

C++ Programming Tutorial 94 - Overloading Insertion and Extraction Operators

Caleb Curry

5 лет назад

23,623 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

bikramkeshari swain
bikramkeshari swain - 10.09.2023 16:25

please explain layman term in insertion and extraction overloading , kindly i can't understand .please listen my voice.

Ответить
Liza-Mari Wright
Liza-Mari Wright - 19.08.2023 14:54

How do we overload insertion and extraction operators as a member function inside a class? This explanation works wonderful, but when I try to put it as member function in a class, it says that there is no match for the operator <<

Ответить
Kzr_Lancelot_V2
Kzr_Lancelot_V2 - 04.04.2023 22:08

Have a question : why would i use this instead of adding a method to print out each object?

Ответить
Armand Duval
Armand Duval - 22.10.2022 16:09

Great video! Thanks a lot!

Ответить
Aung Shan Htay
Aung Shan Htay - 26.10.2021 17:33

Dear bro, I just did it the get line overload and it works. Thank you bro. I just started learning C++ by watching your tutorials. Yeahhhhh

istream& getline (istream &input,User &user) //extration operator syntax getline(note: reference at object)
{
getline(input,user.first_name);
getline(input,user.last_name);
return input;
}

Ответить
James O'Connor
James O'Connor - 18.09.2021 18:23

Great stuff

Ответить
eien
eien - 12.08.2021 07:18

i have a question, is there any way to return ostream without returning reference, i kno this is useless but i just wanna kno

Ответить
samter
samter - 03.08.2021 17:08

Extraction? More like extraction of ignorance, and replacement with knowledge! Thanks again so much for this wonderful tutorial series.

Ответить
North Trade
North Trade - 18.07.2021 05:19

I had to go over this a couple times, which was well worth it, as I walked away with an understanding of operator overloading, rather than to put it off until later. Yet another tool in the arsenal in the C++ language thanks to Caleb Curry's tutorial. Thank you, sir.

Ответить
maxim25o2
maxim25o2 - 03.07.2021 18:09

I see another example in my school,
as public member of class ___ void operator << ( ostream &);
and later in main program call just that obiect ___ cout << kogel ;
function class __ void kogel::operator << ( ostream &OS ){ OS << " example " << diameter ;}
Is not easier to just create instance in side of class?? Why everybody are creating overloaded ostream outside of class??
What is difference between your example and creating overloaded operator in side in class?

Ответить
Pier Francesco Peperoni
Pier Francesco Peperoni - 29.06.2021 22:02

This is gold.

Ответить
Kacper Bazan
Kacper Bazan - 13.06.2021 06:38

first vid that actually explains each of the keywords and symbols for the operator overload instead of just writing the overload with no further elaboration. <3

Ответить
Rooke Ron
Rooke Ron - 01.06.2021 22:06

This was actually less confusing than the previous video.

Ответить
Typical_YT
Typical_YT - 15.05.2021 08:36

For people who got the error "Too many parameters for this operator function"

Declare the function in the class as a friend, and then define the function outside of the class. Example:

class Position
{
public:
int x = 10;
int y = 20;


friend std::ostream& operator << (std::ostream& output, Position user);

private:

};

std::ostream& operator << (std::ostream& output, Position user)
{
output << "x" << user.x << "y" << user.y;
return output;
}

int main()
{

}

Ответить
joao.v
joao.v - 07.04.2021 02:38

Why can't we do copies of the ostream? I know it's bad practice but would it even be possible?

Ответить
Sajjad Jafari
Sajjad Jafari - 23.03.2021 11:42

You made it as easy as a piece of cake for me as a beginner.
liked and subscribed.

Ответить
Olliver Aira
Olliver Aira - 13.12.2020 19:22

Another way of doing the istream operator overload:

istream &operator >> (istream &input, User &user)
{
    cout << "What should " << user.first_name << user.last_name << "'s new first name be?";
    input >> user.first_name;
    cout << "What should " << user.first_name << user.last_name << "'s new last name be?";
    input >> user.last_name;
    cout << "What should " << user.first_name << user.last_name << "'s new status name be?";
    string temp_status;
    input >> temp_status;
    user.set_status(temp_status);
    return input;
}

Ответить
Varun Poondi
Varun Poondi - 16.10.2020 20:24

When I add in User into the operator overloading function parameter, I get the error "too many parameters for this operator function", but when I add it in a friend, this error goes away. Can someone explain why this is happening?

ostream& operator << (ostream& output, User user){
return output;
}

This is not compiling

Ответить
TAN ZENGYANG
TAN ZENGYANG - 27.08.2020 16:33

Actually you can use previous example:

Position y;
y.y = 5;
cout << "What the new value of Y"<<endl;
//Compiler know how to insert y value to y
cin >> y;
//y is updated to user input value
cout << y << endl;
really like your explanation

Ответить
Rasum
Rasum - 11.08.2020 11:58

I tried the input without returning anything (with a void type) and it worked. Is it supposed to be done like I did?

Ответить
Omer Ozhan
Omer Ozhan - 12.06.2020 13:46

This tutorial series is great I have been following along since the first tutorial and I learned a lot.

Ответить
Nguyễn Trọng Phúc
Nguyễn Trọng Phúc - 02.05.2020 15:55

thank your for this, I didn't really know what my teacher was teaching until this

Ответить
Ernest Hemingway
Ernest Hemingway - 08.09.2019 12:13

The second parameter of extraction overloading function is passed by reference.
Does this mean that the reference allows us to change an user object to another?

Ответить