P. 1
DYNMEMALL

DYNMEMALL

|Views: 0|Likes:
Published by Revathi Durai

More info:

Published by: Revathi Durai on Apr 25, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

04/25/2011

pdf

text

original

.

. Ex : int ptr=new int[10].of elements]. delete new . 2) For array variable datatype pointer=new datatype[no. Ex : int ptr=new int. . new 2.Dynamic Memory Allocation : It required memory allocation during run time (execution time) Two operators: 1.single variable ex : delete ptr. . delete operator : To destroy the variable space which has been created by using the new dynamically syntax : delete pointer.used to create heap memory space for an object of a class syntax : 1) For single variable datatype pointer=new datatype. delete []pointer.for an array delete []ptr.

i++) { cin>>*(ptr+i).i<5. } . } for(i=0. for(i=0.i++) { cout<<*(ptr+i)<<endl. #include<iostream.i<5.Create a memory for 10 integers using NEW operator and destroy using the delete operator. cout<<endl<<"Enter 5 numbers". } delete []ptr.i.h> void main() { int *ptr. ptr=new int[5].

show().h> class book { private : int bno. } -> Used to access the member of the class through pointer variable . public : void get(). } }. b2=new book. cout<<bno<<author<<bname.bname[20]. void show(). b2->get(). delete b2. ~book() { cout<<"Destructor called"<<endl.*b2.get(). } void main() { book b1. cin>>bno>>author>>bname. b2->show(). b1.Write a C++ program to creates an object of class book and destroy it #include<iostream. b1. char author[15]. } void book::show() { cout<<endl. void book::get(){ cout<<endl<<"enter the data".

str=new char[length+1]. void main() { String s1="Lafore".s).h> #include<string. s1. } void display() { cout<<str. } }.display(). cout<<"\n Destructor called". // +1 -> '\0' strcpy(str. public : String(char *s) { length=strlen(s). } ~String() { delete str.Class USING new Operator #include<iostream. cout<<length. } OUTPUT Lafore 6 Destructor called .h> int length. class String { private : char *str.

It is a special pointer that exists for a class while a non-static Member function is executing. It is used to access the address of the Class itself and may return data items to the caller. .

int). void show().20).int b) { 10 this->length=l. } void rectangle ::show() { cout<<this->length<<this->breadth .set_data(10. OUTPUT this->breadth=b. public : } void set_data(int.breadth.#include<iostream.show(). }. void rectangle::set_data(int l. } 20 . int length. private : r.h> void main() class rectangle { { rectangle r. r.

getch(). public: void b() { a=10. } this-> a value is 10 (*this). OUTPUT void main() { ptr p. } }.a value is³ <<(*this).h> class ptr { private: int a.this pointer Example #include<iostream.b().h> #include<conio.a value is 10 . p. cout<<"\n (*this).a. clrscr(). cout<<"this-> a value is"<<this->a.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->