C++ dynamic Initialization of objects

C++ Dynamic Initialization of objects

Class objects can be initialize dynamically too. That is to say the initial value of an object may be provided during run time. There are one advantage of dynamic initialization is that we can provide various initialization formats, using overloading constructor. This provides the flexibility of using different format of data at the run time depending on the situation consider the long term(time) deposit schemes working in the commercial banks. The banks provide different different interest rates for different schemes as well as for different periods of investment

let us take example below of how to use the class variables for holding account details and how to construct these variables at run Time using dynamic initialization

//it is long term fix deposit system
#include<iostream>
using namespace std;
class Fixed_deposit
{
long int pamount; // principal amount
int years; // peroid of investment
float rates; // rate of interest
float R_value; // return value of amount
public :
Fixed_deposit()
{
}
Fixed_deposit(long int p,int y,float r=0.12);
Fixed_deposit(long int p,int y,int r);
void display();
};
Fixed_deposit::Fixed_deposit(long int p, int y,float r)
{
pamount=p;
years=y;
rates=r;
R_value=pamount;
for(int i=1;i<=y;i++)
{
R_value=R_value*(1.0+r);
}
} Fixed_deposit::Fixed_deposit(long int p,int y, int r)
{
pamount=p;
years=y;
rates=r;
R_value=pamount;
for(int i=1;i<=y;i++)
{ R_value=R_value*(1.0+float(r)/100);
}
} void Fixed_deposit::display()
{
std::cout<<"\n";
std::cout<<"principal amount="<< pamount <<" \n";
std::cout << "return value="<<R_value<<"\n";
}
int main()
{
Fixed_deposit fd1,fd2,fd3; // deposit created
long int p; // for principal amount
int y; // investment periods years
float R; // interest rate, decimal form
int r; // interest rate, percentage form
std::cout <<"Enter amount, period, interest rate(in percentage)" << "\n";
std::cin >>p>> y>> R;
fd1=Fixed_deposit(p,y,R);
std::cout<<"Enter amount, period, interest rate(decimal form)" <<"\n";
std::cin>>p >>y >> r;
fd2=Fixed_deposit(p,y,r);
std::cout<<"Enter amount and period" << "\n";
std::cin>>p >> y;
fd3=Fixed_deposit(p,y);
std::cout <<"\nDeposit 1";
fd1.display();
std::cout <<"\n desposit 2";
fd2.display();
std::cout <<"\n deposit 3";
fd3.display();
}

the output of this program
enter amount, period, interest rate(in percentage)
10000 3 18
Enter amount, period, interest rate(in decimal form)
10000 3 0.18
Enter amount and period
10000 3



deposit 1
principal amount=10000
return value =16430.3
deposit 2
principal amount =10000
return value =16430.3


deposit 3
principal amount =10000
return value =14049.3

in this program use three overloaded constructors. The parameter values to these constructs are provide at run time, The user can provide input in one of the following forms

1. Amount, period and interest in decimal form.
2. Amount, period and interest in percentage form.
3. Amount and period.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise