COMPUTER SCIENCE INHERITANCE,POLYMORPHISM AND VIRTUAL FUNCTIONS PRESENTED BY-PRIYANSHU MITTAL B.TECH(BIOINFORMATICS) A20204222006 3RD SEMESTER FACULTY-DR.MADHULATA MAM
Inheritance Inheritance is a mechanism in C++ that allows you to create a new class that is a modified version of an existing class. The new class (derived class) inherits attributes and behaviors from the existing class (base class). There are different types of inheritance: • Single Inheritance • Multiple Inheritance • Multi-leveI Inheritance • Hierarchical Inheritance • Hybrid Inheritance
Single Inheritance Single inheritance involves one base class and one derived class. The derived class inherits the properties and behaviors of the base class. class Animal { public: void eat() { cout << "Animal is eating." << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking." << endl; } };
Multiple Inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
class Mammal { public: void breathe() { cout << "Mammal is breathing." << endl; } }; class Bird { public: void breathe() { cout << "Bird is breathing." << endl; } }; class Bat : public Mammal, public Bird { public: // Ambiguity here; need to specify which "breathe" to use. };
Multi-level Inheritance In multilevel inheritance, a class inherits from a derived class.This creates a hierarchy of classes. class Grandparent { // ... }; class Parent : public Grandparent { // ... }; class Child : public Parent { // ... };
Hierarchial Inheritance Hierarchical inheritance involves multiple derived classes inheriting from a single base class. class Vehicle { public: void startEngine() { cout << "Vehicle engine started." << endl; } }; class Car : public Vehicle { // ... }; class Motorcycle : public Vehicle { // ... };
POINTER Pointers are variables that store memory addresses. They are a fundamental concept for dynamic memory allocation and working with data structures. In C++, you can declare and use pointers with the asterisk (*) symbol. int x = 10; int* ptr = &x; // Pointer to an integer cout << "Value of x: " << *ptr << endl; // Dereferencing the pointer
Pointer Types: Null Pointer: A null pointer doesn't point to any memory location. It's often used to represent an invalid or uninitialized pointer. int* ptr = nullptr; Pointer to an Array: Pointers can be used to traverse arrays or access their elements. int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; // Points to the first element
Pointer to a Function: Pointers can store the address of functions, allowing you to call functions indirectly. int add(int a, int b) { return a + b; } int (*ptr)(int, int) = add;
POLYMORPHISM Polymorphism is the ability of objects of different classes to be treated as objects of a common base class. There are two main types of polymorphism 1.Compile-time Polymorphism (Static Binding):: •Achieved through function overloading and operator overloading. •Resolution of the function call is done at compile-time Example-- int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
2.Run-time Polymorphism: •Achieved through virtual functions and inheritance. •Resolution of the function call is done at run-time. VIRTUAL FUNCTIONS Virtual functions are used in C++ for achieving runtime polymorphism. They are declared in the base class and can be overridden in derived classes. Virtual functions allow you to call the derived class's version of a function using a base class pointer.
class Shape { public: virtual void draw() { cout << "Drawing a shape" << endl; } }; class Circle : public Shape { public: void draw() override { cout << "Drawing a circle" << endl; } }; Example--
THANK YOU!

Polymorphism and Virtual Functions ppt bioinformatics

  • 1.
    COMPUTER SCIENCE INHERITANCE,POLYMORPHISM ANDVIRTUAL FUNCTIONS PRESENTED BY-PRIYANSHU MITTAL B.TECH(BIOINFORMATICS) A20204222006 3RD SEMESTER FACULTY-DR.MADHULATA MAM
  • 2.
    Inheritance Inheritance is amechanism in C++ that allows you to create a new class that is a modified version of an existing class. The new class (derived class) inherits attributes and behaviors from the existing class (base class). There are different types of inheritance: • Single Inheritance • Multiple Inheritance • Multi-leveI Inheritance • Hierarchical Inheritance • Hybrid Inheritance
  • 3.
    Single Inheritance Single inheritanceinvolves one base class and one derived class. The derived class inherits the properties and behaviors of the base class. class Animal { public: void eat() { cout << "Animal is eating." << endl; } }; class Dog : public Animal { public: void bark() { cout << "Dog is barking." << endl; } };
  • 4.
    Multiple Inheritance Multiple inheritanceis a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
  • 5.
    class Mammal { public: voidbreathe() { cout << "Mammal is breathing." << endl; } }; class Bird { public: void breathe() { cout << "Bird is breathing." << endl; } }; class Bat : public Mammal, public Bird { public: // Ambiguity here; need to specify which "breathe" to use. };
  • 6.
    Multi-level Inheritance In multilevelinheritance, a class inherits from a derived class.This creates a hierarchy of classes. class Grandparent { // ... }; class Parent : public Grandparent { // ... }; class Child : public Parent { // ... };
  • 7.
    Hierarchial Inheritance Hierarchical inheritanceinvolves multiple derived classes inheriting from a single base class. class Vehicle { public: void startEngine() { cout << "Vehicle engine started." << endl; } }; class Car : public Vehicle { // ... }; class Motorcycle : public Vehicle { // ... };
  • 8.
    POINTER Pointers are variablesthat store memory addresses. They are a fundamental concept for dynamic memory allocation and working with data structures. In C++, you can declare and use pointers with the asterisk (*) symbol. int x = 10; int* ptr = &x; // Pointer to an integer cout << "Value of x: " << *ptr << endl; // Dereferencing the pointer
  • 9.
    Pointer Types: Null Pointer: Anull pointer doesn't point to any memory location. It's often used to represent an invalid or uninitialized pointer. int* ptr = nullptr; Pointer to an Array: Pointers can be used to traverse arrays or access their elements. int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; // Points to the first element
  • 10.
    Pointer to aFunction: Pointers can store the address of functions, allowing you to call functions indirectly. int add(int a, int b) { return a + b; } int (*ptr)(int, int) = add;
  • 11.
    POLYMORPHISM Polymorphism is theability of objects of different classes to be treated as objects of a common base class. There are two main types of polymorphism 1.Compile-time Polymorphism (Static Binding):: •Achieved through function overloading and operator overloading. •Resolution of the function call is done at compile-time Example-- int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
  • 12.
    2.Run-time Polymorphism: •Achieved throughvirtual functions and inheritance. •Resolution of the function call is done at run-time. VIRTUAL FUNCTIONS Virtual functions are used in C++ for achieving runtime polymorphism. They are declared in the base class and can be overridden in derived classes. Virtual functions allow you to call the derived class's version of a function using a base class pointer.
  • 13.
    class Shape { public: virtualvoid draw() { cout << "Drawing a shape" << endl; } }; class Circle : public Shape { public: void draw() override { cout << "Drawing a circle" << endl; } }; Example--
  • 14.