|
|
|
Memory Management Using New and Delete The statement int a[100]; reserves memory for 100 integer elements. This very simple approach for data storage, but they have very serious drawback : the memory get allocated before running & arrays requires a continuous memory block. We could not use variables to allocate memory-using array. Even though this good enough when we know number of elements are to be used. In many cases we don’t know much memory we need until run time. C++ provides us approach to obtain blocks of memory using new operator.
The new operator returns a pointer that points the section of memory just large enough to hold the required elements. The delete operator returns the system whatever memory was pointed by variable
#include #include
void main() { char *str = “Vishwanet Computers Pvt. Ltd. Kolhapur “; int I = strlen(str); char *ptr; ptr = new char[I+1]; //allocate memory & returns pointer strcpy(ptr, str); cout<< str <delete ptr; // de-allocate memory }
Pointers to Objects Pointers can point to objects. When we don’t know how many objects are to be created, we have to use new operator to create objects at runtime.
#include class stud { private: char *nm; int total; public: void getdata() { cout<< “Enter Name : “; cin>>nm; cout<< “Enter Total : “; cin>>total; } void dispdata() { cout<<”\n Name : “<< nm; cout<<”\n Total : “<} };
void main() { stud *s; s-> getdata() s-> dispdata() }
The ‘this’ pointer Pointer named as this which points to the object itself. A this pointer refers to an object that currently invokes a member function. For example, the function call a.show() will set the pointer ‘this’ to the address of the object ‘a’. Example
#include
class thispointer { private: char a[10]; public: void disp() { cout<<”The address of object is : “< } };
void main() { thispointer t1,t2; t1.disp(); t2.disp(); }
Virtual Functions Virtual means existing in effect but not in reality. This is an important for polymorphism, i.e. giving different meaning to the same thing. Function & Operator overloading are the examples of compile time polymorphism. The overloaded member functions are selected for invoking by matching arguments, both by type & number of arguments. The compiler knows these functions a particular call at compile time this is called as early or static binding. It means the object is bound to its functions call at compile time. Virtual function is an example of run time polymorphism. This type of polymorphism the function call is invoked at run time. It is called as Dynamic Binding. Dynamic binding requires use of pointers to objects. Example:
class base { public: virtual void show() //virtual function { cout<<”In base”; } }; class derived1 : public base { public: void show() { cout<<”\n Derived 1”; } };
class derived2 : public base { public: void show() { cout<<"\n Derived 2"; } };
void main() { base *b; derived1 d1; derived2 d2; b = &d1; b->show(); b = &d2; b->show(); }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|