0% found this document useful (0 votes)
6 views16 pages

Data Abstraction in C++

The document provides an overview of key Object-Oriented Programming concepts in C++, including data abstraction, encapsulation, and inheritance. It defines each concept and illustrates them with code examples, demonstrating how to implement classes, access specifiers, and various inheritance types. The document also emphasizes the benefits of these principles, such as security, reusability, and maintainability.

Uploaded by

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

Data Abstraction in C++

The document provides an overview of key Object-Oriented Programming concepts in C++, including data abstraction, encapsulation, and inheritance. It defines each concept and illustrates them with code examples, demonstrating how to implement classes, access specifiers, and various inheritance types. The document also emphasizes the benefits of these principles, such as security, reusability, and maintainability.

Uploaded by

drsomeshdewangan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SHRI SHANKARACHARYA TECHNICAL CAMPUS

Data Abstraction in C++

Definition:
Data abstraction is the process of hiding implementation details and showing only the
essential features to the user.
It helps in achieving security, reusability, and simplicity in Object-Oriented Programming.

In C++, abstraction is mainly achieved using:

1. Classes (public interface, private data)

2. Access specifiers (public, private, protected)

3. Abstract classes (with pure virtual functions)

Examples

Example 1: Using Class with Access Specifiers

#include <iostream>

using namespace std;

