C++ Classes and Objects Exercises
Here are some exercises that you can use to practice working with classes and objects in C++:
Exercise 1: Book Class
Create a Book class with attributes such as title, author, and ISBN. Include methods to display
book details and set book details.
Exercise 2: Bank Account Class
Design a BankAccount class with methods like deposit, withdraw, and getBalance. Ensure you
have a constructor to set an initial balance.
Exercise 3: Student Class
Create a Student class with attributes like name, rollNumber, and marks. Implement methods to
calculate the grade based on the marks.
Exercise 4: Car Class
Develop a Car class that contains attributes like make, model, year, and speed. Include methods
like accelerate and brake.
.Exercise 5: Library System
Develop a Library class that contains a collection of books (Book objects). Implement methods
to add a book, remove a book, and display all books.
To solve these exercises:
Start by designing the class structure with attributes and methods.
Implement constructors to initialize the objects.
Define member functions to perform various operations on objects.
Test your classes by creating objects in the main() function and calling the methods.
These exercises will help you understand how to design and implement classes and objects in C+
+, enhancing your object-oriented programming skills.
Programming Questions on Classes and Objects in C++
Here are some programming questions on classes and objects in C++ along with their respective
answers:
1. Question: Create a Car class with attributes brand, model, and year. Provide methods to
set and display these attributes.
Answer:
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
string brand;
string model;
int year;
public:
// Constructor
Car(string b, string m, int y) : brand(b), model(m), year(y) {}
// Setter method
void setDetails(string b, string m, int y) {
brand = b;
model = m;
year = y;
// Display method
void displayDetails() {
cout << “Brand: ” << brand << “, Model: ” << model << “, Year: ” << year << endl;
};
int main() {
Car myCar(“Toyota”, “Camry”, 2020);
myCar.displayDetails();
// Update details
myCar.setDetails(“Honda”, “Civic”, 2022);
myCar.displayDetails();
return 0;
2. Question: Create a Rectangle class with attributes length and width. Include methods to
calculate the area and perimeter of the rectangle.
Answer:
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
// Constructor
Rectangle(double l, double w) : length(l), width(w) {}
// Method to calculate area
double calculateArea() {
return length * width;
// Method to calculate perimeter
double calculatePerimeter() {
return 2 * (length + width);
};
int main() {
Rectangle rect(5.0, 3.0);
cout << “Area of Rectangle: ” << rect.calculateArea() << endl;
cout << “Perimeter of Rectangle: ” << rect.calculatePerimeter() << endl;
return 0;
3. Question: Define a Book class with attributes title, author, and isbn. Provide methods to
set and display these attributes.
Answer:
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title;
string author;
string isbn;
public:
// Constructor
Book(string t, string a, string i) : title(t), author(a), isbn(i) {}
// Setter method
void setDetails(string t, string a, string i) {
title = t;
author = a;
isbn = i;
// Display method
void displayDetails() {
cout << “Title: ” << title << “, Author: ” << author << “, ISBN: ” << isbn << endl;
}
};
int main() {
Book myBook(“The Alchemist”, “Paulo Coelho”, “9780062315007”);
myBook.displayDetails();
// Update details
myBook.setDetails(“1984”, “George Orwell”, “9780451524935”);
myBook.displayDetails();
return 0;