C++
C++
Provide
methods to set and display these attributes.
Answer:
#include <iostream>
#include <string>
class Car {
private:
string brand;
string model;
int year;
public:
// Constructor
// Setter method
brand = b;
model = m;
year = y;
// Display method
void displayDetails() {
cout << “Brand: ” << brand << “, Model: ” << model << “, Year: ” << year <<
endl;
};
int main() {
myCar.displayDetails();
// Update details
myCar.displayDetails();
return 0;
#include <iostream>
class Rectangle {
private:
double length;
double width;
public:
// Constructor
double calculateArea() {
double calculatePerimeter() {
return 2 * (length + width);
};
int main() {
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>
class Book {
private:
string title;
string author;
string isbn;
public:
// Constructor
// Setter method
title = t;
author = a;
isbn = i;
// Display method
void displayDetails() {
cout << “Title: ” << title << “, Author: ” << author << “, ISBN: ” << isbn <<
endl;
};
int main() {
myBook.displayDetails();
// Update details
myBook.displayDetails();
return 0;
#include <iostream>
#include <string>
class Student {
private:
string name;
int rollNumber;
char grade;
public:
void displayDetails() {
cout << “Name: ” << name << “, Roll Number: ” << rollNumber << “, Grade:
” << grade << endl;
};
int main() {
student1.displayDetails();
return 0;
#include <iostream>
#include <string>
class BankAccount {
private:
string accountNumber;
string accountHolder;
double balance;
public:
balance += amount;
cout << “Amount deposited. Current Balance: ” << balance << endl;
} else {
balance -= amount;
cout << “Amount withdrawn. Current Balance: ” << balance << endl;
void displayBalance() {
};
int main() {
account.displayBalance();
account.deposit(2000);
account.withdraw(1000);
account.withdraw(7000); // This will display an “Insufficient balance!”
message.
return 0;