CrazyEngineers
  • Miscellaneous c++ Issues

    Manish Goyal

    Manish Goyal

    @manish-r2Hoep
    Updated: Oct 22, 2024
    Views: 824
    Well all of the c++ programmers know about it's well known feature operator overloading but can anyone explain me.
    1:-Why we can't overload ternary operator,sizeof(),.(dot) operator?why?
    2:-what is difference between endl and /n ?as both accomplish same task?
    what is difference?
    Please explain with some solid reason...google doesnot satisfy me..so I hope you guys can explain me ..😡
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • sarveshgupta

    MemberJan 8, 2010

    goyal for the second question newline character is \n not /n and I think both the solve the same purpose of inserting a new line but then there is a minor difference in the way they do that endl does what '\n' does but also calls flush() for the stream.

    This makes it a little less efficient than '\n'.

    \n came from C syntax and endl was added to C++ as a new way to do the job.

    Correct me if I am wrong. According to me it will not bother the program if you use any of these.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 8, 2010

    Thanks sarvesh..I think you are right actually these questions are asked by my training teacher of java..but nobody knows answers of these questions..
    what about first question?Please explain if anyone knows
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberFeb 3, 2010

    goyal420
    Well all of the c++ programmers know about it's well known feature operator overloading but can anyone explain me.
    1:-Why we can't overload ternary operator,sizeof(),.(dot) operator?why?
    2:-what is difference between endl and /n ?as both accomplish same task?
    what is difference?
    Please explain with some solid reason...google doesnot satisfy me..so I hope you guys can explain me ..😡

    dear goyal,
    Sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it. Consider:
    X a[10];
    X* p = &a[3];
    X* q = &a[3];
    p++; // p points to a[4]
    // thus the integer value of p must be
    // sizeof(X) larger than the integer value of q
    Thus, sizeof(X) could not be given a new and different meaning by the programmer without violating basic language rules.
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberFeb 3, 2010

    You cannot overload them because the language doesn't allow you to do so.

    That is also the reason for the fact that we cannot create our own operators.
    Are you sure? This action cannot be undone.
    Cancel
  • thechamp

    MemberFeb 27, 2010

    some of my queries in C/C++

    1) during input, user can enter endlessly until he presses ENTER KEY, i want to limit user's input character just like in GUIs. he can enter only 'x' characters for input.

    2) simmilarly i want to limit the data type he wants to enter. for example if i am asking his age and he tries to enter characters instead of integers then character keys should not work. only numeric keys should work.

    3) when i ask user to enter an integer, if he enters string or character my program clashes and end abruptly. how can i keep check on it, such that an error message is shown instead of program clashing.

    please someone help
    Are you sure? This action cannot be undone.
    Cancel
  • alkakumari

    MemberDec 24, 2010

    Difference between object and variable
    Are you sure? This action cannot be undone.
    Cancel
  • alkakumari

    MemberDec 24, 2010

    Role of iostream.h and iomanip.h
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberDec 24, 2010

    alkakumari
    Difference between object and variable
    An object is the class instance that models some real world object like a book or a car. Object has some data members and some member functions. Data members specify the properties of the object (eg name, the number of pages, author etc for a book object) and member functions specify the operations that can be applied on that object (eg we can read the number of pages or the author of a particular book).

    While a variable is the name given to a particular memory location that stores some value. This stored value can be referred using the variable name. In simple words: variable is a token of C++ which may be holding different values at different stages of execution...
    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberDec 25, 2010

    alkakumari
    Difference between object and variable
    To say simply,

    A variable can be considered as a name given to the location in memory. It is able to hold different values during the execution of a program.

    An Object is a collection of variables and associated functions of type class. These are run time entities in an Object Oriented Program.


    Are you sure? This action cannot be undone.
    Cancel
  • Sahithi Pallavi

    MemberDec 25, 2010

    alkakumari
    Role of iostream.h and iomanip.h
    These are Header files used in our program to access the pre-defined functions.
    iostream.h is added to perform input/output operations in our program.
    iomanip.h is added to perform manipulations in our program.
    Are you sure? This action cannot be undone.
    Cancel
  • Sachin Jain

    MemberDec 27, 2010

    thechamp
    some of my queries in C/C++

    1) during input, user can enter endlessly until he presses ENTER KEY, i want to limit user's input character just like in GUIs. he can enter only 'x' characters for input.

    2) simmilarly i want to limit the data type he wants to enter. for example if i am asking his age and he tries to enter characters instead of integers then character keys should not work. only numeric keys should work.

    3) when i ask user to enter an integer, if he enters string or character my program clashes and end abruptly. how can i keep check on it, such that an error message is shown instead of program clashing.

    please someone help
    Solution for problem number 1:-
    use the method fgets.
    Syntax
    char *fgets(char *str,int size,FILE *file)
    Normally this function is used for reading from file but we can read from keyboard also using stdin (refering it as a file)
    Working
    This method takes size-1 characters from the user and leaves 1 for null character.
    No matter how long the user enters a string it will store only size-1 characters in your str character array.
    This is a very good method to avoid buffer overflow.
    Problem
    If the user enters string of lesser size as you specified in the function then it also stores '\n' in your character array so you have to manually delete that.

    Problem 2:-
    I have not heard of this. So sorry in this case i may not help.

    problem 3:-
    You can always put a check on what user has entered.
    You are expecting an integer and user has entered a string.
    Simply always take input in the form of string and check if all characters lie between 48 and 57 (ASCII valued of 0 and 9)
    And if it is true then convert your string in integer.
    else print an error message.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register