Create a class `Employee` with data members `emp_id`, `name`, and `salary`. Accept and display employee details. Calculate and display net salary after deducting 10% tax.

SOLUTION

Employee Class in C++ — Demo Page
#include <iostream>
#include <string>
using namespace std;

class Employee {
private:
    int emp_id;
    string name;
    float salary;

public:
    // Function to accept employee details
    void acceptDetails() {
        cout << "Enter Employee ID: ";
        cin >> emp_id;
        cin.ignore(); // clear newline before getline
        cout << "Enter Employee Name: ";
        getline(cin, name);
        cout << "Enter Employee Salary: ";
        cin >> salary;
    }

    // Function to calculate net salary (after 10% tax)
    float calculateNetSalary() {
        return salary - (salary * 0.10f);
    }

    // Function to display employee details
    void displayDetails() {
        cout << "\nEmployee Details:\n";
        cout << "ID: " << emp_id << endl;
        cout << "Name: " << name << endl;
        cout << "Gross Salary: " << salary << endl;
        cout << "Net Salary (after 10% tax): " << calculateNetSalary() << endl;
    }
};

int main() {
    Employee emp;
    emp.acceptDetails();
    emp.displayDetails();
    return 0;
}

OUTPUT :- 

Leave a Reply

Your email address will not be published. Required fields are marked *