Type of constructor
Type of constructor and their program and what is difference between them.
Different type of constructor:
1) Default constructor
2)parameter constructor
3)copy constructor
4)conversion constructor
5)Explicit constructor
1.Default constructor:
- A constructor which has no argument (access no parameter) is known as Default argument.
- It is also known as no argument constructor.
đŸ‘‰coding using Default argument:
#include<iostream>
using namespace std;
class ADD
{
int x,y,z;
public:
ADD(); //using class name to create function declaration it is called default argument
void sum();
void display();
};
ADD :: ADD() //In this function has no argument
{
x=10;
y=20;
}
void ADD :: sum()
{
z=x+y;
}
void ADD :: display()
{
cout<<"addition is:\n"<<z;
}
int main()
{
ADD a;
a.sum();
a.display();
return 0;
}
OUTPUT:
2.parameter constructor:
The constructor which take some argument is known as parameter constructor.
đŸ‘‰coding using parameter constructor:
#include<iostream>
using namespace std;
class ADD
{
int x,y,z;
public:
ADD(int,int);
void sum();
void display();
};
ADD :: ADD(int a,int b)
{
x=a;
y=b;
}
void ADD :: sum()
{
z=x+y;
}
void ADD :: display()
{
cout<<"addition is:\n"<<z;
}
int main()
{
ADD a(10,20);
a.sum();
a.display();
return 0;
}
output:
3.copy constructor:
A constructor which takes reference to its own class as argument is known as copy constructor.
đŸ‘‰coding:
#include<iostream>
using namespace std;
class Example {
// Member Variable Declaration
int a, b;
public:
//Normal Constructor with Argument
Example(int x, int y) {
// Assign Values In Constructor
a = x;
b = y;
cout << "\nIm Constructor";
}
//Copy Constructor with Obj Argument
Example(const Example& obj) {
// Assign Values In Constructor
a = obj.a;
b = obj.b;
cout << "\nIm Copy Constructor";
}
void Display() {
cout << "\nValues :" << a << "\t" << b;
}
};
int main() {
//Normal Constructor Invoked
Example Object(10, 20);
//Copy Constructor Invoked - Method 1
Example Object2(Object);
//Copy Constructor Invoked - Method 2
Example Object3 = Object;
Object.Display();
Object2.Display();
Object3.Display();
// Wait For Output Screen
return 0;
}
output:
4.Explicit constructor:
In explicit,also same as parameter constructor but difference is function call in this manner,
ADD a=ADD(10,20);
đŸ‘‰Coding:
#include<iostream>
using namespace std;
class ADD
{
int x,y,z;
public:
ADD(int,int);
void sum();
void display();
};
ADD :: ADD(int a,int b)
{
x=a;
y=b;
}
void ADD :: sum()
{
z=x+y;
}
void ADD :: display()
{
cout<<"addition is:\n"<<z;
}
int main()
{
ADD a=ADD(10,20);
a.sum();
a.display();
return 0;
}
output:
same as above output.
5.conversion constructor:
Conversion constructors are used for type conversion wherein the type of argument passed is converted to the class type.
What is a conversion constructor?
It is a constructor that accepts one argument of a different type.
Advantages of inline functions.
A function when defined as INLINE, the code from the function definition is directly copied into the code of the calling function. A function is defined as inline using the 'inline' keyword as shown below :
inline float add(float i, float j) { return i + j};
Advantages : - It avoids the overhead of calling the actual function. This is because the compiler performs and inline expansion which eliminates the time overhead when a function is called.
- It reduces space as no separate set of instructions in memory is written.
- It reduces space as no separate set of instructions in memory is written.
exlent
ReplyDelete