C++ Object Initialization





Dynamic initialization of objects in c++
The dynamic initialization means that the initial values may be provided during run time. Even class objects can be initialized dynamically. I.e. with the values provided at run time. The following example explains it.

A Program to find the factorial of an integer using constructor.

#include<iostream.h>
#include<conio.h>
Class factorial
{
Private:
Int n;
Public:
Factorial (int number)
{
N=number;
}
Void display ()
{
Int fact=1;
If (n==0)
Cout<<”\n factorial=1”;
Else
For (int i=1; i<=n; i++)
{
Fact=fact *I;
}
Cout<<”\n factorial=”<<fact;
}
};
Void main ()
{
Int x;
Clrscr ();
Cout<<”\n enter the number to find its factorial “;
Cin>>x;
Obj.dispay ();
getch ();
}



Related posts:

  1. C++ Functions C++ Functions A function is meant for performing a specified...
  2. Switch Statement In C++ Switch statement in c++ C++ provides a multi way decision...
  3. Constructor in C++ Constructor in C++ Constructor is a member function with the...
  4. Loops in C++ Loops in C++ Loop is used when we want to...
  5. Class In C++ Class in c++ Class is a group of similar object....