0% found this document useful (0 votes)
11 views10 pages

OOPs Assisng

The document explains key Object-Oriented Programming (OOP) concepts in C++ including encapsulation, inheritance, polymorphism, abstraction, and dynamic binding. It provides code examples for each concept, illustrating how classes and objects interact, and how features like access specifiers, virtual functions, and abstract classes are implemented. The document serves as a comprehensive guide for understanding and applying OOP principles in C++.
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)
11 views10 pages

OOPs Assisng

The document explains key Object-Oriented Programming (OOP) concepts in C++ including encapsulation, inheritance, polymorphism, abstraction, and dynamic binding. It provides code examples for each concept, illustrating how classes and objects interact, and how features like access specifiers, virtual functions, and abstract classes are implemented. The document serves as a comprehensive guide for understanding and applying OOP principles in C++.
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/ 10

Assignment: OOP Concepts in C++ Explained

Encapsulation :
Encapsulation is the concept of bundling data and methods that operate on that data into a
single unit called a class. It helps to protect data from unauthorized access by using
access specifiers such as private, protected, and public.
#include <iostream>
using namespace std;

class Student {
private:
string name;
int age;
double percentage;

public:
void setName(string n) {
name = n;
}

string getName() {
return name;
}

void setAge(int a) {
if (a > 0)
age = a;
else
cout << "Invalid age!" << endl;
}

int getAge() {
return age;
}

void setPercentage(double p) {
if (p >= 0 && p <= 100)
percentage = p;
else
cout << "Invalid percentage!" << endl;
}

double getPercentage() {
return percentage;
}
};
int main() {
Student s;

// Setting values using setters


s.setName("Satyam Gupta”);
s.setAge(22);
s.setPercentage(91.4);

// Getting and displaying values using getters


cout << "Name: " << s.getName() << endl;
cout << "Age: " << s.getAge() << endl;
cout << "Percentage: " << s.getPercentage() << "%" << endl;

return 0;
}

Example 1: Bank Account


class BankAccount {
private:
double balance;
public:
void deposit(double amount) { balance += amount; }
double getBalance() const { return balance; }
};
Example 2: Employee
class Employee {
private:
int id;
std::string name;
public:
void setInfo(int i, std::string n) { id = i; name = n; }
void displayInfo() { std::cout << "ID: " << id << ", Name: Satyam Gupta" <<
std::endl; }
};
Example 3: Rectangle
class Rectangle {
private:
double width, height;
public:
void setDimensions(double w, double h) { width = w; height = h; }
double area() { return width * height; }
};
Example 4: Configuration
class Config {
private:
std::map<std::string, std::string> settings;
public:
void set(std::string key, std::string val) { settings[key] = val; }
std::string get(std::string key) { return settings[key]; }
};
Inheritance :
Inheritance is the process by which one class can acquire the properties and methods of
another class. It promotes code reusability and establishes a parent-child relationship
between classes.
#include <iostream>
using namespace std;

// Base class
class Person {
public:
void displayInfo() {
cout << "I am a person." << endl;
}
};

// Derived class inheriting from Person


class Student : public Person {
public:
void displayStudent() {
cout << "I am Satyam Gupta, a student." << endl;
}
};

int main() {
// Creating object of Student
Student s;

// Calling functions from both classes


s.displayInfo(); // Inherited from Person
s.displayStudent(); // Own method

return 0;
}

Example 1: Single Inheritance


class Animal {
public:
void eat() { std::cout << "Eating" << std::endl; }
};
class Dog : public Animal {
public:
void bark() { std::cout << "Barking" << std::endl; }
};
Example 2: Multilevel Inheritance
class A {
public:
void displayA() { std::cout << "Class A" << std::endl; }
};
class B : public A {
public:
void displayB() { std::cout << "Class B" << std::endl; }
};
class C : public B {
public:
void displayC() { std::cout << "Class C for”Satyam Gupta” << std::endl; }
};
Example 3: Hierarchical Inheritance
class Person {};
class Student : public Person {};
class Teacher : public Person {};
Example 4: Multiple Inheritance
class Printable { public: void print() { std::cout << "Printable" << std::endl; } };
class Serializable { public: void serialize() { std::cout << "Serializable" << std::endl; } };
class Data : public Printable, public Serializable {};
Polymorphism :
Polymorphism allows a function or method to behave differently based on the object that
calls it. It is achieved through function overloading, operator overloading, and virtual
functions.
#include <iostream>
using namespace std;

// Base class
class Person {
public:
// Function Overriding Example
virtual void introduce() {
cout << "I am a person." << endl;
}
};

// Derived class
class Student : public Person {
public:
// Overriding the base class method
void introduce() override {
cout << "I am Shobhit Mishra, a student." << endl;
}

// Function Overloading Example


void showMarks(int marks) {
cout << "Marks: " << marks << endl;
}

void showMarks(int marks1, int marks2) {


cout << "Marks in Subject 1: " << marks1 << ", Subject 2: " << marks2 << endl;
}
};

int main() {
// Base class pointer pointing to derived class object for overriding
Person* p;
Student s;

p = &s;
p->introduce(); // Calls Student's overridden method

// Function Overloading calls


s.showMarks(85); // Single parameter
s.showMarks(90, 95); // Two parameters
return 0;
}

Example 1: Function Overloading


class Print {
public:
void display(int num) { std::cout << "Integer: " << num << std::endl; }
void display(std::string text) { std::cout << "Text: " << text << std::endl; }
};
Example 2: Operator Overloading
class Complex {
public:
double real, imag;
Complex operator+(Complex c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
};
Example 3: Virtual Function
class Base {
public:
virtual void show() { std::cout << "Base class" << std::endl; }
};
class Derived : public Base {
public:
void show() { std::cout << "Derived class for “Satyam Gupta << std::endl; }
};
Example 4: Runtime Polymorphism
Base* b = new Derived();
b->show(); // Calls Derived's show() method
Abstraction :
Abstraction is the process of hiding implementation details and showing only essential
features of an object. It is implemented using abstract classes and interfaces.
#include <iostream>
using namespace std;

// Abstract class
class Shape {
public:
// Pure virtual function (makes this class abstract)
virtual void area() = 0;
};

// Derived class 1
class Rectangle : public Shape {
public:
void area() override {
cout << "Area of Rectangle: length * breadth" << endl;
}
};

// Derived class 2
class Circle : public Shape {
public:
void area() override {
cout << "Area of Circle: π * radius * radius" << endl;
}
};

int main() {
// Shape s; // ❌ Error: Cannot instantiate abstract class

// Pointers to base class


Shape* shapePtr;

Rectangle r;
Circle c;

// Abstraction in action — only essential info shown via area()


shapePtr = &r;
shapePtr->area();

shapePtr = &c;
shapePtr->area();
return 0;
}

Example 1: Abstract Class


class Shape {
public:
virtual void draw() = 0;
};
Example 2: Interface Implementation
class Serializable {
public:
virtual std::string serialize() = 0;
};
Example 3: Encapsulated Data
class Database {
private:
void connect() { /* code to connect */ }
public:
void executeQuery() { connect(); /* execute */ }
};
Example 4: File System
class File {
public:
virtual void open() = 0;
virtual void close() = 0;
};
Dynamic Binding :
Dynamic binding refers to the linking of a function call to its function definition at
runtime. It is implemented using virtual functions and base class pointers.
#include <iostream>
using namespace std;

// Base class
class Person {
public:
// Virtual function
virtual void show() {
cout << "I am a person." << endl;
}
};

// Derived class
class Student : public Person {
public:
// Overriding the virtual function
void show() override {
cout << "I am Satyam Guptaa, a student." << endl;
}
};

int main() {
// Base class pointer
Person* personPtr;

// Derived class object


Student s;

// Base class pointer pointing to derived class object


personPtr = &s;

// Dynamic binding — calls derived class version at runtime


personPtr->show();

return 0;
}

Example 1: Virtual Destructor


class Base {
public:
virtual ~Base() { std::cout << "Base Destructor" << std::endl; }
};
Example 2: Late Binding
class Animal {
public:
virtual void sound() { std::cout << "Animal sound" << std::endl; }
};
class Dog : public Animal {
public:
void sound() { std::cout << "Bark for Shobhit Mishra" << std::endl; }
};
Example 3: Callback Example
class Callback {
public:
virtual void onClick() = 0;
};
Example 4: Runtime Polymorphism
Animal* a = new Dog();
a->sound(); // Calls Dog's sound method

You might also like