Finding Employee Salary using Constructor Overloading in C++ || Constructor Overloading || C++
In this, we are going to see how to calculate Salary of a Person using Constructor Overloading, in C++.


The Code given below can be used in TURBO C++ Compilers: -

#include <stdio.h>
#include<conio.h>
class Person
{
    int salary;

    public:
    Person()
    {
        salary = 45000;
    }
    Person(int sal)
    {
        salary = sal;
    }
    getsal()
    {
        return salary;
    }
};

void main()
{
    clrscr();
    Person p1, p2(60000);

    cout << "Person 1's salary: " << p1.getsal();
    cout << "\nPerson 2's salary: " << p2.getsal();
    getch();
}

//...........Coded by Sahil Shaikh


The Code given below can be used in gcc/g++ Compilers: -

#include <iostream>
using namespace std;

class Person
{
    int salary;

    public:
    Person()
    {
        salary = 45000;
    }
    Person(int sal)
    {
        salary = sal;
    }
    getsal()
    {
        return salary;
    }
};

int main()
{
    Person p1, p2(60000);

    cout << "Person 1's salary: " << p1.getsal();
    cout << "\nPerson 2's salary: " << p2.getsal();
    return 0;
}

//...........Coded by Sahil Shaikh


#ENJOY CODING

Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post