Const Salary
Take a look at this code. It's currently missing a function declaration for print_info. Define the print_info function where the comments indicate it to complete the program. Don't make modification to the code outside of the commented area.
#include <iostream>
#include <string>
struct Employee {
std::string name;
int id;
double salary;
Employee(const std::string& emp_name, int emp_id, double emp_salary)
: name(emp_name), id(emp_id), salary(emp_salary) {}
//******
// Place the `print_info` function here
//******
};
int main() {
const Employee emp("John Doe", 12345, 75000.0);
emp.print_info();
}
Expected output:
Name: John Doe, ID: 12345, Salary: $75000
Hint
The function has to be declared as a const function to be be called wtih a const object.
Answer
**Here is the code with the print_info function added.
#include <iostream>
#include <string>
struct Employee {
std::string name;
int id;
double salary;
Employee(const std::string& emp_name, int emp_id, double emp_salary)
: name(emp_name), id(emp_id), salary(emp_salary) {}
// Const function to print employee information
void print_info() const {
std::cout << "Name: " << name << ", ID: " << id << ", Salary: $" << salary << std::endl;
}
};
int main() {
// Create a const instance of Employee
const Employee emp("John Doe", 12345, 75000.0);
// Call the const function print_info
emp.print_info();
return 0;
}