Saturday, 4 June 2016

Constructors And Destructor In C++


The automatic initialization as soon as the object is created is called as constructor.
We should know the concept of access specifiers here. Whenever we create a class, we put variables under public, private, protected etc.
Now the variables which comes under the Private are known as Member Variables while the variables which comes under the Private section are known as Member Function. The Constructors are always declared in the public section of the program. The prime work of the constructor is to initialize Member Variable from the PRIVATE class.
The constructor has the same name as the class name.
Destructor is the anti of Constructor. If object is born when constructor is created, it dies when destructor is called. Destructor has also the same name as the class name except for a trend that it is preceded by ~ symbol.
Program:
#include<iostream.h>
#include<conio.h>
class ratio
{
private:
int num,den;
public: \\Access specifier
ratio() \\Constructor
{
cout<<“Object is born\n\n”;
}
~ratio() \\Destructor
{
cout<<“Object dies\n\n”;
}
};
void main()
{
clrscr();
{
ratio x; \\This is how object is created i.e Class name then Object name.
Also constructor is called here because object is created here(See the text above)
cout<<“Now x is alive\n\n”;
}
cout<<“Now between the blocks\n\n”; \\you need to be careful with blocks. Blocks are nothing but the curly brackets inside which the program is written.
{
ratio y; \\Another object created. Again the constructor will be called 
cout<<“Now y is alive\n\n”;
}
getch();
}

Functions in C++


What are Functions?

Now functions are modules or its like a part of a device which performs its work indivually. All parts of the computer like monitor or mouse or speakers can be seen as small small modules which together makes a computer. They can work indiviually as well. Speakers can work independently. This example is to show you guys that function in C++ is just a part of program which does it work when called.

Why do we use Function?

To modularise the program. Segmenting the program into small modules so that it can be easy to debug or update easily. If there’s a problem in your speaker you dont change you entire PC, you just repair or replace the speaker. Likewise, here too if there’s a problem in any segment of the program you just correct the function. The key point is functions helps in modularising the program.

How do we use it?

In C++, function is anything after which () is written. Even after void main(), we use the symbol (). () indicates that it is function. Functions can be named anything(Except keywords). However, there is a return type with every function. Return type is what does you function return to the program(Like your speaker plays songs). Data type can be int,float,double,void etc.

Programs on Functions are given on the ALL PROGRAMS option or you can search it. Detailed explanation is provided at each step. Comment below to ask any question or anything that might be useful to your knowlege.