C++ Tutorial 12 : Operator Overloading & File I/O

  Рет қаралды 35,190

Derek Banas

Derek Banas

Күн бұрын

Пікірлер: 106
@mehdibm1591
@mehdibm1591 5 жыл бұрын
For greater operator (>) you can easily use the less operator ( (const Box& box2){ return (!(*this < box2)) }
@vyasramankulangara8804
@vyasramankulangara8804 6 жыл бұрын
I finally understand why operator overloading is useful. Thank you so much!
@derekbanas
@derekbanas 6 жыл бұрын
I'm happy I could help :)
@tylerburgess8543
@tylerburgess8543 6 жыл бұрын
I think I'm starting to sound like a broken record recommending your channel :D Great work!
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) I greatly appreciate that!
@electronix259
@electronix259 5 жыл бұрын
Man, for some reason overloaded operators were always escaping me during classes, just couldn't understand it. Now I realize how easy they are.
@derekbanas
@derekbanas 5 жыл бұрын
Happy I could help :)
@jorgeherrera1074
@jorgeherrera1074 3 жыл бұрын
Love the explanation, however, better to return a reference to the object than to return void on these overloaded operators. If you return a reference then you can do things like ObjA = ObjB = ObjC or you could also do things such as ObjA < ObjB < ObjC and you can evaluate to true/false in a conditional whereas return of void will be a compiler error
@luis622
@luis622 6 жыл бұрын
Hey, I'm a big fan of your videos! Thank you for all the time and effort on your part. I'm a big fan of using cerr for example as part of the else statement if the file is not open. Maybe it would be a good idea to implement for your future videos to make your viewers aware of that function. Thanks again!
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) Yes I'm not finished covering File I/O. This was but a sample
@andrewman178
@andrewman178 6 жыл бұрын
I would love to see more videos full of problems and solutions using these kinds of concepts. Great work on this series so far!
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) I'll always do my best to include at least one problem.
@clbgrmn
@clbgrmn 6 жыл бұрын
For your overload of the increment operator, you're returning a pointer to a box. But, the return statement returns the data at 'this'. Isn't 'this' already a pointer to a box? So using '*' on this gives you a box object. Seems like it should be returning 'this' rather than '*this', right?
@AdnaanBheda
@AdnaanBheda 6 жыл бұрын
Why are we using ios_base instead of ios ? Can you please elaborate on that? Thanks for the awesome videos Derek, I'm loving every series of yours :)
@samangharatchorlou6572
@samangharatchorlou6572 6 жыл бұрын
Hey Derek, great videos, so much information! Have you done a video on best coding practices? Like how to structure your code, how to best write a function (i.e. how long or specific to a task should it be), when to and not to use classes (should I wrap all my functions in a class, like you did for the Battle problem, and why?), file names and stuctures etc. Great stuff :).
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) Sure check out my object oriented design, and refactoring tutorials
@ethanmiller5223
@ethanmiller5223 5 жыл бұрын
This video is literally amazing thanks
@derekbanas
@derekbanas 5 жыл бұрын
Thank you very much :)
@WiseLyonsPowerfulLyo
@WiseLyonsPowerfulLyo 6 жыл бұрын
I should start by saying that I am not particularly well versed in programming yet. You should do an in-depth video(s) about larger abstractions of object oriented systems within an OO codebase. I mean it would be really helpful to understand the theory, reasoning, and coded examples of how systems of objects and code are *rightly* supposed to work together. The only good book that I've found that particularly teaches about this is "Refactoring in Large Software Projects" by Martin Lippert and Stephen Roock. If you want I can send a bunch of photographs of the pages to you. Let me know. Another few aspects, that come to mind, that I think should be adequately addressed are things like naming conventions; and composition, dependencies, and relationships of classes. I would like to know a broad and in-depth theory and methodology of why certain objects are used within specific classes and not others. Also, why certain relationships exists as *opposed* to other possible relationships. Now, keep in mind that most classes are not modeled after real world things but are, apparently(I guess), mainly modeled to create order and structure and logically participate in larger systems of order and structure. In fact, if a larger system should need to change then a class may need to either change to logically work with and accommodate the larger system OR the larger system, itself, may need to HEAVILY be reworked *around* the class - to accommodate such class. I want to understand, among other things, how classes and systems of classes should be written and as well as the broader view of how these systems should be thought of. Thanks.
@derekbanas
@derekbanas 6 жыл бұрын
I plan on covering object oriented design. I've already covered it with Java here kzbin.info/www/bejne/nHu6Z2iNpJx8fqs and I covered refactoring here kzbin.info/www/bejne/rJm8fGamebqcoM0
@WiseLyonsPowerfulLyo
@WiseLyonsPowerfulLyo 6 жыл бұрын
*@Derek Banas* *-"I plan on covering object oriented design. I've already covered it with Java here"* Yeah I've downloaded and partially watched your previous vids on OO design and refactoring. So far they are very good btw. I believe your previous vids only partially covered the concepts of larger abstractions of OO *system* designs and it's theory . What, it seems, you originally lectured more about were the concepts of *smaller* abstractions of OO *system* designs and it's theory. As a matter of fact, I think that you could/should have gone into more detail about smaller system design theory *also* , and that would have been SO much more awesome! If you even know how, that is..... So I guess I'm saying it would be SO helpful and awesome to lecture about both BIGGER and smaller OO *system* design and it's theory. I've skimmed through a few books on refactoring and the book, "Refactoring in Large Software Projects" by Martin Lippert and Stephen Roock is NOT very focused on refactoring merely the pieces of systems but rather the entirety of the systems themselves. Other refactoring books focus much more on the smaller aspects, and, yes, I know the larger is made out of the smaller. If you have a Safari account then maybe you could check out this book. I'm telling you - it ain't the same. Also, if you want I will send you many android photos from the book.
@xavierlesperance6156
@xavierlesperance6156 3 жыл бұрын
Hey Derek, thanks so much for the videos they are great. I have a quick question about overloading the increment and decrement operators, typically ++i and i++ give the same results but in the case of the Box class only the ++box was overloaded how would you go about overloading box++ as well?
@gimmegaming5345
@gimmegaming5345 4 жыл бұрын
I''m hoping you can help me understand but why does "operator const char*(){}" not have a return type like other functions? Is it something to do with being a pointer?
@tmjromao
@tmjromao 6 жыл бұрын
Thanks for the video. Well explained and quite informative.
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :)
@danigol6663
@danigol6663 5 жыл бұрын
awesome man. its helping me a lot.I would have Liked it thousand times if possible. Thank you
@derekbanas
@derekbanas 5 жыл бұрын
Happy I could help :)
@CSSuccessGamer
@CSSuccessGamer 5 жыл бұрын
nice tutorial. I'm trying to figure out how to use the file i/o to load file and save it into a binary tree and stream out a 20 question ai game. And then save the new tree data into another file when game finishes. Do you have any related tutorials for doing this?
@exodus8814
@exodus8814 6 жыл бұрын
Hey Derek :) Thanks for your hard work. Just a question, have you ever thought of contributing to the filed of Computer Science?
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) In what way? I think I already kind of do that. My tutorials are shown in numerous colleges all over the world from what I've been told.
@exodus8814
@exodus8814 6 жыл бұрын
Derek Banas Awesome to hear that! I meant in a way similar to what other scientists did before. For example like Alan Turing, Dennis Ritchie etc...
@derekbanas
@derekbanas 6 жыл бұрын
I'm not that smart. The thing I seem to be better at then many people is that I can learn things quickly and then present that info in an understandable way. I definitely don't have the brain of a Turing though.
@exodus8814
@exodus8814 6 жыл бұрын
Derek Banas Okay Derek. Do you think it's a bad idea to wish to contribute to a science field? Is it possible by people like me or others? Are people really born with supreme intellectual abilities or they just work hard to achieve it? Can anyone contribute to science?
@exodus8814
@exodus8814 6 жыл бұрын
Derek Banas yes Derek?
@Zen-lf7zr
@Zen-lf7zr 6 жыл бұрын
Hey Derek, I didn't understand at 6:19 how you were able to use boxString without declaring it anywhere, what kind of object is it? Also could you tell me more about how to differentiate what return type each overloaded operator should have. Thanks in advance :)
@Conorkc86
@Conorkc86 3 жыл бұрын
I bet you haven't included #include try that and let me know if it worked
@Zen-lf7zr
@Zen-lf7zr 3 жыл бұрын
@@Conorkc86 I don't remember haha but I'm decent at C/C++ now thanks to these videos.
@rapidreaders7741
@rapidreaders7741 5 жыл бұрын
Is there a way to use format specifiers when writing doubles to a file?
@yankomirov4290
@yankomirov4290 4 жыл бұрын
Great tutorial, thank you very much!
@derekbanas
@derekbanas 4 жыл бұрын
Happy I could help :)
@nadrogiranihibiskus5186
@nadrogiranihibiskus5186 6 жыл бұрын
is this operator overloading stuff really usefull in programming ?
@derekbanas
@derekbanas 6 жыл бұрын
Yes very much so
@brodoswaggins5387
@brodoswaggins5387 4 жыл бұрын
the getline() in the readFromFile's while loop return an error saying 'no instance of getline matches the argument list'. would there be an updated code for VS2019? std::cin seems to work fine lol
@XxD4rkAssassinxX
@XxD4rkAssassinxX 6 жыл бұрын
Coming from Java, I find operator overloading really amazing
@derekbanas
@derekbanas 6 жыл бұрын
C++ is the language that can do everything :)
@AndrewDChristie
@AndrewDChristie 3 жыл бұрын
Awesome Video
@derekbanas
@derekbanas 3 жыл бұрын
Thank you very much :)
@mcalex4747
@mcalex4747 4 жыл бұрын
Tell me please when EXACTLY do you use this "&" as a REF, I was never able to understand it.
@propkillerr
@propkillerr 4 жыл бұрын
When you need to pass an argument as a reference. It's not really hard to understand.
@berndrichter1549
@berndrichter1549 6 жыл бұрын
Derek im a fan of your videos I would be happy if you can do same kind of tutorials for javascript and arduino
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) Arduino will start soon. I'll see about JS
@techtalktoe9758
@techtalktoe9758 4 жыл бұрын
Derek has a great JavaScript playlist. Check it out!
@ozbity3243
@ozbity3243 6 жыл бұрын
Will you be making a nim Lang tutorial? Plus love your videos
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) I will definitely if I get requests
@chickerinoradio6617
@chickerinoradio6617 4 жыл бұрын
what if i want to open a file in a different directory?
@luckyluuk8788
@luckyluuk8788 5 жыл бұрын
Hi Derek, quick question: how would one call the overloaded const char* operator? I can't seem to get it working.
@Pensive_117
@Pensive_117 5 жыл бұрын
He doesn't like answering people. I knew him in college; he's extremely anti-social.
@coder0079
@coder0079 4 жыл бұрын
I had this same question, and it took me a bit of searching to solve it. What I believe he is doing with the const char* operator is called a "User Defined Conversion." An internet search should make it clear.
@bluewatermelon3253
@bluewatermelon3253 4 жыл бұрын
With that line, he defines how conversion to const char* will behave. For example, if you had code like this: Box green(10,20,40); int number = green; You would get an error because the class (green) cannot be converted to int (number) But, if you define operator overloading in class like this operator int(){ return Box->length + Box->height + Box->width } And that would mean int number = green that variable "number" stores sum of box "green" dimensions. So, when you have to do datatype conversion you just use the following syntax: operator datatype_name(){ return //what you want to return with datatype_name } Hope this clarifies what are you confused about
@ronanpatrick5763
@ronanpatrick5763 3 жыл бұрын
Thanks
@derekbanas
@derekbanas 3 жыл бұрын
Happy to help :)
@Stormhatt
@Stormhatt 5 жыл бұрын
Why does boxString have to be stored outside of the operator const char*() function?
@AntonioAstorino
@AntonioAstorino 5 жыл бұрын
const char* is a pointer. If you don't store boxString, as soon as you return from the function operator const char*() the pointer will be deleted. Just try and see what happens :) Great question by the way. Not great answer though. Insist if I haven't been clear enough
@Stormhatt
@Stormhatt 5 жыл бұрын
@@AntonioAstorino Makes sense, thanks for the explanation :)
@AntonioAstorino
@AntonioAstorino 5 жыл бұрын
@@Stormhatt Any time!
@Gosei_Nakara
@Gosei_Nakara 6 жыл бұрын
I love you.
@derekbanas
@derekbanas 6 жыл бұрын
You're very kind :) Thank you
@MdmanProductions
@MdmanProductions 6 жыл бұрын
You should include LSEEK!!!
@braindeveloperdimensional5579
@braindeveloperdimensional5579 6 жыл бұрын
can you please make a tutorial on how to write a clean and correct code in C++ and makefile tutorial also, I am stuck with a C++ library. I can't compile it which I found on GitHub. Please Help!
@derekbanas
@derekbanas 6 жыл бұрын
I'll see what I can do
@souravmukherjee175
@souravmukherjee175 5 жыл бұрын
why you dont use using namespace std..
@derekbanas
@derekbanas 5 жыл бұрын
To avoid naming conflicts
@bhargav7476
@bhargav7476 6 жыл бұрын
Part 12 of ? How many parts left ?
@derekbanas
@derekbanas 6 жыл бұрын
Sorry, but I don't know. These aren't scripted out. I just will keep making them until I feel I've covered most everything
@bhargav7476
@bhargav7476 6 жыл бұрын
Derek Banas I guess I will keep watching then
@derekbanas
@derekbanas 6 жыл бұрын
I doubt I'll make that many. My videos tend to be rather long. I can cover just about everything in much less
@bhargav7476
@bhargav7476 6 жыл бұрын
Suyggyu 7 years ago
@bhargav7476
@bhargav7476 6 жыл бұрын
Derek Banas Helping me score better grades in my diploma. Thanks
@ronistiawan1130
@ronistiawan1130 6 жыл бұрын
when are you going to start making GUI?
@derekbanas
@derekbanas 6 жыл бұрын
Sorry I don't know. After I cover the core language
@braindeveloperdimensional5579
@braindeveloperdimensional5579 6 жыл бұрын
You mean GUI in C++?
@braindeveloperdimensional5579
@braindeveloperdimensional5579 6 жыл бұрын
Derek Banas If you are going to create a GUI series then please do it in any game programming library like :- OpenGL, SDL or Allegro5. Game programming libraries offer a lot more than gtk+, Qt or MyGUI. Please do it in game library if you could.
@derekbanas
@derekbanas 6 жыл бұрын
I haven't decided which I'll use yet
@exodus8814
@exodus8814 6 жыл бұрын
Could you answer my last question on my previous comment Derek?
@derekbanas
@derekbanas 6 жыл бұрын
Sorry, KZbin didn't send it to me. Yes just about anyone can achieve anything if they are motivated and have a good plan. I have met many what you'd call genius types. I think they accomplish so much because they all tend to have a strong focus on one subject. That is why they achieve so much, but also why other parts of their life are a mess. I think anyone can cultivate a strong enough focus on something they are passionate about. Look to the achievements of others and to entertainment to cultivate passion. Surround yourself with the best teachings from numerous sources. Then set aside time every day and you can do just about anything.
@shahidkhan0219
@shahidkhan0219 6 жыл бұрын
Super
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :)
@dx2002sg
@dx2002sg 3 жыл бұрын
Char *() is an c++ operator? interesting.
@2271masoud
@2271masoud 6 жыл бұрын
operator overloading and file I/O in nutshell. Thanks Derek
@derekbanas
@derekbanas 6 жыл бұрын
Thank you for watching :)
@braindeveloperdimensional5579
@braindeveloperdimensional5579 6 жыл бұрын
Hello falcon
@gfamily2781
@gfamily2781 6 жыл бұрын
Pls make complie to dll
@scriptingtutorials4060
@scriptingtutorials4060 6 жыл бұрын
can you make a tutorial on html5 games?'
@Alphabet_-_
@Alphabet_-_ 5 жыл бұрын
@shipilov23
@shipilov23 6 жыл бұрын
:)
@derekbanas
@derekbanas 6 жыл бұрын
:)
C++ Tutorial 13 : Advanced Functions
18:27
Derek Banas
Рет қаралды 67 М.
C++ Tutorial 11 : Polymorphism
18:48
Derek Banas
Рет қаралды 76 М.
How it feels when u walk through first class
00:52
Adam W
Рет қаралды 24 МЛН
إخفاء الطعام سرًا تحت الطاولة للتناول لاحقًا 😏🍽️
00:28
حرف إبداعية للمنزل في 5 دقائق
Рет қаралды 84 МЛН
НАШЛА ДЕНЬГИ🙀@VERONIKAborsch
00:38
МишАня
Рет қаралды 2,9 МЛН
C++ Tutorial 3 : Pointers & Functions
36:32
Derek Banas
Рет қаралды 70 М.
C++ Tutorial 2 : Conditionals, Arrays, Vectors, Strings, Loops
32:14
C++ Tutorial 10 : Object Oriented Programming
40:25
Derek Banas
Рет қаралды 241 М.
Python Vs Perl Programming - Which Is Easier?
4:20
Dr Python
Рет қаралды 3,3 М.
Lazarus IDE Tools to Help Boost Your Productivity
9:58
The Silver Coder
Рет қаралды 122
C++ Tutorial 7 : Solving Problems
26:03
Derek Banas
Рет қаралды 26 М.
C++ Tutorial 19 : C++ Regular Expressions
27:29
Derek Banas
Рет қаралды 83 М.
C++ Tutorial 14 : Templates & Iterators
22:13
Derek Banas
Рет қаралды 74 М.
How it feels when u walk through first class
00:52
Adam W
Рет қаралды 24 МЛН