Class is a user defined data type that behave very similar to built-in data types.
We should able to initialize a class type variable (object) when it is declared much the same way as an ordinary variable,
We should able to initialize a class type variable (object) when it is declared much the same way as an ordinary variable,
int a=10;
Similarly, when a variable of built-in data type goes out of scope the compiler automatically destroys the variable.
Similarly, when a variable of built-in data type goes out of scope the compiler automatically destroys the variable.
C++ provide special member functions called constructor and destructor that used to initialize and destroy objects.
Constructors
A constructor is a special member function whose task is to initialize the objects of its class. It have some special characteristics- The name of constructor is same as class name.
- it should be declared in public section.
- it is invoked automatically when the objects are created.
- it do not have any return type, so it cannot return values.
- it can’t be inherited, though a derived class can call the base class constructor.
- it can’t be virtual.
- it can have default arguments.
class integer
{
private:
int m,n;
public:
integer() // constructor defined
{
m=0,n=0;
}
};
integer a; // object created and initialized
{
private:
int m,n;
public:
integer() // constructor defined
{
m=0,n=0;
}
};
integer a; // object created and initialized
Tags:
Cpp