#include<iostream.h>
#include<conio.h>
class add //Declaration of class
{
Private: // Data member
int a,b;
Public:
add(int x,int y):a(x),b(y){} //constructor with initialize list
void Display ( ) //Data member Function
{
int z;
z=a+b;
cout<<"\n sum is="<<z;
}
};
main()
{
clrscr( );
int p,q;
cout<<Enter number=";
add a1(p,q) //a1 is object of class
a1.Display // accessing function through object a1
getch();
}
---------------------------------------------------------------------
Initialization List is another or a better approach for initializing the member variables (and base class objects) of a class upon construction of an instance of it's own.
Things to know about Initialization List
Initialization List can appear irrespective of the place the constructor is defined.
Initializing the member variables in the Initialization List is better than initializing them inside the body of the constructor of the class.
Data members are initialized in the order they are declared, regardless of the order of their initialization.
It is mandatory to initialize Reference Data Member in an Initialization List because it can not exist without being initialized.
It is mandatory to initialize Constant Data Member in an Initialization List otherwise it would be constrcuted with some junk values and we cannot initialize it later anywhere else.
It is mandatory to construct and initialize, embedded class objects/base class objects in case of inheritance, in an Initialization List, if they do not themselves have a zero-argument/default constructor provided.

No comments:
Post a Comment