Overloading Unary Operator in C++ | C++ Tutorial | Mr. Kishore

Поділитися
Вставка
  • Опубліковано 13 гру 2024

КОМЕНТАРІ • 69

  • @NareshIT
    @NareshIT  5 років тому +3

    Please Comment, Subscribe and Click Bell🔔🔔🔔 Icon for More Updates. To learn software course from our experts please register here for online training: goo.gl/HIB0wL

    • @gloryofgames1607
      @gloryofgames1607 5 років тому +1

      //I get error
      #include
      using namespace std;
      class Abc
      {
      public:
      int a;
      ABC()
      {
      a=0;
      }
      void input()
      {
      couta;
      }
      void show()
      {
      cout

    • @4.otechnologysaikumar829
      @4.otechnologysaikumar829 4 роки тому

      @@gloryofgames1607 me to broo...I have also error...

  • @stockmarket_matrix
    @stockmarket_matrix 6 років тому +22

    The given example is post increment so the below is the way to write it:
    #include
    using namespace std;
    class test
    int a;
    public:
    {
    test()
    {
    a=20;
    }
    void operator++()
    {
    ++a;
    }
    void operator--()
    {
    --a;
    }
    void operator++(int )
    {
    a++;
    }
    void operator--(int )
    {
    a--;
    }
    void show()
    {
    cout

  • @ajaykharat8689
    @ajaykharat8689 6 років тому +4

    Generally operators are designed to work with pre-defined datatype. In order to use operator with user-defined datatype operator are overloaded.
    In operator overloading:-
    a. With Binary operator 2 objects are required(one implicit and other explicit)
    b. with Unary operator 1 object is required(one implicit on LHS)
    void operator ++()
    {
    a++;
    cout

  • @codingwithmx
    @codingwithmx 5 років тому +31

    It was two errors in this code:
    you need to correct:
    void operator ++ (int){a++;} and void operator -- (int){a--;}
    because the unary operator has only one implicit and it also an integer.

    • @JohnDoe-ej6vm
      @JohnDoe-ej6vm 5 років тому +1

      thanks a lot . but plz exmplain why we face error , and how parameter is working in this function ?

    • @shrishojha6490
      @shrishojha6490 4 роки тому +1

      thanks , what sir is doing would be right if he would have been using pre increment operator

    • @troopIine
      @troopIine 3 роки тому +1

      correct that's right

    • @HarishKumarC007
      @HarishKumarC007 3 роки тому +1

      Wonderful, thanks a lot

    • @ekambaramsainikhil9301
      @ekambaramsainikhil9301 3 роки тому +1

      we need to call the function as operator objectname eg: ++t where ++ is the operator and t is the object name

  • @ahadsun1188
    @ahadsun1188 4 роки тому +1

    i have never seen any videos with this much details,hats off you Sir.....Respect 1000

  • @arjumandayoub8483
    @arjumandayoub8483 Рік тому +2

    the reason for error can be explained as :
    The code you provided contains a compilation error because the `++` operator is overloaded as a prefix operator, but it is being used as a postfix operator in the `main` function.
    In C++, the `++` operator can be overloaded as both a prefix and a postfix operator. The prefix version of the `++` operator is declared by defining a member function with the signature `void operator++()`, while the postfix version is declared by defining a member function with the signature `void operator++(int)`.
    In your code, you have defined the prefix version of the `++` operator by declaring the member function `void operator++()`. However, in the `main` function, you are using the `++` operator as a postfix operator when you write `t++;`. This causes a compilation error because there is no matching overload for the postfix version of the `++` operator.
    To fix this error, you can either change the usage of the `++` operator in the `main` function to use it as a prefix operator (`++t;`) or define an overload for the postfix version of the `++` operator in your class.
    Is there anything else you would like to know?

  • @mohitsaud2071
    @mohitsaud2071 3 роки тому +4

    //Actually the program for the post fix is //
    #include
    using namespace std;
    class Test
    {
    int a;
    public:
    Test()
    {
    a=0;
    }
    Test( int n)
    {
    a=n;
    }
    void operator ++(int)
    {
    a++;
    }
    void operator --(int)
    {
    a--;
    }
    void show()
    {
    cout

  • @yuvrajsharma2134
    @yuvrajsharma2134 4 роки тому +2

    Your teaching and content is very good. Really feels good to be on your channel.

  • @shashankgupta6700
    @shashankgupta6700 3 роки тому

    Sir you are the best .....I have seen so many videos of opeator overloading but did not get the concept and then i watched your video all concepts are clear.Thank you so much sir

  • @tayyabshahzad2747
    @tayyabshahzad2747 3 роки тому

    you are a good instructor.
    t++ //Error because compiler treat it as binary operator.
    if one wants to run this , he/she must use it as binary operator.
    #include
    using namespace std;
    class fun{
    public:
    int a;
    fun(int x):a(x)
    {}
    void operator - (int i)
    {
    a=a + i;
    cout

  • @arslanbegmyradov3048
    @arslanbegmyradov3048 4 роки тому +2

    whithout ERROR - Program should be:
    class Sample
    {
    int a;
    public:
    Sample() { //default constructor
    a = 0;
    }
    void operator++() {
    a++; // implicit
    }
    void operator--() {
    a- -; // implicit
    }
    void printValue() {
    cout

  • @iniyaviji2796
    @iniyaviji2796 Рік тому

    Lovely sir thank u so much

  • @darshkatare8896
    @darshkatare8896 8 місяців тому

    sir I scored good marks in practical exam because of you love u sir❤❤❤❤

  • @jaypatel9392
    @jaypatel9392 4 роки тому

    Thank u sir for providing your valuable knowledge ❤️

  • @lincycarolinesagayam6726
    @lincycarolinesagayam6726 4 роки тому

    It makes us feel easy

  • @sushantgupta4757
    @sushantgupta4757 4 роки тому

    Thanku so much sir 😍😍

  • @aishwarya2860
    @aishwarya2860 6 років тому

    Excellent sir

  • @gopialavala8618
    @gopialavala8618 2 роки тому

    sir in the place of a=0, with out using the zero it can be treated as the zero initial value

  • @amitkumargupta6722
    @amitkumargupta6722 4 роки тому

    here is the code for this program.....
    #include
    using namespace std;
    class test
    {
    int a;
    public:
    test()
    {
    a=0;
    }
    void operator ++ (int)
    {
    ++a;
    }
    void operator -- (int)
    {
    --a;
    }
    void show()
    {
    cout

  • @harshavardhantp8308
    @harshavardhantp8308 4 роки тому

    When we are defining the constructor.
    Do we have to define default constructor or will it define itself

  • @bs_it4975
    @bs_it4975 3 роки тому

    Thanks sir!

  • @ernisrisatyavennela3523
    @ernisrisatyavennela3523 4 роки тому

    can't we use pre increment operator for overloading?????

  • @vaishnavidubeyit_0309
    @vaishnavidubeyit_0309 6 років тому

    Sir in unary operator if we have to increment & decrement in two different values means suppose a++ & b-- so how we can use it .

  • @sumanthkavikondala1036
    @sumanthkavikondala1036 6 років тому +11

    This code wont work, as you had overloaded the operator for prefix(pre-increment) and you were using postfix to call the overloaded function by ("t++") but instead "++t" would work. Where as postfix overloading is having a different syntax to do so by passing a dummy int as a argument. Sir please educate the correct information instead misleading the students. I understand that you are trying to educate students for free here but i feel its better not to teach at all instead of passing false information.

    • @gamuchiraimureyani281
      @gamuchiraimureyani281 6 років тому +3

      actually the misleading person is you

    • @sumanthkavikondala1036
      @sumanthkavikondala1036 6 років тому +4

      A concept is a standard which cant be manipulated by any individual apart from the creator. Because of people like you students are getting wrong information and they carry the same to others.

    • @smarttube1141
      @smarttube1141 6 років тому

      Yeah Correct bro...

    • @adityadubey408
      @adityadubey408 6 років тому

      yeah you are correct I found out the same error

    • @adityadubey408
      @adityadubey408 6 років тому

      shut the f*** up !

  • @al-hadees6236
    @al-hadees6236 3 роки тому

    there is small mistake in void main instead of t++ and t-- the correct way is ++t and --t

  • @unknownchannel1924
    @unknownchannel1924 3 роки тому

    just change a++ to ++a and a-- to --a similarly for t also.

  • @ashishmishra-yu1jr
    @ashishmishra-yu1jr 4 роки тому

    Sir here pre increment and pre decrement work not post increment/decrement

  • @4.otechnologysaikumar829
    @4.otechnologysaikumar829 4 роки тому

    Here postfix oparator using to become error sir......then output become when the prefix oparating using.........whyyy this happend sirr

  • @dharmadevi9817
    @dharmadevi9817 7 років тому

    pls upload all java concepts.rly nice class

  • @ravikishore1743
    @ravikishore1743 5 років тому

    Why & is present in the declaration of the Operator ++ overloading in the blow code sir?
    Complex &Complex::operator ++(){ }
    Please suggest.
    Also i saw some code where * is used instead of & like belo
    Complex *Complex::operator ++() { }
    Please explain.

    • @venkatp9381
      @venkatp9381 4 роки тому

      & refers to reference, so a reference of class complex is the return type. If i t is *, it is pointer to class complex

  • @rakshith3458
    @rakshith3458 7 років тому

    Can we refine "+" operator to subtract two operands instead of addition using operator overloading ?

  • @shubhampandey1073
    @shubhampandey1073 4 роки тому

    sir can we overload the += operator or not

  • @viswanathgupta7406
    @viswanathgupta7406 7 років тому +3

    load sessions on function overload sir

  • @shubhampandey1073
    @shubhampandey1073 4 роки тому +1

    sir this programme show an error in ide

    • @HarishKumarC007
      @HarishKumarC007 3 роки тому +2

      So in comment section, they have changed please go once, even I faced same thing

  • @rahmanullahshirzad7865
    @rahmanullahshirzad7865 4 роки тому

    sir this is towice run but error what is problem?

  • @arjunmenonkandanat6328
    @arjunmenonkandanat6328 3 роки тому

    what he is telling is wrong. This program will be of pre increment operator. NOT post increment operator
    in main() , he has to write ++t , instead of t++