OOPs (Unit-2)
OOPs (Unit-2)
void get( int one, int two=2, int three=3);
void get(int one, int two, int three)
{
cout <<“One =“<
void main(void)
{
int *ptr;
ptr=new int(10);
cout<<*ptr;
delete ptr;
ae is will-allocate memory for an int(eger) having initial value
10, pointed by the ptr pointer.
Memory space for arrays is allocated as shown below:
ptr=new DataType [x];
Here, .
e ptr and DataType have the same meaning as above.
e xis the number of elements and C is a constant.
Ex.
/fExample Program in C++
#include
void main(void)
{
int *ptr, size;; (41)
cin>>size;
ptr=new int[size]:
/arrays are freed-up like this
delete []ptr:
} ;
Freeing-Up Allocated Memory:
Unlike static variables, C++ will not free-up the memory allocated
through dynamic allocation. Its your duty to free them up. delete
keyword is used to free-up the allocated memory.
delete ptr;
Arrays are deleted in a slightly different manner as shown below:
delete []ptr;
It’s easy to forget to free the allocated memory because C++
compiler won’t inform you that you are doing this. It’s your job and
should always be done. :
Now let me show you an example of dynamic memory allocation
in action: -
//Example Program in C++
#include
void main(void)
{ ee
a EHOW many PH. Numbers do you wish to enter:”;
cin>>size; .
\ptr=new int[size];//allocate memory
//imput ph. numbers
for (int i=0;i>ptr[i];
output ph. numbers ie
cout<<“\n\n\n PH. NOs. are\n”s
i=0;iYou might also like
- Lecture 6 - Object Oriented Programming
No ratings yetLecture 6 - Object Oriented Programming72 pages - Notes - CSE2001 - OOP - With - C++ - Unit II
No ratings yetNotes - CSE2001 - OOP - With - C++ - Unit II22 pages - Data Security Enhancement in Cloud Computing by Proposing A Dke Encryption Protocol
No ratings yetData Security Enhancement in Cloud Computing by Proposing A Dke Encryption Protocol75 pages - Ec-204 Data Structures & Object Oriented Programming: Dr. Waqar Shahid LE Arshia Arif
No ratings yetEc-204 Data Structures & Object Oriented Programming: Dr. Waqar Shahid LE Arshia Arif16 pages - Uni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On
No ratings yetUni T-I:Arrays: Ani NT R Oduct I Ont Oc++Cl Ass: Abst R Act I On15 pages - Constructor, Destructors and Inheritance
No ratings yetConstructor, Destructors and Inheritance29 pages - C++ Tutorials: Introduction To The Object-Oriented Approach
No ratings yetC++ Tutorials: Introduction To The Object-Oriented Approach24 pages - Object Oriented Programming Using C++: (Access Specifiers)
No ratings yetObject Oriented Programming Using C++: (Access Specifiers)17 pages - Features of Oops Are The Following:: Objects
No ratings yetFeatures of Oops Are The Following:: Objects23 pages - UNIT-2: Classes and Object, Dynamic Constructor & Destructor BCA-2 Sem
No ratings yetUNIT-2: Classes and Object, Dynamic Constructor & Destructor BCA-2 Sem40 pages - Unit - Ii: Class and Object Encapsulation and Data Abstraction
No ratings yetUnit - Ii: Class and Object Encapsulation and Data Abstraction37 pages