0% found this document useful (0 votes)
1 views

C++

The document contains code examples for creating various classes in C++, including Car, Rectangle, Book, Student, and BankAccount. Each class has attributes and methods for setting and displaying details, calculating area and perimeter, and managing bank transactions. The examples illustrate object-oriented programming concepts such as encapsulation and method implementation.

Uploaded by

cojogan898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C++

The document contains code examples for creating various classes in C++, including Car, Rectangle, Book, Student, and BankAccount. Each class has attributes and methods for setting and displaying details, calculating area and perimeter, and managing bank transactions. The examples illustrate object-oriented programming concepts such as encapsulation and method implementation.

Uploaded by

cojogan898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy