0% found this document useful (0 votes)
8 views13 pages

C++ OOP Concepts: Encapsulation, Inheritance, Polymorphism

The document provides examples of object-oriented programming concepts in C++, including encapsulation, inheritance, and polymorphism. It contains code snippets demonstrating how to implement classes such as BankAccount, Student, Car, Vehicle, Circle, and Animal, along with their respective functionalities. Each section includes a main function that showcases the usage of these classes and outputs the results.
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)
8 views13 pages

C++ OOP Concepts: Encapsulation, Inheritance, Polymorphism

The document provides examples of object-oriented programming concepts in C++, including encapsulation, inheritance, and polymorphism. It contains code snippets demonstrating how to implement classes such as BankAccount, Student, Car, Vehicle, Circle, and Animal, along with their respective functionalities. Each section includes a main function that showcases the usage of these classes and outputs the results.
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

1.

Encapsulation :

(a)

#include <iostream>

using namespace std;

class BankAccount {

private:

double balance;

public:

BankAccount() {

balance = 0;

void deposit(double amount) {

balance += amount;

void withdraw(double amount) {

balance -= amount;

double getBalance() {

return balance;
}

};

int main() {

BankAccount b;

[Link](50.0);

[Link](20.0);

cout << "Balance: " << [Link]();

Output:
Balance 30.0

(b)

#include <iostream>

using namespace std;

class Student {

private:

string name;

char grade;

public:

void setData(string n, char g) {

name = n;

grade = g;

}
void getData() {

cout << "Name: " << name << endl;

cout << "Grade: " << grade << endl;

};

int main() {

Student s;

[Link]("Ali", 'A');

[Link]();

Output:
Name :Ali
Grade : A

(c)

#include <iostream>

using namespace std;

class Car {

private:

int speed;

public:
Car() {

speed = 0;

void accelerate() {

speed += 10;

void brake() {

speed -= 5;

int getSpeed() {

return speed;

};

int main() {

Car c;

[Link]();

[Link]();

cout << "Speed: " << [Link]();

Output:
Speed : 5
2. Inheritance :

(a)

#include <iostream>

using namespace std;

class Vehicle {

public:

string brand;

};

class Car : public Vehicle {

public:

int doors;

void displayDetails() {

cout << "Brand: " << brand << endl;

cout << "Doors: " << doors << endl;

};

int main() {

Car c;

[Link] = "Toyota";

[Link] = 4;

[Link]();
}

Output:
Brand : Toyota

Doors: 4

(b)

#include <iostream>

using namespace std;

class Shape {

public:

double radius;

};

class Circle : public Shape {

public:

double calculateArea() {

return 3.14 * radius * radius;

};

int main() {

Circle c;

[Link] = 5.0;

cout << "Area: " << [Link]();

}
Output:
Area : 78.5

(c)

#include <iostream>

using namespace std;

class Employee {

public:

string name;

};

class Manager : public Employee {

public:

string department;

void displayEmployeeDetails() {

cout << "Name: " << name << endl;

cout << "Department: " << department << endl;

};

int main() {

Manager m;

[Link] = "Ahmed";
[Link] = "IT";

[Link]();

Output:
Name: Ahmed
Department: IT

3. Polymorphism :

(a)

#include <iostream>

using namespace std;

class Shape {

public:

virtual void draw() {

cout << "Drawing Shape" << endl;

};

class Circle : public Shape {

public:

void draw() {

cout << "Drawing Circle" << endl;

}
};

class Rectangle : public Shape {

public:

void draw() {

cout << "Drawing Rectangle" << endl;

};

int main() {

Shape* s;

Circle c;

Rectangle r;

s = &c;

s->draw();

s = &r;

s->draw();

Output:
Draw circle
Draw rectangle

(b)
#include <iostream>

using namespace std;

class Animal {

public:

virtual void sound() {

cout << "Animal sound" << endl;

};

class Dog : public Animal {

public:

void sound() {

cout << "Dog barks" << endl;

};

class Cat : public Animal {

public:

void sound() {

cout << "Cat meows" << endl;

};

int main() {

Animal* a;
Dog d;

Cat c;

a = &d;

a->sound();

a = &c;

a->sound();

Output:
Dog brak
Cat meow

(c)

#include <iostream>

using namespace std;

class BankAccount {

public:

virtual double calculateInterest() {

return 0;

};
class SavingsAccount : public BankAccount {

public:

double calculateInterest() {

return 1000 * 0.05;

};

class CurrentAccount : public BankAccount {

public:

double calculateInterest() {

return 1000 * 0.02;

};

int main() {

BankAccount* b;

SavingsAccount s;

CurrentAccount c;

b = &s;

cout << "Savings Interest: " << b->calculateInterest() << endl;

b = &c;

cout << "Current Interest: " << b->calculateInterest();

Output:
Saving: 50
Current: 20

You might also like