2080 1

CSC166 ยท 2080 ยท 10 marks

State the use of new operator. An educational institute wishes to maintain a data of its employee. The hierarchical relationships of related classes are as follows. Define all the classes to represent above hierarchy and define functions to retrieve individual information as and when required.

State the use of new operator. An educational institute wishes to maintain a data of its employee. The hierarchical relationships of related classes are as follows. Define all the classes to represent above hierarchy and define functions to retrieve individual information as and when required.[10]

Object Oriented ProgrammingChapter 5: InheritanceTypes of inheritance

Quick answer

The new operator in C++ is used for dynamic memory allocation. It allocates memory at runtime from the heap (free store) and returns a pointer to the beginning of the allocated memory block.

New Operator and Employee Hierarchy in C++

Use of new Operator (2 marks)

Key uses:

  • Allocates memory for a variable or object during program execution
  • Returns a pointer to the allocated memory
  • Memory persists until explicitly released using delete
  • Syntax: pointer_variable = new data_type;

Example:

int *ptr = new int;        // allocates memory for one integer
int *arr = new int[10];    // allocates memory for array of 10 integers
delete ptr;                // free single variable
delete[] arr;              // free array

Employee Hierarchy Program (8 marks)

Class Hierarchy Structure

           Employee
          (name, emp_id)
               |
        ________________
        |               |
    Teacher          Officer
  (subject,        (department,
   salary)           salary)
        |
   Typist
  (speed,
   salary)

Complete C++ Program

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

// โ”€โ”€โ”€ Base Class โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
class Employee {
protected:
    string name;
    int emp_id;

public:
    // Function to set employee data
    void getEmployeeData() {
        cout << "Enter Employee Name: ";
        cin >> name;
        cout << "Enter Employee ID: ";
        cin >> emp_id;
    }

    // Function to display employee data
    void showEmployeeData() {
        cout << "\n--- Employee Information ---" << endl;
        cout << "Name     : " << name << endl;
        cout << "Emp ID   : " << emp_id << endl;
    }
};

// โ”€โ”€โ”€ Derived Class: Teacher โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
class Teacher : public Employee {
protected:
    string subject;
    float salary;

public:
    // Function to set teacher data
    void getTeacherData() {
        getEmployeeData();   // call base class function
        cout << "Enter Subject Specialization: ";
        cin >> subject;
        cout << "Enter Salary: ";
        cin >> salary;
    }

    // Function to display teacher data
    void showTeacherData() {
        showEmployeeData();  // call base class function
        cout << "Subject  : " << subject << endl;
        cout << "Salary   : " << salary << endl;
    }
};

// โ”€โ”€โ”€ Derived Class: Typist (derived from Teacher) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
class Typist : public Teacher {
private:
    int typingSpeed;   // words per minute

public:
    // Function to set typist data
    void getTypistData() {
        getTeacherData();
        cout << "Enter Typing Speed (wpm): ";
        cin >> typingSpeed;
    }

    // Function to display typist data
    void showTypistData() {
        showTeacherData();
        cout << "Typing Speed: " << typingSpeed << " wpm" << endl;
    }
};

// โ”€โ”€โ”€ Derived Class: Officer โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
class Officer : public Employee {
private:
    string department;
    float salary;

public:
    // Function to set officer data
    void getOfficerData() {
        getEmployeeData();   // call base class function
        cout << "Enter Department: ";
        cin >> department;
        cout << "Enter Salary: ";
        cin >> salary;
    }

    // Function to display officer data
    void showOfficerData() {
        showEmployeeData();  // call base class function
        cout << "Department: " << department << endl;
        cout << "Salary    : " << salary << endl;
    }
};

// โ”€โ”€โ”€ Main Function โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
int main() {
    int choice;
    cout << "===== Educational Institute Employee System =====" << endl;
    cout << "1. Teacher\n2. Typist\n3. Officer" << endl;
    cout << "Enter your choice: ";
    cin >> choice;

    // Using new operator for dynamic object creation
    if (choice == 1) {
        Teacher *t = new Teacher;
        t->getTeacherData();
        t->showTeacherData();
        delete t;
    }
    else if (choice == 2) {
        Typist *ty = new Typist;
        ty->getTypistData();
        ty->showTypistData();
        delete ty;
    }
    else if (choice == 3) {
        Officer *o = new Officer;
        o->getOfficerData();
        o->showOfficerData();
        delete o;
    }
    else {
        cout << "Invalid choice!" << endl;
    }

    return 0;
}

Sample Output

===== Educational Institute Employee System =====
1. Teacher
2. Typist
3. Officer
Enter your choice: 1

Enter Employee Name: Ram
Enter Employee ID: 101
Enter Subject Specialization: Mathematics
Enter Salary: 45000

--- Employee Information ---
Name     : Ram
Emp ID   : 101
Subject  : Mathematics
Salary   : 45000

Key Concepts Used

ConceptApplication
Base ClassEmployee holds common data (name, emp_id)
InheritanceTeacher, Officer inherit from Employee; Typist inherits from Teacher
protected membersAllow derived classes to access base class data directly
new operatorUsed to create objects dynamically at runtime
Function overridingEach derived class defines its own show and get functions
public inheritanceDerived class inherits all public and protected members of base class

**

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.