Skip to main content

ProwessApps

Learn · Practice · Excel

Define a class "Deposit" with following member
private : principle, time_period, rate of interest, amount
public : input(), display()
Default Constructor - to initialize all members by zero
Parametrized Constructor - to initialize with parameter
Copy Constructor - to copy and destructor
WAP to calculate amount

C++ Code Example — Constructor Programs

ADVERTISEMENT

Define a class "Deposit" with following member
private : principle, time_period, rate of interest, amount
public : input(), display()
Default Constructor - to initialize all members by zero
Parametrized Constructor - to initialize with parameter
Copy Constructor - to copy and destructor
WAP to calculate amount

Objective

Write a C++ program for a Bank Deposit class using multiple constructors to initialize the data.

Algorithm / Approach

  1. Create a class Deposit with private variables (p, t, r, a).
  2. Define input() and display() methods.
  3. Create a Default constructor (initializes to 0).
  4. Create a Parameterized constructor (initializes to passed values).
  5. Create a Copy constructor (copies values from a passed Deposit object).
  6. In main(), instantiate objects using all constructors and calculate the final amount.
main.cpp
#include<iostream>
using namespace std;
class Deposit{
 int p,t,r,a;
 public :
 void input() {
  cout<<"Enter principle amount";
  cin>>p;
  cout<<"Enter time ";
  cin>>t;
  cout<<"Enter Rate of Interest ";
  cin>>r;
 }
 void display() {
  a= p+((p*r*t)/100);
  cout<<"Total Amount "<< a<< endl;
 }
 Deposit() {
  cout<<"Default Const. called\n";
  p =0;
  t= 0;
  r = 0;
 }
 Deposit(int pr, int ti, int ra) {
  cout<<"param Const. called \n"<<;
  p = pr;
  t= ti;
  r = ra;
 }
 Deposit(Deposit &d){
  cout<<"Copy Const. called\n";
  p = d.p;
  t = d.t;
  r = d.r;
 }
 ~Deposit() {
  cout<<"Destructor called\n";
 }
};
int main() {
 Deposit d;
 d.input();
 d.display();
 Deposit d2(1000,20,3);
 d2.display();
 cout<<"Default Const. copied\n"<<;
 Deposit d3(d);
 d3.display();
 cout<<"Param. Const. copied\n";
 Deposit d4(d2);
 d4.display();
 return 0;
}

Expected Output

Default Const. called
Enter principle amount 5000
Enter time 2
Enter Rate of Interest 5
Total Amount 5500
param Const. called
Total Amount 1600
Default Const. copied
Copy Const. called
Total Amount 5500
Param Const. copied
Copy Const. called
Total Amount 1600
Destructor called
Destructor called
Destructor called
Destructor called

Explanation of the Program

  • This program uses "Constructor Overloading". Just like function overloading, you can have multiple constructors for the same class as long as they require different parameters.
  • Note on the provided code: There are severe syntax errors on lines 92 and 113. cout&lt;&lt;"..."&lt;&lt;; ends with an empty insertion operator before the semicolon. This will cause a compilation error. The trailing &lt;&lt; must be removed.

Complexity

Time Complexity O(1)
Space Complexity O(1)

Common Mistakes

  • Syntax Error: Ending a cout statement with a dangling insertion operator (e.g., cout &lt;&lt; "Text" &lt;&lt;;).
ADVERTISEMENT