C++ : Constructors And Destructors 5

Question: What is constructor overloading?

Sol:
When we have two or more constructors in a program it is known as constructor overloading.

Note:

There can be a number of parameterized constructors in a class (i.e. parameterized constructors can be overloaded)

Example:
Class:student(roll,name,per)

We can have following parameterized constructors:
student(int r,char *n,float p);
student(int r);
student(char *n);
student(float p);
student(int r,char *n);
student(char *n,float p);
student(int r,float p);

Dynamic initialization

When values of the data element are entered at runtime by the user and then the data element are initialized by those values by passing them as parameters, then it is called as dynamic initialization.

Student s;
Student s(100,”abc”,10);
int r;
char n[20];
float a;
cout<<”Enter roll, name and age “;
cin>>r>>n>>a;
student s1(r,n,a); //dynamic initialization

Example Of Dynamic Initialization

Question:1
C++ program to declare a class named student with attributes as roll, name, and age. Initialize the data elements dynamically and display the details.
Sol:

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
//dynamic init
class student
{
private:
	int roll;
	char name[20];
	float age;
public:
	student();//default constructor
	student(int r,char *n,float a);//para cons
	student(student &s); //copy cons
	void read();
	void show();
};
student::student() //defa cons
{
  roll=0;
  name[0]='\0';
  age=0;
}
student::student(int r,char *n,float a)//para cons
{
  roll=r;
  strcpy(name,n);
  age=a;
}
//copy cons
student::student(student &s)
{
  roll=s.roll;
  strcpy(name,s.name);
  age=s.age;
}
void student::read()
{
  cout<<"Enter roll,name and age ";
  cin>>roll>>name>>age;
}
void student::show()
{
  cout<<"roll "<<roll<<" name "<<name<<" age "<<age<<endl;
}
int main()
{
  /* clrscr(); */
  student s1,s2(500,"Kapil",14);
  int rn;
  char na[20];
  float ag;
  cout<<"Enter roll,name and age ";
  cin>>rn>>na>>ag;
  student s3(rn,na,ag);//dynamic init
  s1.show();  s2.show();  s3.show();
  getch();
  return(0);
}
/* Output */

Enter roll,name and age 1002
Amit
12
roll 0 name  age 0
roll 500 name Kapil age 14
roll 1002 name Amit age 12

Question:2
C++ program to declare a class named bank with attributes as accno, name and bal. Initilize the data elements dynamically and display the details.
Sol:

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
//dynamic init
class bank
{
private:
	int accno;
	char name[20];
	float bal;
public:
	bank();//default constructor
	bank(int a,char *n,float b);//para cons
	bank(bank &s); //copy cons
	void read();
	void show();
};
bank::bank() //defa cons
{
  accno=0;
  name[0]='\0';
  bal=0;
}
bank::bank(int a,char *n,float b)//para cons
{
  accno=a;
  strcpy(name,n);
  bal=b;
}
//copy cons
bank::bank(bank &s)
{
  accno=s.accno;
  strcpy(name,s.name);
  bal=s.bal;
}
void bank::read()
{
  cout<<"Enter accno, name and balance ";
  cin>>accno>>name>>bal;
}
void bank::show()
{
  cout<<"accno "<<accno<<" name "<<name<<" bal "<<bal<<endl;
}
int main()
{
  /* clrscr(); */
  bank s1,s2(500,"Kapil",14);
  int ac;
  char na[20];
  float ba;
  cout<<"Enter accno, name and bal ";
  cin>>ac>>na>>ba;
  bank s3(ac,na,ba);//dynamic init
  s1.show();  s2.show();  s3.show();
  getch();
  return(0);
}
/* Output */

Enter accno, name and bal 1005
Kavita
85000
accno 0 name  bal 0
accno 500 name Kapil bal 14
accno 1005 name Kavita bal 85000