1. 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. 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. 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;
4. Question: Define a Student class with attributes name,
rollNumber, and grade. Provide a method to display the student
details.
Solution:
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name;
int rollNumber;
char grade;
public:
Student(string n, int r, char g) : name(n), rollNumber(r), grade(g) {}
void displayDetails() {
cout << “Name: ” << name << “, Roll Number: ” << rollNumber << “, Grade:
” << grade << endl;
};
int main() {
Student student1(“John Doe”, 101, ‘A’);
student1.displayDetails();
return 0;
5. Question: Create a BankAccount class with attributes
accountNumber, accountHolder, and balance. Implement methods
to deposit and withdraw money. Ensure to display an appropriate
message if a withdrawal amount exceeds the available balance.
Solution:
#include <iostream>
#include <string>
using namespace std;
class BankAccount {
private:
string accountNumber;
string accountHolder;
double balance;
public:
BankAccount(string num, string holder, double bal) : accountNumber(num),
accountHolder(holder), balance(bal) {}
void deposit(double amount) {
balance += amount;
cout << “Amount deposited. Current Balance: ” << balance << endl;
void withdraw(double amount) {
if (amount > balance) {
cout << “Insufficient balance!” << endl;
} else {
balance -= amount;
cout << “Amount withdrawn. Current Balance: ” << balance << endl;
void displayBalance() {
cout << “Account Number: ” << accountNumber << “, Holder: ” <<
accountHolder << “, Balance: ” << balance << endl;
};
int main() {
BankAccount account(“123456789”, “Alice”, 5000);
account.displayBalance();
account.deposit(2000);
account.withdraw(1000);
account.withdraw(7000); // This will display an “Insufficient balance!”
message.
return 0;