cswithalam mehtabalam1
Practice Problems
Objects and Encapsulation
Problem 1: Create a Rectangle Class with Constructors
Objective: Work with both default and parameterized constructors.
Instructions:
1. Define a Rectangle class with the following attributes:
○ int width
○ int height
2. Write the following constructors:
○ A default constructor that sets the width and height to 1.
○ A parameterized constructor that takes two arguments to initialize the width and
height.
3. Add a method void display() to show the dimensions of the rectangle.
Bonus: Add a method int area() to calculate and return the area of the rectangle.
Problem 2: Create a Car Class with Constructors
Objective: Practice using parameterized constructors to initialize attributes.
Instructions:
1. Define a Car class with the following attributes:
○ string make
○ string model
○ int year
2. Write the following constructors:
○ A default constructor that sets the make to "Unknown", the model to "Generic",
and the year to 0.
○ A parameterized constructor that accepts values for make, model, and year.
3. Add a method void displayCarInfo() to display the car’s details.
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
Bonus: Add a destructor that prints a message when a Car object is destroyed.
Problem 3: Create a Laptop Class with Destructor
Objective: Implement and understand the use of destructors.
Instructions:
1. Define a Laptop class with the following attributes:
○ string brand
○ string model
○ double price
2. Write a constructor to initialize the attributes.
3. Add a destructor that prints "Laptop object destroyed".
4. Add a method void display() to print the laptop’s details.
Problem 4: Create a Book Class with Parameterized Constructor
Objective: Focus on parameterized constructors to set object attributes.
Instructions:
1. Define a Book class with the following attributes:
○ string title
○ string author
○ double price
2. Write a parameterized constructor to initialize these attributes.
3. Add a method void displayBookDetails() to display all book details.
Problem 5: Create a BankAccount Class with Destructor
Objective: Work with destructors to manage the lifecycle of objects.
Instructions:
1. Define a BankAccount class with the following attributes:
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
○ string accountNumber
○ double balance
2. Write a constructor to initialize the account number and balance.
3. Add a method void deposit(double amount) to increase the balance.
4. Add a destructor that prints "BankAccount object destroyed".
5. Add a method void displayAccountInfo() to display the account details.
Problem 6: Create a Student Class with Constructors
Objective: Practice using both default and parameterized constructors.
Instructions:
1. Define a Student class with the following attributes:
○ string name
○ int age
○ char grade
2. Write:
○ A default constructor that sets the name to "Unknown", age to 0, and grade to
'N'.
○ A parameterized constructor to initialize all three attributes.
3. Add a method void displayStudentInfo() to display the student’s details.
Bonus: Add a destructor that prints "Student object destroyed".
Problem 7: Create a Course Class with Destructor
Objective: Practice creating and destroying objects using constructors and destructors.
Instructions:
1. Define a Course class with the following attributes:
○ string courseName
○ string courseCode
○ int credits
2. Write a constructor to initialize all attributes.
3. Add a destructor that prints "Course object destroyed".
4. Add a method void displayCourseInfo() to display the course details.
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
Visit my website: https://mehtab.netlify.app/