Q:

C++ Program to demonstrate Constructor Overloading with Example

0

Writ a C++ Program to demonstrate Constructor Overloading with Example. Here’s a Simple Program to demonstrate Constructor Overloading with Example in C++ Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What is Class and Objects in C++?


  • The classes are the most important feature of C++ that leads to Object Oriented programming.
  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables. Objects are initialized using special class functions called Constructors.

Just like any other function, the constructor of a class may also be overloaded so that even with different number and types of initial values, an object may still be initialized.

 
 

Below is the source code for C++ Program to demonstrate Constructor Overloading with Example which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C++ Program to demonstrate Constructor Overloading with Example  */

#include<iostream>
#include<stdlib.h>
using namespace std;

class Deposit
{
        long int principal;
        int time;
        float rate;
        float totalamount;

        public:
                Deposit();                            // #1
                Deposit(long p, int t, float r);      // #2
                Deposit(long p, int t);               // #3
                Deposit(long p, float r);             // #4
                void calculateamount(void);
                void display(void);
};

Deposit::Deposit()
{
        principal = time = rate = 0.0;
}

Deposit::Deposit(long p, int t, float r)
{
        principal = p;
        time = t;
        rate = r;
}
Deposit::Deposit(long p, int t)
{
        principal = p;
        time = t;
        rate = 0.08;
}

Deposit::Deposit(long p, float r)
{
        principal = p;
        time = 2;
        rate = r;
}
void Deposit::calculateamount(void)
{
        totalamount = principal + (principal*time*rate)/100;
}

void Deposit::display(void)
{
        cout<<"\nPrincipal Amount :: Rs."<<principal<<"\n";
        cout<<"\nNo. of Years :: "<<time<<" years\n";
        cout<<"\nRate of interest :: "<<rate<<"\n";
        cout<<"\nTotal Amount :: Rs."<<totalamount<<"\n";
}


int main()
{
        Deposit d1;
        Deposit d2(2000, 2, 0.07f);
        Deposit d3(4000, 1);
        Deposit d4(3000, 0.12f);

        d1.calculateamount();
        d2.calculateamount();
        d3.calculateamount();
        d4.calculateamount();

        cout<<"Object D1 Details ::-------------- \n";
        d1.display();
        cout<<"\nObject D2 Details ::--------------\n";
        d2.display();
        cout<<"\nObject D3 Details ::----------------\n";
        d3.display();
        cout<<"\nObject D4 Details ::----------------\n";
        d4.display();

        return 0;
}

OUTPUT : :


/*  C++ Program to demonstrate Constructor Overloading with Example  */

Object D1 Details ::--------------

Principal Amount :: Rs.0

No. of Years :: 0 years

Rate of interest :: 0

Total Amount :: Rs.0

Object D2 Details ::--------------

Principal Amount :: Rs.2000

No. of Years :: 2 years

Rate of interest :: 0.07

Total Amount :: Rs.2002.8

Object D3 Details ::----------------

Principal Amount :: Rs.4000

No. of Years :: 1 years

Rate of interest :: 0.08

Total Amount :: Rs.4003.2

Object D4 Details ::----------------

Principal Amount :: Rs.3000

No. of Years :: 2 years

Rate of interest :: 0.12

Total Amount :: Rs.3007.2

Process returned 0

Above is the source code and output for C++ Program to demonstrate Constructor Overloading with Example which is successfully compiled and run on Windows System to produce desired output.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Classes and Objects Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Program To calculate Volume of Box using Const... >>
<< Write a C++ Program to show Example of Default cop...