Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Unary Operators, Prefix, Postfix, Behavior of predefined types, Member function definition, Friend function definition, Type Conversion, Types of conversions, Drawback of conversions are main points of this lecture.
Typology: Slides
1 / 49
defined types:
int x = 1, y = 2; cout << y++ << endl; cout << y;
int y = 2; y++++; // Error
y++ = x; // Error
Post-increment ++ returns by value, hence an error while assignment
int y = 2; cout << ++y << endl; cout << y << endl;
int x = 2, y = 2; ++++y; cout << y; ++y = x; cout << y;
4 2
Pre-increment ++ returns by reference, hence NOT an error
class Complex{
double real, img;
public: ...
Complex & operator ++ ();
// friend Complex & operator // ++(Complex &);
} Docsity.com
Complex & Complex::operator++(){
real = real + 1;
return * this;
}
Complex & operator ++ (Complex & h){
h.real += 1;
return h;
}
Complex h1, h2, h3;
++h1;
++h1 = h2 + ++h3;
whether it is a pre-increment
or a post-increment?
Member function with 1 dummy int argument
OR
Non-member function with two arguments
class Complex{
...
Complex operator ++ (int);
// friend Complex operator // ++(const Complex &, int);
}
Complex Complex::operator ++ (int){
*complex t = this;
real += 1;
return t;
}
Complex operator ++ (const
Complex & h, int){
complex t = h;
h.real += 1;
return t;
}
Complex h1, h2, h3;
h1++;
h3++ = h2 + h3++; // Error…
operator -- is implemented in
exactly the same way
int f = 0.021;
double g = 34;
// type float is automatically converted // into int. Compiler only issues a // warning…
int g = (int)0.0210;
double h = double(35);
// type float is explicitly converted // (casted) into int. Not even a warning // is issued now…
C style type casting
class String{ ... public:
String(int a); char * GetStringPtr()const; };