class Account {

private:

double balance; // Hidden from outside (implementation detail)

public:

Account(double initial) {

balance = initial;

void deposit(double amount) {

balance += amount;

void withdraw(double amount) {

if (amount <= balance) {

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 1


SHRI SHANKARACHARYA TECHNICAL CAMPUS

balance -= amount;

} else {

cout << "Insufficient balance!" << endl;

void displayBalance() { // Only essential detail exposed

cout << "Balance: " << balance << endl;

};

int main() {

Account acc(500);

[Link](200);

[Link](100);

[Link](); // User sees only balance, not inner working

return 0;

Example 2: Abstract Class with Pure Virtual Function

#include <iostream>

using namespace std;

// Abstract class

class Shape {

public:

virtual void draw() = 0; // Pure virtual function (only essential operation defined)

};

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 2


SHRI SHANKARACHARYA TECHNICAL CAMPUS

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing Circle" << endl;

};

class Rectangle : public Shape {

public:

void draw() override {

cout << "Drawing Rectangle" << endl;

};

int main() {

Shape* s1 = new Circle();

Shape* s2 = new Rectangle();

s1->draw();

s2->draw();

delete s1;

delete s2;

return 0;

Example 3: Abstraction with Function Interface

#include <iostream>

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 3


SHRI SHANKARACHARYA TECHNICAL CAMPUS

#include <cmath>

using namespace std;

class Calculator {

public:

double getSquareRoot(double x) {

return sqrt(x); // Implementation detail hidden

double getPower(double base, double exp) {

return pow(base, exp); // User doesn’t need to know how pow() works

};

int main() {

Calculator calc;

cout << "Square root of 25: " << [Link](25) << endl;

cout << "2 raised to 5: " << [Link](2, 5) << endl;

return 0;

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 4


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Data Encapsulation in C++

Definition:
Encapsulation is the process of binding data (variables) and methods (functions) together
into a single unit (class), and controlling access using access specifiers (private, public,
protected).

It ensures:

• Data hiding (restricting direct access).

• Security (only controlled functions can access/modify data).

• Modularity & maintainability.

Examples

Example 1: Bank Account (Basic Encapsulation)

#include <iostream>

using namespace std;

class BankAccount {

private:

int accountNumber;

double balance; // Data hidden

public:

// Setter function

void setAccount(int accNo, double bal) {

accountNumber = accNo;

balance = bal;

// Getter function

void display() {

cout << "Account Number: " << accountNumber << endl;

cout << "Balance: " << balance << endl;

};

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 5


SHRI SHANKARACHARYA TECHNICAL CAMPUS

int main() {

BankAccount acc;

[Link](12345, 5000.75); // Data accessed only through function

[Link]();

return 0;

Example 2: Student Information (Setters & Getters)

#include <iostream>

using namespace std;

class Student {

private:

string name;

int age;

public:

// Setter functions

void setName(string n) {

name = n;

void setAge(int a) {

if (a > 0) age = a; // Validation

// Getter functions

string getName() {

return name;

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 6


SHRI SHANKARACHARYA TECHNICAL CAMPUS

int getAge() {

return age;

};

int main() {

Student s1;

[Link]("Rahul");

[Link](20);

cout << "Student Name: " << [Link]() << endl;

cout << "Student Age: " << [Link]() << endl;

return 0;

Example 3: Employee Salary (Encapsulation with Validation)

#include <iostream>

using namespace std;

class Employee {

private:

string empName;

double salary;

public:

void setEmployee(string name, double sal) {

empName = name;

if (sal >= 0) // validation

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 7


SHRI SHANKARACHARYA TECHNICAL CAMPUS

salary = sal;

else

salary = 0;

void showEmployee() {

cout << "Employee: " << empName << endl;

cout << "Salary: " << salary << endl;

};

int main() {

Employee e1;

[Link]("Anita", 45000);

[Link]();

Employee e2;

[Link]("Ravi", -5000); // Invalid, salary set to 0

[Link]();

return 0;

Inheritance in C++

Definition:
Inheritance is an OOP feature that allows a class (derived/child) to acquire the properties
and functions of another class (base/parent).

Types of Inheritance in C++:

1. Single Inheritance

2. Multiple Inheritance

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 8


SHRI SHANKARACHARYA TECHNICAL CAMPUS

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

Type 1: Single Inheritance

One base class → One derived class.

Example

#include <iostream>

using namespace std;

class Animal { // Base class

public:

void eat() {

cout << "Animal eats food\n";

};

class Dog : public Animal { // Derived class

public:

void bark() {

cout << "Dog barks\n";

};

int main() {

Dog d;

[Link](); // Inherited

[Link](); // Own

return 0;

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 9


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Diagram

Animal (Base)

Dog (Derived)

Type 2: Multiple Inheritance

One derived class → inherits from multiple base classes.

Example

#include <iostream>

using namespace std;

class Engine {

public:

void start() { cout << "Engine started\n"; }

};

class Wheels {

public:

void rotate() { cout << "Wheels rotating\n"; }

};

class Car : public Engine, public Wheels { // Multiple inheritance

public:

void drive() { cout << "Car is driving\n"; }

};

int main() {

Car c;

[Link]();

[Link]();

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 10


SHRI SHANKARACHARYA TECHNICAL CAMPUS

[Link]();

return 0;

Diagram

Engine Wheels

\ /

\ /

Car (Derived)

Type 3: Multilevel Inheritance

A derived class becomes a base for another derived class.

Example

#include <iostream>

using namespace std;

class LivingBeing {

public:

void breathe() { cout << "Breathing...\n"; }

};

class Animal : public LivingBeing {

public:

void move() { cout << "Animal is moving\n"; }

};

class Bird : public Animal {

public:

void fly() { cout << "Bird is flying\n"; }

};

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 11


SHRI SHANKARACHARYA TECHNICAL CAMPUS

int main() {

Bird b;

[Link](); // From LivingBeing

[Link](); // From Animal

[Link](); // Own

return 0;

Diagram

LivingBeing (Base)

Animal

Bird (Derived)

4. Hierarchical Inheritance

One base class is inherited by multiple derived classes.

Example 1: Shape → Circle & Rectangle

#include <iostream>

using namespace std;

class Shape {

public:

void area() { cout << "Calculating area...\n"; }

};

class Circle : public Shape {

public:

void drawCircle() { cout << "Drawing Circle\n"; }

};

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 12


SHRI SHANKARACHARYA TECHNICAL CAMPUS

class Rectangle : public Shape {

public:

void drawRectangle() { cout << "Drawing Rectangle\n"; }

};

int main() {

Circle c;

Rectangle r;

[Link](); [Link]();

[Link](); [Link]();

return 0;

Diagram

Shape (Base)

/ \

Circle Rectangle

Example 2: Vehicle → Car & Bike

class Vehicle {

public:

void start() { cout << "Vehicle started\n"; }

};

class Car : public Vehicle {

public:

void drive() { cout << "Car driving\n"; }

};

class Bike : public Vehicle {

public:

void ride() { cout << "Bike riding\n"; }

};

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 13


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Diagram

Vehicle (Base)

/ \

Car Bike

Example 3: Person → Student & Teacher

class Person {

public:

void speak() { cout << "Person speaking\n"; }

};

class Student : public Person {

public:

void study() { cout << "Student studying\n"; }

};

class Teacher : public Person {

public:

void teach() { cout << "Teacher teaching\n"; }

};

Diagram

Person (Base)

/ \

Student Teacher

5. Hybrid Inheritance

Combination of two or more types of inheritance (like multiple + multilevel).

Example 1: A → B → D and C → D

#include <iostream>

using namespace std;

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 14


SHRI SHANKARACHARYA TECHNICAL CAMPUS

class A { public: void showA(){ cout << "Class A\n"; } };

class B : public A { public: void showB(){ cout << "Class B\n"; } };

class C { public: void showC(){ cout << "Class C\n"; } };

class D : public B, public C { public: void showD(){ cout << "Class D\n"; } };

int main(){

D obj;

[Link](); [Link](); [Link](); [Link]();

return 0;

Diagram

B C

\ /

\ /

Example 2: Student (Person + Learner + ExamResult)

class Person { public: void info(){ cout << "I am a person\n"; } };

class Learner : public Person { public: void study(){ cout << "Studying\n"; } };

class Exam { public: void marks(){ cout << "Exam marks shown\n"; } };

class Student : public Learner, public Exam { public: void grade(){ cout << "Grade A\n"; } };

Diagram

Person

Learner Exam

\ /

\ /

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 15


SHRI SHANKARACHARYA TECHNICAL CAMPUS

Student

Example 3: Employee (Person + Worker + Manager)

class Person { public: void speak(){ cout << "Speaking\n"; } };

class Worker : public Person { public: void work(){ cout << "Working\n"; } };

class Manager { public: void manage(){ cout << "Managing\n"; } };

class Employee : public Worker, public Manager { public: void role(){ cout << "Employee
role\n"; } };

Diagram

Person

Worker Manager

\ /

\ /

Employee

Final Unified Diagram (All 5 Types)

1) Single: 2) Multiple: 3) Multilevel: 4) Hierarchical: 5) Hybrid:

Base Base1 Base2 Base Base A

| \ / | / \ |

Derived Derived Derived1 Derived1 Derived2 B C

| \ /

derived2 \/

Derived

DR. SOMESH KUMAR DEWANGAN DEPARTMENT OF CSE 16

You might also like