Dynamic Constructors in C++ | Dynamic Constructors Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books

Dynamic Constructors in C++ | Dynamic Constructors Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books

Dynamic Constructors in C++ | Dynamic Constructors Example | C++ Programming Language | C++ Programming Tutorial | C++ Programming Examples | C++ Programming Books |

Dynamic Constructors

A constructor apportioning dynamic memory utilizing new administrator is called as powerful constructors.



#include <bits/stdc++.h>

utilizing namespace sexually transmitted disease;

class dyncons




{

string name;

int length;

open :



dyncons()

{

length = 0;

name = new char[length + 1];

}

dyncons(string s)

{

length = s.length();

name = new char[length + 1];

name=s;

}

void operation()

{

cout <<“\n”<< name<< “\n”<< “Number of characters in the string is ” <<name.length();

}

};

int fundamental ()

{

string first = “Hi !”;

dyncons name1(first);

dyncons name2(“Example”);

dyncons name3(“Quora”);

name1.op();

name2.op();

name3.op();

return 0;

}

/Output

The yield for the code will be.

Yield:

Hi !

Number of characters in the string is 7

Illustration

Number of characters in the string is 7

Quora

Number of characters in the string is 5

Clarification:

In the above code, class dyncons (Dynamic Constructor) is made and the default constructor assigns length as 0 and the char* has length 0.

Be that as it may, when the parameterised constructor is called by passing strings like “Hi !”, “Illustration”, and “Quora”, the items name1, name2 and name3 get values 7, 7 and 5 as their lengths and correspondingly the *name for every protest gets the particular sizes.

Dynamic Constructor

Dynamic constructor is utilized to designate the memory to the items at the run time.

Memory is designated at run time with the assistance of “new” administrator.

By utilizing this constructor, we can progressively introduce the items.

Illustration:

#include <iostream.h>

#include <conio.h>

class dyncons

{

int * p;

open:

dyncons()

{

p=new int;

*p=10;

}

dyncons(int v)

{

p=new int;

*p=v;

}

int dis()

{ return(*p);

}

};

void principle()

{

clrscr();

dyncons o, o1(9);

cout<<“The estimation of question o’s p is:”;

cout<<o.dis();

cout<<“\nThe estimation of question 01’s p is:”<<o1.dis();

getch();

}

Yield:

The estimation of question o’s p is:10

The estimation of question 01’s p is:9

 

Thanks for reading this post and don’t forget to share it and keep visits for more updates



About Ashok.cse

Hi, I'm currently studying for the Bachelor of Technology In Computer Science from Rajasthan Technical University. I am the web developer with 3+ years of experience. my range of developer services including creating the design for new apps and web platforms or building UI design for mobile, tablets or desktops. making UX designs wireframes and layouts concepts and redesigns websites are adapting to mobile and responsive design. I have worked with many clients. I am working professionally with WordPress And Adobe tools.

View all posts by Ashok.cse →

Leave a Reply

Your email address will not be published. Required fields are marked *