C++ Program To Calculate Tax Using Constructor and Destructor 

Calculating the tax using the C++ program is easy if you have the concept of constructor and destructor. There is also another way to calculate Tax using C++ which is simple but the use of constructor is much practical in the programming world. Let us have some knowledge about the class constructor and destructor below.

The Class Constructor

A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have the exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

The Class Destructor

A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.
A destructor will have the exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories, etc.

Question

/*
Define a class taxpayer with following specifications:
Data members:
- Int pan
- Char name[]
- Float income
- Float tax
Member functions:
-Input() to input data
-Disp() to display data
-Calctax() to calculate tax
Tax is calculated according to the following rates
Income                      tax
<=100000                    0%
100000 to 200000            10%
200000 to 500000            15%
>500000                      20%
(Make constructor and destructor too)

*/



#include<iostream>
#include<cstring>
#include<iomanip>
using namespace std;
class Taxpayer
{
private:
int pan;
char name[30];
float income;
float tax;

public:
Taxpayer() //constructor making
{
pan=0;
name[50]=0;
income=0;
tax=0;
}
Taxpayer(int p,char n[],float inc);
void Input();
void Display();
void Calctax();

~Taxpayer()
{
cout<<"\nDestructed..."<<endl;
}
};
void Taxpayer::Input()
{
cout<<"\nEnter pan number : ";
cin>>pan;
cout<<"\nEnter Name : ";
cin>>name;
cout<<"\nEnter income : ";
cin>>income;
}

void Taxpayer::Display()
{
cout<<"Pan No.:- "<<pan<<endl;
cout<<"Name:- "<<setw(4)<<name<<endl;
cout<<"Income :- "<<income<<endl;
}

void Taxpayer::Calctax()
{
if (income<=100000)
{
tax=0;
cout<<"No tax is charged."<<endl;
}
else if (income>100000 && income<=200000)
{
tax=0.1*income;
cout<<"Tax of 10% of total income is charged and the tax amount is : "<<tax<<endl;
}
else if (income>200000 && income<=500000)
{
tax=0.15*income;
cout<<"Tax of 15% of total income is charge and the tax amount is : "<<tax<<endl;
}
else
{
tax=0.2*income;
cout<<"Tax of 20% of total income is charged and the tax amount is : "<<tax<<endl;
}
}

int main()
{
Taxpayer t;
t.Input();
t.Display();
t.Calctax();
return 0;
}

Output


Enter pan number : 25

Enter Name : George

Enter income : 600000

Pan No.:- 25

Name:- George

Income :- 600000

Tax of 20% of total income is charged and the tax amount is : 120000

Destructed...


Share To:

Arogya Thapa Magar

Post A Comment:

0 comments so far,add yours