Thursday, March 13, 2025

C++ program to calculate an employee's salary

 Here's a simple C++ program to calculate an employee's salary:



#include <iostream>

#include <string>

using namespace std;


class Employee {

private:

    string name;

    int employeeId;

    string department;

    double basicSalary;

    double allowance;

    double deduction;

    double netSalary;


public:

    void inputDetails() {

        cout << "Enter employee name: ";

        cin.ignore();

        getline(cin, name);

        cout << "Enter employee ID: ";

        cin >> employeeId;

        cout << "Enter department: ";

        cin.ignore();

        getline(cin, department);

        cout << "Enter basic salary: ";

        cin >> basicSalary;

        cout << "Enter allowance: ";

        cin >> allowance;

        cout << "Enter deduction: ";

        cin >> deduction;

    }


    void calculateNetSalary() {

        netSalary = basicSalary + allowance - deduction;

    }


    void displayDetails() {

        cout << "\nEmployee Details\n";

        cout << "Name: " << name << endl;

        cout << "Employee ID: " << employeeId << endl;

        cout << "Department: " << department << endl;

        cout << "Basic Salary: $" << basicSalary << endl;

        cout << "Allowance: $" << allowance << endl;

        cout << "Deduction: $" << deduction << endl;

        cout << "Net Salary: $" << netSalary << endl;

    }

};


int main() {

    Employee employee;

    employee.inputDetails();

    employee.calculateNetSalary();

    employee.displayDetails();

    return 0;

}



*Example Output:*


Enter employee name: John Doe

Enter employee ID: 123

Enter department: HR

Enter basic salary: 5000

Enter allowance: 1000

Enter deduction: 500


Employee Details

Name: John Doe

Employee ID: 123

Department: HR

Basic Salary: $5000

Allowance: $1000

Deduction: $500

Net Salary: $5500



This program defines an `Employee` class with private member variables for employee details and public methods for inputting details, calculating the net salary, and displaying details.


In the `main()` function, an `Employee` object is created, and the user is prompted to input employee details. The program then calculates the net salary and displays the employee details.

C++ program for a school management system

Here's a simple C++ program for a school management system:




#include <iostream>
#include <string>
using namespace std;

class Student {
private:
    string name;
    int rollNumber;
    int age;
    string className;

public:
    void inputDetails() {
        cout << "Enter student name: ";
        cin.ignore();
        getline(cin, name);
        cout << "Enter roll number: ";
        cin >> rollNumber;
        cout << "Enter age: ";
        cin >> age;
        cout << "Enter class name: ";
        cin.ignore();
        getline(cin, className);
    }

    void displayDetails() {
        cout << "\nStudent Details\n";
        cout << "Name: " << name << endl;
        cout << "Roll Number: " << rollNumber << endl;
        cout << "Age: " << age << endl;
        cout << "Class Name: " << className << endl;
    }
};

class Teacher {
private:
    string name;
    string subject;
    int age;

public:
    void inputDetails() {
        cout << "Enter teacher name: ";
        cin.ignore();
        getline(cin, name);
        cout << "Enter subject: ";
        getline(cin, subject);
        cout << "Enter age: ";
        cin >> age;
    }

    void displayDetails() {
        cout << "\nTeacher Details\n";
        cout << "Name: " << name << endl;
        cout << "Subject: " << subject << endl;
        cout << "Age: " << age << endl;
    }
};

class School {
private:
    string schoolName;
    string address;
    Student students[10];
    Teacher teachers[10];
    int numberOfStudents;
    int numberOfTeachers;

public:
    void inputSchoolDetails() {
        cout << "Enter school name: ";
        cin.ignore();
        getline(cin, schoolName);
        cout << "Enter school address: ";
        getline(cin, address);
    }

    void addStudent() {
        students[numberOfStudents].inputDetails();
        numberOfStudents++;
    }

    void addTeacher() {
        teachers[numberOfTeachers].inputDetails();
        numberOfTeachers++;
    }

    void displayStudentDetails() {
        for (int i = 0; i < numberOfStudents; i++) {
            students[i].displayDetails();
        }
    }

    void displayTeacherDetails() {
        for (int i = 0; i < numberOfTeachers; i++) {
            teachers[i].displayDetails();
        }
    }

    void displaySchoolDetails() {
        cout << "\nSchool Details\n";
        cout << "Name: " << schoolName << endl;
        cout << "Address: " << address << endl;
    }
};

int main() {
    School school;
    int choice;
    school.numberOfStudents = 0;
    school.numberOfTeachers = 0;
    school.inputSchoolDetails();
    while (true) {
        cout << "\nSchool Management System\n";
        cout << "1. Add student\n";
        cout << "2. Add teacher\n";
        cout << "3. Display student details\n";
        cout << "4. Display teacher details\n";
        cout << "5. Display school details\n";
        cout << "6. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
        switch (choice) {
            case 1:
                school.addStudent();
                break;
            case 2:
                school.addTeacher();
                break;
            case 3:
                school.displayStudentDetails();
                break;
            case 4:
                school.displayTeacherDetails();
                break;
            case 5:
                school.displaySchoolDetails();
                break;
            case 6:
                return 0;
            default:
                cout << "Invalid choice. Please try again.\n";
        }
    }
    return 0;
}
```

*Example Output:*
```
Enter school name: ABC School
Enter school address: 123 Main St

School Management System
1. Add student
2. Add teacher
3. Display student details
4. Display teacher details
5. Display school details
6. Exit
Enter your choice: 1

Enter student name: John Doe
Enter roll number: 1
Enter age: 12
Enter class name: 7th

School Management System
1. Add student
2. Add teacher
3. Display student details
4. Display teacher details
5. Display school details
6. Exit
Enter your choice: 3

Student Details
Name: John Doe
Roll Number: 1
Age: 12
Class Name: 7th

School Management System
1. Add student
2. Add teacher
3. Display student details
4. Display teacher details
5. Display school details
6. Exit
Enter your choice: 5

School Details
Name: ABC School
Address: 123 Main St

Featured posts

स्मार्ट इन्वेस्टिंग फॉर बिगिनर्स:

 स्मार्ट इन्वेस्टिंग फॉर बिगिनर्स:  पैसिव इनकम कमाने का आसान रास्ता"।  इसमें मैंने बताया है कि कैसे छोटी रकम से शुरुआत करके, म्यूचुअल फ...

Popular posts