Hello guys!!! welcome back. I am here with my new post related to constructors and their types. In previous post we learned inline and friend functions. So now in this post we will understand constructors. So here we go!!!!
First thing is that what is constructor? We know what is objects and class. When we define a variable, object is working on it and initialize it. Now question comes that why initialize ? Answer is that if the variables are not been initialized then they produce some garbage values and these values are taken for operation which may generate errors in result. So, for avoiding that type of variables we use special function constructors. It is the most useful function in the programming.
Here are some points, which we need to know while using of constructors :-
--> Constructor can automatically called when new object of this class created.
--> Constructor's name is the same name as the class name .
--> It should not have any return type.
--> It should declared in public.
--> It has own (default) arguments.
Now we are going to understand its types:
1) Default Constructors.
2) Parameterized constructors.
3) Copy Constructors.
4) Explicit Constructors.
5) Dynamic Constructors.
In this post we will discuss first two types. We will discuss remaining types in later posts !!!!
1) Default Constructor :-
#include<iostream.h>
#include<conio.h>
class image
{
private : int h,w;
public : image() /* constructor is defined
and its name is as same the class name*/
{
h=0;
w=0;
}
int area()
{
cout<<"value of h is"<<"\n";
cin>> h;
cout<<"value of w is"<<"\n";
cin>>w;
return(h*w);
}
};
void main()
{
image obj1; /* object created and values are initialized*/
clrscr();
cout<<"area is : "<<obj1.area()<<endl;
getch();
}
In default Constructor, we use default methods like we learned in c language (without any argument or without passing any parameter). So in this program, we direct initialized both values in constructor. In area we accept values from user and process done in return(h*w). And in main function we created object so it can initialize the variables. and after that we call that function(obj1.are()).
2) Parameterized Constructor :-
#include<iostream.h>
#include<conio.h>
class image
{
private : int h,w;
public : image(int a, int b) /*constructor*/
{
h=a;
w=b;
}
int area()
{
return (h*w);
}
};
void main()
{
image obj1(3,4);
cout<<"area is : "<<obj.area()<<endl;
getch();
}
In this constructor, we use another variables in argument section [image(int a ,int b)]. This is same as we learned call by value and reference in c language.
So this is it. in next post we will understand remaining three types of constructors.
Until then BYE BYE!!!
facebook: CLICK HERE!!!!!!!!!!
Twitter: @NewAgeInformers
Instagram: @new_age_informers_
First thing is that what is constructor? We know what is objects and class. When we define a variable, object is working on it and initialize it. Now question comes that why initialize ? Answer is that if the variables are not been initialized then they produce some garbage values and these values are taken for operation which may generate errors in result. So, for avoiding that type of variables we use special function constructors. It is the most useful function in the programming.
Here are some points, which we need to know while using of constructors :-
--> Constructor can automatically called when new object of this class created.
--> Constructor's name is the same name as the class name .
--> It should not have any return type.
--> It should declared in public.
--> It has own (default) arguments.
Now we are going to understand its types:
1) Default Constructors.
2) Parameterized constructors.
3) Copy Constructors.
4) Explicit Constructors.
5) Dynamic Constructors.
In this post we will discuss first two types. We will discuss remaining types in later posts !!!!
1) Default Constructor :-
#include<iostream.h>
#include<conio.h>
class image
{
private : int h,w;
public : image() /* constructor is defined
and its name is as same the class name*/
{
h=0;
w=0;
}
int area()
{
cout<<"value of h is"<<"\n";
cin>> h;
cout<<"value of w is"<<"\n";
cin>>w;
return(h*w);
}
};
void main()
{
image obj1; /* object created and values are initialized*/
clrscr();
cout<<"area is : "<<obj1.area()<<endl;
getch();
}
In default Constructor, we use default methods like we learned in c language (without any argument or without passing any parameter). So in this program, we direct initialized both values in constructor. In area we accept values from user and process done in return(h*w). And in main function we created object so it can initialize the variables. and after that we call that function(obj1.are()).
2) Parameterized Constructor :-
#include<iostream.h>
#include<conio.h>
class image
{
private : int h,w;
public : image(int a, int b) /*constructor*/
{
h=a;
w=b;
}
int area()
{
return (h*w);
}
};
void main()
{
image obj1(3,4);
cout<<"area is : "<<obj.area()<<endl;
getch();
}
In this constructor, we use another variables in argument section [image(int a ,int b)]. This is same as we learned call by value and reference in c language.
So this is it. in next post we will understand remaining three types of constructors.
Until then BYE BYE!!!
facebook: CLICK HERE!!!!!!!!!!
Twitter: @NewAgeInformers
Instagram: @new_age_informers_
Comments
Post a Comment