Miscellaneous c++ Issues

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 ..😡

Replies

  • sarveshgupta
    sarveshgupta
    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.
  • Manish Goyal
    Manish Goyal
    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
  • Morningdot Hablu
    Morningdot Hablu
    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.
  • gaurav.bhorkar
    gaurav.bhorkar
    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.
  • thechamp
    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
  • alkakumari
    alkakumari
    Difference between object and variable
  • alkakumari
    alkakumari
    Role of iostream.h and iomanip.h
  • Deepika Bansal
    Deepika Bansal
    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...
  • Sahithi Pallavi
    Sahithi Pallavi
    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.


  • Sahithi Pallavi
    Sahithi Pallavi
    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.
  • Sachin Jain
    Sachin Jain
    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.

You are reading an archived discussion.

Related Posts

Questions like these are normal in common competitive exams. They use just school level mathematics but used to measure someone's analyzing, reasoning & inferring capabilities. So give your head some...
:smile😁o prison buses have emergency exits? When lightning strikes the ocean why don't all the fish die? Do you yawn in your sleep? Why are dogs noses always wet? Do...
In winter season, when we cover ourself with a blanket, we feel hot. But when we cover ourself with with 2 blankets we feel much hotter. What's the reason?😁
I heard about the starting the website. If I developed a site using j2ee, what are the other things do I need to setup? How can I increase the security...
Sometimes I want to change the title as we editing the post, but it is restricted. how about giving rights to ceans to edit the title of the thread