Question 1.
A college wants to develop a simple Student Admission System. When a new student record
is created, the system should automatically assign default values for the student’s roll
number and department.
Write a C++ program that:
1. Uses a class Student with data members: rollNo, department.
2. Uses a default constructor to assign default values.
3. Displays the student details using a member function.
Question 2.
An organization is developing an Employee Payroll System. Each employee’s details such as
employee ID and basic salary should be provided at the time of object creation.
Write a C++ program that:
1. Uses a class Employee with data members: empId, basicSalary.
2. Uses a parameterized constructor to initialize values at runtime.
3. Displays the employee payroll details.
#include <iostream>
using namespace std;
class Student {
int rollNo;
string department;
public:
// Default Constructor
Student() {
rollNo = 1001;
department = "Computer Science";
}
void display() {
cout << "Roll Number : " << rollNo << endl;
cout << "Department : " << department << endl;
}
};
int main() {
Student s; // Default constructor is invoked automatically
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class Employee {
int empId;
float basicSalary;
public:
// Parameterized Constructor
Employee(int id, float salary) {
empId = id;
basicSalary = salary;
}
void display() {
cout << "Employee ID : " << empId << endl;
cout << "Basic Salary : " << basicSalary << endl;
}
};
int main() {
Employee e(201, 30000); // Values passed at object creation
[Link]();
return 0;
}