Dynamic Initialization Through Constructors

Object’s data members can be dynamically initialized during runtime, even after their creation. One advantage is that it supports different initialization formats using overloaded constructors. This provides flexibility of using different forms of data at runtime depending upon the user’s need.

//Object with different name pattern
#include<iostream.h>
#include<string.h>
class name
{
char first[15]; //first name
char middle[15]; //middle name
char last[15]; //last name
public:
name() //constructor 0
{
first[0]=middle[0]=last[0]='\0';
}
name (char*firstname) //constructor 1
{
strcpy (first,firstname);
middle[0]=last[0]='\0';
}
name (char*firstname,char*middle name)
{
strcpy (first,firstname);
strcpy (middle,middle name);
last[0] = '\0';
}
name (Char*firstname,char*middlename,char*lastname)
{
strcpy(first,firstname);
strcpy(middle,middlename);
strcpy(last,lastname);
}
void show()
{
cout<<"firstname"<<first<<end/;
if(middle[0])
cout<<"middlename"<<middle<<end/;
if(last[0])
cout<<"lastname"<<last<<end/;
}
};
void main()
{
name n1,n2,n3; //constructor 0
n1 = name ("Ramu"); //constructor 1
n2 = name ("Somu", "S")
n3 = name ("Raj", "A","B");
n1.Show ();
n2.Show ();
n3.Show ();
};

Read More Topics
Characteristics of constructor in C++
Disk file I/O with stream in C++
Pointer to pointer in C++
C++ string

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply