-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExp1
122 lines (113 loc) · 3.26 KB
/
Exp1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
using namespace std;
class Person {
protected:
string name;
int age;
public:
// Constructor
Person(string n, int a) : name(n), age(a) {}
// Function to display information
virtual void displayInfo() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public Person {
private:
string course;
string studentID;
public:
// Constructor
Student(string n, int a, string c, string id) : Person(n, a), course(c), studentID(id) {}
// Function to display information
void displayInfo() override {
Person::displayInfo();
cout << "Course: " << course << endl;
cout << "Student ID: " << studentID << endl;
}
};
class Employee : public Person {
private:
string position;
string department;
string employeeID;
double salary;
public:
// Constructor
Employee(string n, int a, string pos, string dept, string id, double sal) : Person(n, a), position(pos), department(dept), employeeID(id), salary(sal) {}
// Function to display information
void displayInfo() override {
Person::displayInfo();
cout << "Position: " << position << endl;
cout << "Department: " << department << endl;
cout << "Employee ID: " << employeeID << endl;
cout << "Salary: $" << salary << endl;
}
};
int main() {
int choice;
cout << "Choose an option:" << endl;
cout << "1. Person" << endl;
cout << "2. Student" << endl;
cout << "3. Employee" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1: {
string name;
int age;
cout << "Enter name: ";
cin.ignore();
getline(cin, name);
cout << "Enter age: ";
cin >> age;
Person person(name, age);
person.displayInfo();
break;
}
case 2: {
string name, course, studentID;
int age;
cout << "Enter name: ";
cin.ignore();
getline(cin, name);
cout << "Enter age: ";
cin >> age;
cout << "Enter course: ";
cin.ignore();
getline(cin, course);
cout << "Enter student ID: ";
cin >> studentID;
Student student(name, age, course, studentID);
student.displayInfo();
break;
}
case 3: {
string name, position, department, employeeID;
double salary;
int age;
cout << "Enter name: ";
cin.ignore();
getline(cin, name);
cout << "Enter age: ";
cin >> age;
cout << "Enter position: ";
cin.ignore();
getline(cin, position);
cout << "Enter department: ";
getline(cin, department);
cout << "Enter employee ID: ";
cin >> employeeID;
cout << "Enter salary: ";
cin >> salary;
Employee employee(name, age, position, department, employeeID, salary);
employee.displayInfo();
break;
}
default:
cout << "Invalid choice!" << endl;
}
return 0;
}