static: Static variable has default value 0. Static variable in the class needs to be defined out side class. Class can have static method as in Java which if defined out side will not have static word with it.
Method cannot be a both static and const.
Example:
class Sample {
private:
static int index;
public:
static void display();
};
int Sample::index = 10;
void Sample::display() { // Note no static here
cout <<>
}
Error prone features:
Features that C++ provides but we are better off without them as they can lead to many unwanted side-effects. If we use them it can lead to un-intentional calls being made and can lead to errors which can be very difficult to trace. Following are the list of error prone features. (We are not going in details about it).
- Operator overloading
- Default arguments in functions
- Copy constructor
- = operator
- Exception handling
explicit: C++ compiler is quite pro-active in trying to determine programmer's intention. It tries to find the closest match for performing an operation. If it doesn't fine one it will use nearest one.
Example:
class Sample {
public:
Sample(int i) {
num = i;
}
private:
int num;
};
...
Sample s = 10;
s = 20;
In above code, compiler will first look for overloaded = operator. On failure to find that it will look for one argument constructor with int. This is called implicit conversion.
If we want to make sure that particular constructor should be invoked explicitly used then add explict before contructor.
No comments:
Post a Comment