Dated: 22/12/24
Programming Fundamentals Lab Assignment
C++ Class
A class is a blueprint for creating objects. It defines properties (attributes) and behaviors (methods) that
objects will have. It organize data and functionality into reusable, logical units.
Class Methods
Functions defined inside a class that define the behavior or actions of its objects. Perform operations on
the data of the class and its objects.
Objects
Instances of a class that hold specific data and can perform actions using class methods. Represent real-
world entities in a program using attributes and behaviors.
Attributes
Variables defined inside a class that store the data or properties of objects. Represent the characteristics
of an object.
Access Specifiers
Keywords (public, private, protected) that control the visibility and accessibility of class members.
public: Accessible from outside the class.
private: Accessible only within the class.
protected: Accessible within the class and derived classes.
Parameters
Values passed to methods or constructors to customize their behavior. Allow flexibility by providing
input data when methods or objects are used.
Constructors
Special methods that are automatically called when an object is created. They have the same name as
the class and no return type. Initialize objects with specific values.
Encapsulation
Encapsulation is the process of bundling data (attributes) and methods that operate on that data into a
single unit (class) while restricting direct access to some components.
Data Protection: Prevents unauthorized access and modification of sensitive data.
Improved Security: Hides implementation details from the user (data hiding).
Modularity: Keeps the code organized and easier to debug or update.
Inheritance
Inheritance allows a class (derived class) to acquire properties and methods from another class (base
class). It provides the following features:
Code Reusability: Avoids rewriting code by reusing existing functionality in new classes.
Hierarchical Relationships: Models real-world relationships like "is-a" (e.g., Dog is a type of
Animal).
Extensibility: Easily extend the functionality of existing classes without modifying them.
Maintainability: Makes code easier to manage by organizing it into logical hierarchies.
Polymorphism
Polymorphism allows a single interface (function or method) to be used for different types of objects. It
comes in two forms:
1. Compile-time Polymorphism (Function Overloading and Operator Overloading)
2. Runtime Polymorphism (Using Virtual Functions).
Tasks
Question 1: Create a class Car with attributes brand, model, and year. Write a method displayDetails()
to print these attributes. Create an object of the class and call the method.
Question 2: Create a class BankAccount with attributes accountHolder, accountNumber, and balance.
Add methods deposit(amount) and withdraw(amount). Ensure withdrawal cannot exceed the balance.
Question 3: Create a base class Employee with attributes name and salary. Create a derived class
Manager that adds an attribute department. Write methods to display the details of a manager.
Question 4: Create a base class Shape with a virtual method area(). Derive two classes Circle and
Square that override the area() method to calculate their respective areas.