FAC1003 Tutorial 14 — Object-Oriented Programming & C++ Concepts

Centre for Foundation Studies in Science, Universiti Malaya Session 2025/2026 Semester 2


Concepts

1) OOP vs Procedural Programming

Define Object-Oriented Programming (OOP). How is it different from procedural programming?

2) Encapsulation

Explain the concept of Encapsulation with a C++ code example.

3) Abstraction

In your own words, describe Abstraction and why it is useful in programming.

4) Inheritance vs Polymorphism

What is the difference between Inheritance and Polymorphism?

5) Four Pillars of OOP

Identify and briefly explain the four pillars of OOP.

6) Class vs Object

Define a class and an object in C++. Give one real-life analogy for each.

7) Constructor and Destructor

Explain the role of a constructor and destructor in a C++ class.

8) Three Types of Constructors

Describe the three types of constructors in C++.


Code Tracing

9) Simple Inheritance

What will be the output of the following C++ code?

class Animal {
public:
    void speak() { cout << "Animal speaks" << endl; }
};

class Dog : public Animal {
};

int main() {
    Dog d;
    d.speak();
    return 0;
}

10) Constructor and Destructor

Trace the output of the following code:

class A {
public:
    A() { cout << "Constructor"; }
    ~A() { cout << "Destructor"; }
};

int main() {
    A obj;
    return 0;
}

Applications

11) Rectangle Class

Create a class called "Rectangle" with two private data members length and width, and public member functions to calculate area and perimeter of the rectangle. Provide constructors to initialize the length and width, and a destructor to delete the object when it is no longer needed. Write a program that creates two rectangle objects and displays their area and perimeter.

12) Student Class

Create a class called "Student" with private data members name, roll number, and marks. Provide member functions to get and set the values of these data members. Also, provide a member function to calculate the average marks of a group of students. Write a program that creates an array of Student objects, sets their values, and displays the average marks of the group.

13) Shape Hierarchy (Polymorphism)

Create a class called "Shape" with pure virtual functions area and perimeter. Derive two classes "Rectangle" and "Circle" from "Shape" and implement these functions in each derived class. Write a program that creates an array of Shape pointers, and assigns each element to a Rectangle or Circle object. Calculate and display the area and perimeter of each object using the virtual functions.


Related Pages