constructor: In C++ constructors are similar to Java. Constructor doesn't have return type and there can be more than one constructors. If default constructor is not specified compiler will generate one. If any constructor is specified (constructor with 1 or more arguments, you need to specify 0 argument constructor explicitly).
Note: In C++ default value of the variable is garbage unlike in Java where default value is 0. Also note that default value of any static variable is 0 in C++.
this pointer: It is similar to this pointer in java. It is accessible in all member functions in class.
new and delete: new should be used to create and object if it needs to be on heap. new operator not only allocates memory for the object it also calls it's constructor. In that sense new is better than malloc of C which just allocates memory for the block size we request. Similarly, delete calls destructor along with de-allocating memory. As object is created on heap, it remains there until explicitly deleted using delete. Using delete on NULL is safe. As new and delete are operators they can also be overloaded. As with destructor we can have only one version of delete per class.
destructor: C++ has concept called destructor which is called when object goes out of scope. There can be only one destructor per class in other words destructor cannot be over loaded.
Example:
class sample {
private:
int i;
public:
sample() { } // 0 - argument constructor
sample(int num) { // 1 - argument constructor
i = num;
}
~sample() { } // destructor
};
Note: In C++ default value of the variable is garbage unlike in Java where default value is 0. Also note that default value of any static variable is 0 in C++.
this pointer: It is similar to this pointer in java. It is accessible in all member functions in class.
Example:
class Sample {
public:
Sample(int i) {
this->i = i;
}
void display() {
cout <<>i;
}
private:
int i;
};
new and delete: new should be used to create and object if it needs to be on heap. new operator not only allocates memory for the object it also calls it's constructor. In that sense new is better than malloc of C which just allocates memory for the block size we request. Similarly, delete calls destructor along with de-allocating memory. As object is created on heap, it remains there until explicitly deleted using delete. Using delete on NULL is safe. As new and delete are operators they can also be overloaded. As with destructor we can have only one version of delete per class.
Example:
int *p = new int;
Sample *sample = new Sample;
int *p1 = new int[10];
delete p;
delete sample;
delete[] p1; // delete[] is called to delete array, delete p would have leaked memory.
new.h has method set_new_handler which is called if call to new fails. set_new_handler can be used to handle heap exhaustion condition.
Example:
set_new_handler(heapfull);
...
...
void heapfull() {
cout << "heap full";
}resolution operator (::) : Resolution operator helps in resolving conflict between local and global variable. It also helps in resolving class's static variable and class's method
int i = 10; // Global variable. Never use it.
class sample {
private:
static int state; // static variable declaration
public:
void method(); // class method declaration
};
// Compiler will throw an error if line below is not included. Static variables in class must be defined outside.
int sample::state = 0; // static variable definition. though variable is private you have to define it outside
// class method definition
void sample::method() {
}
int main() {
int i = 20;
cout << ::i; // Will print 10 for global i;
}
Above code also explains how to use static variable inside the class and how to define method outside class declaration.
No comments:
Post a Comment