12/03/2022

A constructor in is a special member function that is use to initialize the object of its class. It
is special because its name is the same as the class name. The constructor is invoke
whenever an object of its associate class is creat. It is call a constructor because it
constructs the value of data members of the class. The constructor doesn’t have a return type. I
do not specify a constructor C++ compiler generates the default constructor.

Then, Using C programming to perform this sorting operation you can follow any language.

So, try it your self on online compiler.

class integer
{
int m, n;
public:
integer(); // constructor declared ……
};
integer :: integer() // constructor defined
{
m = 0;
n = 0;
}

When a class contains a constructor, the object create by the class will be initialized
automatically.
integer int1; //object int1 created
it not only creates the object int1 of type integer but also initializes its data
members m and n to zero.
There is no need to write any statement to invoke the constructor function (as we do with
the normal member functions).
A constructor that accepts no parameters is call the default constructor.
The default constructor for class A is A::A().
If no such constructor is define, then the compiler supplies a default constructor.
Therefore, a statement such as
A a;
invokes the default constructor of the compiler to create the object of a.

Characteristics of constructor

  1. They should be declared in the public section.
  2. Constructors are invoking automatically when the objects are create.
  3. They do not have return types, not even void and therefore, they cannot return values.
  4. They cannot be inherent, though a derived class can call the base class constructor.
  5. Like other C++ functions, they can have default arguments.
  6. Constructors cannot be virtual.
  7. We cannot refer to their addresses.
  8. An object with a constructor (or destructor) cannot be use as a member of a union.
  9. They make ‘implicit calls’ to the operators new and delete them when memory allocation is require.
  10. Note: when a constructor is declare for a class, the initialization of the class objects becomes mandatory.

Default Constructor

#include<iostream.h>
#include<conio.h>
class construct
{
public:
int a,b;
construct () //Default constructor
{
a=10;
b=20;
}
};
int main()
{
construct c;
clrscr();
cout<<"Values of A"<<c.a<<endl<<"Values of B"<<c.b;
getch();
return 0;
}
OUTPUT: 
Values of A 10
Values of B 20
types of constructor

So, for more update going to –learning points – Home | A better learning future starts here

For more OOCP concept go for –Class and Object – Learning Points

Leave a Reply

Your email address will not be published. Required fields are marked *

Related News