cswithalam mehtabalam1
Practice Problems
Objects and Encapsulation
Problem 1: Create a Product Class
Objective: Explore encapsulation by managing access to attributes using getter and setter
methods.
Instructions:
1. Define a Product class with the following attributes:
○ string name;
○ double price;
○ int quantity;
2. Implement the following methods:
○ void setProductDetails(string n, double p, int q) to set the
product details.
○ double getPrice() to return the price.
○ int getQuantity() to return the quantity.
○ void printProductInfo() to display all product details.
Bonus: Add a method to increase or decrease the quantity: void updateQuantity(int
amount).
Problem 2: Create an Employee Class
Objective: Work with encapsulated methods that interact with private attributes.
Instructions:
1. Define an Employee class with the following private attributes:
○ string name;
○ int id;
○ double salary;
2. Add getter and setter methods for each attribute:
○ void setEmployeeInfo(string n, int i, double s) to set the
employee details.
○ string getName() to return the name.
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
○ double getSalary() to return the salary.
○ void printEmployeeInfo() to display the employee’s information.
Problem 3: Create a BankAccount Class (Encapsulation)
Objective: Implement proper encapsulation by protecting sensitive information.
Instructions:
1. Define a BankAccount class with the following private attributes:
○ string accountNumber;
○ double balance;
2. Add the following methods:
○ void deposit(double amount) to add money to the balance.
○ void withdraw(double amount) that deducts money if sufficient funds
exist.
○ double getBalance() to return the balance.
○ void printAccountDetails() to display the account number and balance.
Bonus: Add a void transfer(BankAccount &other, double amount) method to
transfer funds between two accounts.
Problem 4: Create a Course Class
Objective: Practice encapsulating attributes and using getter/setter methods.
Instructions:
1. Define a Course class with private attributes:
○ string courseName;
○ string courseCode;
○ int credits;
2. Implement the following methods:
○ void setCourseDetails(string name, string code, int credits)
to set course details.
○ string getCourseName() to return the course name.
○ void printCourseInfo() to display all course details.
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
Problem 5: Create a Laptop Class (with Encapsulation)
Objective: Introduce the concept of private attributes and enforce encapsulation.
Instructions:
1. Define a Laptop class with the following private attributes:
○ string brand;
○ string model;
○ double price;
2. Add getter and setter methods:
○ void setLaptopInfo(string b, string m, double p) to set the
brand, model, and price.
○ double getPrice() to return the laptop's price.
○ void printLaptopDetails() to display all laptop details.
Problem 6: Create a Rectangle Class (with Encapsulation)
Objective: Reinforce encapsulation by hiding data and accessing it via methods.
Instructions:
1. Define a Rectangle class with private attributes:
○ double width;
○ double height;
2. Implement the following methods:
○ void setDimensions(double w, double h) to set the width and height.
○ double area() to return the area of the rectangle.
○ void printRectangleDetails() to display width, height, and area.
Problem 7: Create a Movie Class
Objective: Introduce encapsulation with methods that control access to attributes.
Instructions:
1. Define a Movie class with private attributes:
○ string title;
○ string director;
○ int duration; // in minutes
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
2. Add getter and setter methods:
○ void setMovieInfo(string t, string d, int dur) to set the movie
details.
○ void printMovieDetails() to display the title, director, and duration.
Problem 8: Create a MobilePhone Class
Objective: Work with encapsulated methods to manipulate private data.
Instructions:
1. Define a MobilePhone class with private attributes:
○ string brand;
○ string model;
○ double price;
2. Add methods:
○ void setPhoneDetails(string b, string m, double p) to set the
phone’s details.
○ void printPhoneDetails() to display all phone details.
Problem 9: Create a Book Class (with Encapsulation)
Objective: Apply encapsulation principles to protect data within a class.
Instructions:
1. Define a Book class with private attributes:
○ string title;
○ string author;
○ string ISBN;
○ int publicationYear;
2. Add methods:
○ void setBookDetails(string t, string a, string isbn, int
year) to set the book’s details.
○ void printBookDetails() to display all book details.
Visit my website: https://mehtab.netlify.app/
cswithalam mehtabalam1
Problem 10: Design Your Own Encapsulated Class
Objective: Practice designing your own encapsulated class based on real-world entities.
Instructions:
1. Choose a real-world entity (e.g., House, Animal, Building, etc.).
2. Define at least three private attributes and two getter/setter methods.
3. Implement a method to print out all the class details.
Visit my website: https://mehtab.netlify.app/