Can you guys help me fill out my C++ exam review? Overloaded operators: 1. basic
ID: 3925395 • Letter: C
Question
Can you guys help me fill out my C++ exam review?
Overloaded operators:
1. basic facts (precedence, associativity,number of operands, etc.)
-Precedence of the operator cannot be changed
-associativity stays the same
-# of operands can’t be changed
-Can’t create new operators
2. operators that cannot be overloaded
member and non-member overloaded operators
3. when can an overloaded operator be a member function
4. when can an overloaded operator NOT be a member function
[Can you explain the following concept?]
function calls that are equivalent
a + b is equivalent to a.operator+(b) for a member function
a + b is equivalent to operator+(a, b) for a non-member function
Friend functions:
5. syntax to make a function a friend of a class
6. pros and cons of making a function a friend of a class
'this' pointer:
7. what it is
8. how it's used to 'connect' the object to the function being called
9. when it is useful
Thank you so much in advance.
Explanation / Answer
While working on operator overloading, you need to keep in mind the below points :
-> Only built-in operators can be overloaded. New operators can not be created.
->Arity of the operators cannot be changed.
->Precedence and associativity of the operators cannot be changed.
->Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.
-> Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators must be defined as member functions
->Except the operators [ = [] () ] all other operators can be either member functions or a non member functions.
-> Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
I hope this will clear your doubts regarding operator overloading.
Friend functions :
class Chegg
{
double question;
public:
double answer;
friend void printQuestio( Chegg chegg );
void setAnswer( double answer);
};
This porinter :
‘this’ pointer is a constant pointer that holds the memory address of the current object. It is an implicit object for all the non-static members of a class. It is not present in case of static member functions as the static member functions or variable don't need any object to access.
This pointer can be useful in two cases :
1> when you need to return the reference to the calling object(current object).
2> when your local variable name is same as member variable name. for eg.
class Chegg
{
int a;
void setValue(int a)
{
this.a=a;
}
}
-------------------------------------------------------------------------------------------------------------------------
I would suggest you to first do hands on with the code. Feel free to reach out if you have any doubt. keep learning :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.