[go: up one dir, main page]

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

Oops

The document explains Object-Oriented Programming (OOP) concepts in C++, focusing on Encapsulation, Inheritance, Polymorphism, and Abstraction. It details Inheritance types, including Single and Multiple Inheritance, and describes Polymorphism with examples of Function Overloading and Function Overriding. Code snippets illustrate these concepts, demonstrating how derived classes can inherit and override base class methods.

Uploaded by

Nitin Yadav
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)
38 views13 pages

Oops

The document explains Object-Oriented Programming (OOP) concepts in C++, focusing on Encapsulation, Inheritance, Polymorphism, and Abstraction. It details Inheritance types, including Single and Multiple Inheritance, and describes Polymorphism with examples of Function Overloading and Function Overriding. Code snippets illustrate these concepts, demonstrating how derived classes can inherit and override base class methods.

Uploaded by

Nitin Yadav
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
You are on page 1/ 13

OOPS!

OOPs in C++ uses classes (blueprints) and objects


(instances) to structure code, featuring:
Encapsulation (data + methods), Inheritance (reuse),
Polymorphism (one name, multiple forms), and
Abstraction (hide complexity).
What is Inheritance?
Inheritance is an OOP concept where a derived class (child)
acquires properties (data members) and behaviors (member
functions) from a base class (parent), promoting code
reusability and hierarchical relationships.
Types of
Inheritance :
Example :
Program
Base Class: Derived Class:
#include <iostream> // 1. Single Inheritance
using namespace std; class Dog : public Animal {
public:
// Base class for animal examples void bark() {
class Animal { cout << "Woof! Woof!" << endl;
public: }
void breathe() { Dog() {
cout << "Breathing..." << endl; cout << "Dog created" << endl;
} }
Animal() { ~Dog() {
cout << "Animal created" << endl; cout << "Dog destroyed" << endl;
} }
~Animal() { };
cout << "Animal destroyed" << endl;
}
};
// Base class for multiple inheritance int main() {
class Pet { cout << "\n===== Single Inheritance =====" << endl;
public: Dog d;
void beCute() { d.breathe();
cout << "Being adorable..." << endl; d.bark();
}
};
// 2. Multiple Inheritance cout << "\n===== Multiple Inheritance =====" << endl;
class DomesticDog : public Dog, public Pet { DomesticDog dd;
public: dd.breathe();
void fetch() { dd.bark();
cout << "Fetching the ball..." << endl; dd.beCute();
} dd.fetch();
DomesticDog() {
cout << "DomesticDog created" << endl;
}
~DomesticDog() {
cout << "DomesticDog destroyed" <<
endl;
}
};
What is a Polymrphism?
Polymorphism allows objects of different classes to be
treated as objects of a common base class, enabling
one interface with multiple implementations.
Type of Polymorphism

Type 1 Type 2

Compile-time (Static) Polymorphism: Compile-time (Static) Polymorphism:


Function/method overloading Function/method overloading
Function Overloading

Function overloading allows multiple functions with the same name but different
parameters (type, number, or order) in the same scope.

Key Points:

1.Same function name


2.Different parameter lists
3.Return type alone cannot differentiate overloaded functions
Program: #include <iostream>
using namespace std;

// Overloaded functions
void print(int a) {
cout << "Integer: " << a << endl;
}

void print(double a) {
cout << "Double: " << a << endl;
}

void print(string s) {
cout << "String: " << s << endl;
}

int main() {
print(10); // Calls print(int)
print(3.14); // Calls print(double)
print("Hello"); // Calls print(string)
return 0;
}
Function Overriding
Function overriding occurs when a derived class redefines a base class
method with the same signature (name, parameters, and return type) to provide its own
implementation.

Key Points:

1.Requires inheritance (base and derived classes)


2.Same function name, parameters, and return type
3.Achieves runtime polymorphism (using virtual functions)
Program: #include <iostream>
using namespace std;

class Base {
public:
virtual void show() { // 'virtual' enables overriding
cout << "Base class show()" << endl;
}
};

class Derived : public Base {


public:
void show() override { // 'override' keyword (optional in C++11+)
cout << "Derived class show()" << endl;
}
};

int main() {
Base* b = new Derived(); // Base pointer, Derived object
b->show(); // Calls Derived::show() (runtime decision)
delete b;
return 0;
}
Thank You

You might also like