Assignment: Introduction to Object-Oriented Programming (OOP) and Basic Concepts
Total Marks: 64 (8 marks each)
Instructions:
• Answer all questions.
• Provide code examples and explanations wherever required.
• Mention assumptions, if any.
Questions:
1. Introduction to OOP:
a) What is Object-Oriented Programming (OOP)? Explain its core principles.
b) How is OOP different from procedural programming? Give at least two examples to
support your answer.
2. Declaring Classes and Creating Objects:
Write a C++ program that:
o Declares a class Student with data members name and rollNumber.
o Defines a member function displayDetails() to display the student’s information.
o Creates an object of the Student class and calls the displayDetails() function.
Explain how objects are created and used in your program.
3. Member Functions, Data Members, and Inline Functions:
a) What are data members and member functions in a class? Explain with examples.
b) What is an inline member function? Modify the displayDetails() function from Question
2 to be an inline function and explain the benefits.
4. Access Specifiers and Access Control:
a) Explain the significance of public, private, and protected access specifiers in C++.
b) Create a class BankAccount with private data members accountNumber and balance,
and public member functions deposit() and displayBalance(). Demonstrate how access
specifiers control access to data.
5. Object Initialization and Initialization Lists:
a) How are objects initialized in C++?
b) Write a class Rectangle with data members length and width. Use an initialization list in
the constructor to initialize these members. Provide a code example and explain the
concept.
6. Constructors and Destructors:
a) What are constructors and destructors? Why are they important in OOP?
b) Write a class Car that includes:
o A default constructor that sets default values.
o A parameterized constructor to set specific values.
o A destructor to print a message when an object is destroyed.
Demonstrate how these are used in a program.
7. Copy Constructor and Its Significance:
a) What is a copy constructor? How is it different from an assignment operator?
b) Write a program with a class Book that uses a copy constructor to copy data from one
object to another. Explain when and why a copy constructor is called.
8. Encapsulation, Inheritance, Polymorphism, and Abstraction:
a) Define encapsulation, inheritance, polymorphism, and abstraction with real-world
examples.
b) Create a base class Shape with a virtual function area(). Derive two classes Circle and
Rectangle that override the area() function. Explain how polymorphism works in this
context.