What Is Meant By Constructor In C++

What Is Meant By Constructor In C++

What Is Meant By Constructor In C++


what is meant by constructor in c++



What Is Meant By Constructor In C++


     Class name and function name should be same is called constructor
1. Default constructor
2. Parametrized constructor
3. Multiple constructor

Default constructor

#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
arun()
{
cout<<"Enter the name and age is :"<<endl;
cin>>name>>age;
}
void putdata()
{
cout<<"The given name is :"<<name<<endl;
cout<<"The given name is :"<<age<<endl;
}
};
void main()
{
clrscr();
arun a;
a.putdata();
getch();
}

Parametrized constructor

#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
int c;
arun(int a,int b)
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
c=a+b;
}
void putdata()
{
cout<<"The given name is"<<name<<endl;
cout<<"The given age is"<<age<<endl;
cout<<"The given value of c is:"<<c<<endl;
}
};
void main()
{
clrscr();
arun a(100,200);
a.putdata();
getch();
}

Multiple constructor


#include<iostream.h>
#include<conio.h>
class arun
{
public:
char name [20];
int age;
int c;
arun()
{
cout<<"Enter the name and age is:"<<endl;
cin>>name>>age;
}
arun(int a,int b)
{
c=a+b;
}
void putdata()
{
cout<<"The given name is:"<<name<<endl;
cout<<"The given age is:"<<age<<endl;
}
void putdata()
{
cout<<"The value of c is:"<<c<<endl;
}
};
void main()
{
clrscr();
arun a;
arun a1(100,200);
a.putdata();
a1.putdata1();
getch();
}

Post a Comment

0 Comments