o1-mini-2024-09-12· OpenAI
WildChat-4.8M (AI2)
write some c++ code
- User
write some c++ code
- Assistant
write some c++ code
Sure! Here's a simple C++ program that demonstrates the use of classes, inheritance, and basic input/output operations. This example models a basic system for handling different types of employees in a company.
#include <iostream>
#include <vector>
#include <string>
// Base class: Employee
class Employee {
protected:
std::string name;
int id;
public:
Employee(const std::string& name_, int id_) : name(name_), id(id_) {}
virtual void displayInfo() const {
std::cout << "Employee ID: " << id << "\nName: " << name << std::endl;
}
virtual double calculatePay() const = 0; // Pure virtual function
virtual ~Employee() {}
};
// Derived class: FullTimeEmployee
class FullTimeEmployee : public Employee {
private:
double annualSalary;
public:
FullTimeEmployee(const std::string& name_, int id_, double salary)
: Employee(name_, id_), annualSalary(salary) {}
void displayInfo() const override {
Employee::displayInfo();
std::cout << "Employment Type: Full-Time\nAnnual Salary: $" << annualSalary << std::endl;
}
double calculatePay() const override {
return annualSalary / 12; // Monthly pay
}
};
// Derived class: PartTimeEmployee
class PartTimeEmployee : public Employee {
private:
double hourlyRate;
double hoursWorked;
public:
PartTimeEmployee(const std::string& name_, int id_, double rate, double hours)
: Employee(name_, id_), hourlyRate(rate), hoursWorked(hours) {}
void displayInfo() const override {
Employee::displayInfo();
std::cout << "Employment Type: Part-Time\nHourly Rate: $" << hourlyRate
<< "\nHours Worked: " << hoursWorked << std::endl;
}
double calculatePay() const override {
return hourlyRate * hoursWorked;
}
};
// Function to display all employees and their pay
void displayEmployees(const std::vector<Employee*>& employees) {
for (const auto& emp : employees) {
emp->displayInfo();
std::cout << "Monthly Pay: $" << emp->calculatePay() << "\n---------------------------\n";
}
}
int main() {
std::vector<Employee*> employees;
// Creating full-time employees
employees.push_back(new FullTimeEmployee("Alice Johnson", 1001, 80000));
employees.push_back(new FullTimeEmployee("Bob Smith", 1002, 95000));
// Creating part-time employees
employees.push_back(new PartTimeEmployee("Charlie Davis", 2001, 20.5, 120));
employees.push_back(new PartTimeEmployee("Dana Lee", 2002, 18.75, 90));
// Display all employee information and their monthly pay
displayEmployees(employees);
// Clean up allocated memory
for (auto emp : employees) {
delete emp;
}
employees.clear();
return 0;
}
Classes and Inheritance:
name and id.calculatePay(), making Employee an abstract class.Employee.annualSalary as an attribute.calculatePay() by dividing the annual salary by 12 to get the monthly pay.Employee.hourlyRate and hoursWorked as attributes.calculatePay() by multiplying hourlyRate with hoursWorked.Polymorphism:
employees vector holds pointers to the base class Employee.FullTimeEmployee or PartTimeEmployee), the appropriate displayInfo() and calculatePay() functions are called.Memory Management:
new and stored as pointers in the employees vector.delete each allocated object to prevent memory leaks.Program Flow:
Employee ID: 1001
Name: Alice Johnson
Employment Type: Full-Time
Annual Salary: $80000
Monthly Pay: $6666.67
---------------------------
Employee ID: 1002
Name: Bob Smith
Employment Type: Full-Time
Annual Salary: $95000
Monthly Pay: $7916.67
---------------------------
Employee ID: 2001
Name: Charlie Davis
Employment Type: Part-Time
Hourly Rate: $20.5
Hours Worked: 120
Monthly Pay: $2460
---------------------------
Employee ID: 2002
Name: Dana Lee
Employment Type: Part-Time
Hourly Rate: $18.75
Hours Worked: 90
Monthly Pay: $1687.5
---------------------------
Save the Code:
employee_system.cpp.Compile:
employee_system.cpp.g++ -std=c++11 -o employee_system employee_system.cpp
clang++ -std=c++11 -o employee_system employee_system.cpp
Run:
./employee_system
This basic structure can be extended in various ways:
Additional Employee Types:
Contractor, Intern, etc.Data Storage:
Error Handling:
Advanced Features:
Feel free to modify and expand upon this example to suit your needs or to explore more advanced C++ features!
Not yet included in a published release.