0% found this document useful (0 votes)
21 views14 pages

C++ Lecture-27

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views14 pages

C++ Lecture-27

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

01

02

LECTURE 27
04
Today’s Agenda
01 Creating dynamic objects

02 Array of dynamic objects

03 Dynamic constructor and dynamic destructor

05

05
Creating Dynamic Objects

Types of Objects

Static Object Dynamic Object


(Created at Compile Time) (Created or allocated at run time)
Creating Dynamic Objects

class Box int main()


{ {
private: Box B; Static Object
int l, b, h; B.get();
public: B.show();
void get() return 0;
{ } B
cout<<"Enter l, b, h: "; 10
l
cin>>l>>b>>h;
} b 20
void show() h 30
{ Destructor call
cout<<l<<", "<<b<<", "<<h<<endl; will be done 1000
} here
}; Will live inside
stack area
Output

Enter l, b, h: 10 20 30
10, 20, 30

Process returned 0 (0x0) execution time : 3.753 s


Press any key to continue.
Creating Dynamic Objects

class Box int main()


{ {
private: Destruction Box * p ;
int l, b, h;
will be done
p = new Box; Dynamic Object
public: p->get();
void get() here p->show();
{ delete p;
cout<<"Enter l, b, h: "; return 0; l 10
cin>>l>>b>>h; }
b 20
}
void show() h 30
{
2000
cout<<l<<", "<<b<<", "<<h<<endl;
} No destructor Will live inside
}; call will be 2000 heap area
done
p
Output

Enter l, b, h: 10 20 30
10, 20, 30

Process returned 0 (0x0) execution time : 3.085 s


Press any key to continue.
Array of Dynamic Objects

class Box int main()


{ {
private: int n;
int l, b, h; Box * p;
public: cout<<"Enter Size?: ";
void get() cin>>n;
{ p = new Box[n];
cout<<"Enter l, b, h: "; if(p == 0)
cin>>l>>b>>h; {
} cout<<"Memory insufficient";
void show() return 1; Failure
{ }
cout<<l<<", "<<b<<", "<<h<<endl; for(int i = 0; i < n; i++)
} (p + i)->get();//or p[i].get();
}; for(int i = 0; i < n; i++)
(p + i)->show();//p[i].show();
delete []p;
return 0;
}
Output

Enter Size?: 2
Enter l, b, h: 10 20 30
Enter l, b, h: 5 5 5
10, 20, 30
5, 5, 5

Process returned 0 (0x0) execution time : 10.962 s


Press any key to continue.
Types of Constructors

 Types of Constructors

1. Default Constructor

2. Default Copy Constructor

3. Non Parametrized Constructor

4. Parametrized Constructor

5. Copy Constructor

6. Default Parametrized Constructor

7. Dynamic Constructor
Dynamic Constructor and Dynamic Destructor

 In C++, a constructor is called DYNAMIC CONSTRUCTOR if it allocates memory for any class member
dynamically. In other words, we can say that, if inside the body of constructor we have used the keyword
"new" for allocating memory dynamically for a class member, then this constructor is called as DY-
NAMIC CONSTRUCTOR.

 For Example
The class MyArray, which we discussed in the previous session had a constructor which was using
"new" for creating "dynamic integer array". Thus that constructor will be called as DYNAMIC
CONSTRUCTOR.

Similarly, if we have a destructor in our class which uses the keyword "delete" to dynamically destroy any
member of the class, then such a destructor will be called as DYNAMIC DESTRUCTOR.

 For Example: The Destructor used in the class MyArray for destroying the dynamic array will be called as
Dynamic Destructor
Example

#include <iostream> Person::Person(int a, char * q) int main()


#include <cstring> { {
age = a; Person * ptr;
using namespace std; Dynamic int x = strlen(q); ptr = new Person(23, "Amit");
constructor p = new char[x + 1]; if(ptr == 0)
class Person strcpy(p, q); {
{ } cout<<"Insufficient memory";
private: return 1;
int age; void Person::show() }
char * p; { ptr->show(); Dynamic
public: cout<<age<<", "<<p; delete ptr; Object
Person(int, char *); } return 0;
void show(); }
~Person(); Person::~Person()
}; { age 23 ‘A’ ‘m’ ‘i’ ‘t’ ‘\0’
Dynamic delete []p;
destructor } ptr p 2000 2000
3000 3000 Dynamic array
Output

23, Amit
Process returned 0 (0x0) execution time : 0.075 s
Press any key to continue.
End of Lecture 27
For any queries mail us @: [email protected]
Call us @ : 0755-4271659, 7879165533

Thank you

You might also like