Code of Constructor

                      Constructor

Define Constructor:-

Types of Constructor:-

1Default Constructor:- Constructor that is no parameter are called default constructor.

#include<iostream>
#include<conio.h>
using namespace std;
class sum
{
int a,b,c;
public:
sum()
{
a=10; b=20;
}
void add()
{
c=a+b;
cout<<c;
}
};
main()
{
sum s1;
s1.add();
getch();
}

Output:- 30

2. Parameterized Constructor:-  A constructor that can take arguments are called parameterized constructor.

#include<iostream>
#include<conio.h>
using namespace std;
class read
{
int a,b;
public:
read(int m , int n)
{
a=m;
b=n;
}
void display()
{
cout<<a<<b;
}
};
main()
{
read r1(10,30);
r1.display();
getch();

}

output:- 10 30



Note:- If you use Dev C++ then copy this code.
If you use Turbo C++ then use below header file and copy whole code
#include<iostream.h>
#include<conio.h>
class ........
copy whole code.


Thank you


Comments

Popular posts from this blog

Addition of two matrix using Array

Multiplication of matrix using Array

Addition of two no. using Class