Buckys C++ Programming Tutorials – 51 – More on Operator Overloading

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

48 responses to “Buckys C++ Programming Tutorials – 51 – More on Operator Overloading”

  1. johan banan Avatar

    How do I make the same operator but for division?

  2. Simon zapati Avatar

    In the main you include the header file for sally #include "sally.h" . However, you didn't #include "sally.cpp" in main. So how does main know how to use all the functions from the sally class? Thanks I am a beginner.

  3. Jeremy Young Avatar

    Why do you use parentheses when you write return (brandNew); ? Just curious if that has a different effect or purpose here.

  4. Call Me Daniels Avatar

    did you not need to overload the = assignment sign aswell ??

  5. Kyonhiss Avatar

    for the most recent versions of c++, you must declare the overloading as:
    Sally Sally::operator+(Sally const& aso) in order to work

  6. Simonmonkey z Avatar

    This is a very good tutorial, thank you!!!

  7. RAVE Avatar

    Diff between old languages and new ones.

    Newer : learning to drive a car.
    Older : building a car and learning to drive it

  8. kegan Avatar

    This doesn't confuse me, as I already know .add, HOWEVER this would confuse a LOT of people who have religiously followed the series from 0 prior knowledge because he says "Well you already know you can do this .add," yada yada etc.. My question is: How would the average beginner viewer of these videos "already know" what you haven't even taught them? I've watched every video so far and not once prior to this video did he mention .add, so NO they WOULDN'T know that they can already do this. He's a great teacher, but leaves canyons-wide gaps in the basic learner's knowledge.

    For anyone looking for another teacher who goes to great lengths to encapsule (;D) the entirety of c++ in your learning, I recommend Cave of Programming (For a sample, watch his video on Pointers and BE AMAZED at how much you just learned).

    Learning Lad also helped me understand some stuff. 🙂

  9. Jordan Fischer Avatar

    Nice job on choosing Fibonacci numbers!

  10. Aspect Avatar

    pretty cool huh

  11. evantheking Avatar

    I think the confusion most people will have :

    – passing one object (b) instead of passing a and b
    – this part " brandnNew.num = num + aso.num"
    the single key "num" that is not referenced through an object like ( a.num)

    so somehow this "num" is "a.num" and the operator+ function just know that and add it ro aso.num ?

  12. Samuel Lee Avatar

    So when does the "Sally Sally::operator+(Sally aso) {}" function get called?
    when you do c= a+b inside the main() fucntion?

  13. Jack Speedicut Avatar

    good stuff. it not an easy concept to grasp

  14. Alex Boyev Avatar

    why return(brandnew) with brackets ?

  15. Me Me Avatar

    Can someone please help me understand this: at 4:08 he says it took the current object a, how does the program take a as the current object even though we created b and c after that ?
    Thanks!

  16. iUpdateyou Avatar

    i don't understand these class tutorials

  17. MM Avatar

    The code below returns a very large negative value, anyone know why?

    #include <iostream>
    using namespace std;

    #define WAIT system("pause")

    class Apple {
    public:
    Apple(int);
    Apple();
    Apple operator+(Apple);
    int num;
    };

    Apple::Apple(int a) {
    a = num;
    }

    Apple::Apple() {
    }

    Apple Apple::operator+(Apple b){
    Apple brandNew;
    brandNew.num = num + b.num;
    return (brandNew);
    }

    int main() {
    Apple a1(2);
    Apple a2(2);
    Apple c;
    c = a1 + a2;
    cout << "brandnew " << c.num << endl;
    WAIT;
    }

  18. MM Avatar

    Could (Sally aso) be replaced with (aso1 aso2)? Why is the first one sally but the second one can be anything?

  19. Geeky Gamer Avatar

    I have written the code in one file

    #include<iostream>
    #include<conio.h>
    #include<cstdlib>
    #include<string>
    using namespace std;
    class sally{
    public:
    int num;
    sally(int a){
    num=a;
    }
    sally(){
    }
    sally operator+(sally aso){
    sally brandnew;
    brandnew.num=num+aso.num;
    return (brandnew);
    }
    };
    int main(){
    sally a(23);
    sally b(34);
    sally c=a+b;
    cout<<c.num;
    }

  20. Alan Medina Avatar

    So using operator overloading we can perform mathematical operations on objects.
    -Cool

  21. Dimitris Atha Avatar

    What is going to happen if I try to add two simple integers, for example? The way the operator works, after overloading it, affects just the objects or the simple processes, as well?

  22. zaryab rizvi Avatar

    can anybody tell why are we creating constructors in overloading

  23. Michael Cowan Avatar

    I'm actually disappointed you don't use this more. I thought this concept was really cool. I like the idea of having more control over the default operations in my programs.

  24. clint lemire Avatar

    dam, this guy really objectifies women.

  25. Guangxu Wang Avatar

    after i understood what operator overloading is with other ways, i come back to watch this video again. must to say, this video is really confusing and bad organised.

  26. Hernan Mendez Avatar

    it took me a lot of .cpp files to understand this

  27. Sharkolan Avatar

    Yo overloading has been eating my lunch in my Data Structures course. Thank you so much!

  28. TheRealPekka Avatar

    In Sally.cpp we have the function Sally(int a), which just assigns num to the value a. But why num gets automatically get assigned to a?, i.e. why this is enough to have a.num = 34?..I thought we have to put it "a.num = a", but this is wrong..why?

  29. Karen Olmos Avatar

    it's simple enough

  30. Nfwf Nqweqed Avatar

    I want a child from you, bucky

  31. len 114602 Avatar

    I got an unresolved external error

  32. sniperkitty 1400xx Avatar

    worst video in the series. what a shit example u used lmao

  33. East Palace Avatar

    Just out of curiosity, why wouldn't it work if I say " Sally c() " when declaring c?

  34. Daedric Shinu v2.0 Avatar

    I hope this makes things clearer for people. I wrote similar but different code from Bucky's: _____________________________#include <iostream>
    using namespace std;class Sally {
      public:
     
      int num;  // Class variable.
     
      Sally(int x) {  // Class constructor.
          num = x;
         
      }
      Sally operator+ (Sally aso) {  // Not a class constructor. Sally is the return type.
          aso.num = num + aso.num; // In other words, b.num = a.num + b.num. Note: .num is a object variable, not a function!
    return aso; // Returns Sally object.       
      }
    };int main()
    {
       Sally a = 5; //initializes through the constructor.
       Sally b = 6;  
       a = (a + b);  // the a object equals the returned object of the operator overload function.
     cout << a.num << endl;
    }Note: Doing it like this is discouraged. Had I declared more variables, num2, num3, for example, object a's variables would have taken on all the values of the b's variables, even without prompting them to do so by the overload function.

  35. Alex Tan Avatar

    brandNew . num = num + aso . num ; So, compiler automatically substitute c= a + b ? ; And one(b) should has object and one (a) shouldn't?

  36. Alex Tan Avatar

    why should we add class name (sally) behind the function? Due to its function wanna to return an object?

  37. Jose Lopez Avatar

    If we initialized a newSally object in the operator function, how come we have to create a blank object in main?

  38. Sara Tokic Avatar

    Why do we write .num? It's not an object …?

  39. Mah Prog Avatar

    Is this thing even useful now? I see no sense in adding 2 numbers in such a roundabout way..

  40. kenneth 92 Avatar

    It took me awhile to understand it.

  41. Vishruit Kulshreshtha Avatar

    So is this '+' getting called from 'a'?

  42. Luke Avatar

    not sure if anyone still replies to these videos, but why didnt he need to overload the "=" operator? ie when he used c = a + b, how did the compiler already know what to do with "=" ?

  43. Modulus V Avatar

    Why do we return brandNew and not brandNew.num?

  44. VeldroN Avatar

    Why did he put brandNew in paranthesys when he typed return (brandNew) ?

  45. VeldroN Avatar

    Why did he put brandNew in paranthesys when he typed return (brandNew) ?

    Why was the a object equal to num ?
    Why was the b object equal to aso ? Is C++ so smart that it automatically sets them equal to the objects ?
    Why c was equal to brandNew?

  46. Gerald Joshua Avatar

    Question:
    so when we use this statement c= a+b, it means we call the member function operator+ that belongs to object a and pass object b as its parameter and at the end it will return a new object and assign it to object c. But, if change the order, c =b+a, then it means we call the member function operator+ that belongs to object b and pass object a as its parameter and at the end it will return a new object and assign it to object c. Am I right?

  47. mark nik Avatar

    hi Bucky can you explain the different between the postfix and prefix operators
    thnks

Leave a Reply

Your email address will not be published. Required fields are marked *