abstract classes and interfaces in c++\ by M adnan Haider MNSUAM.pptx
haiderkhooradnan
17 views
16 slides
May 18, 2024
Slide 1 of 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
About This Presentation
new oop presentation
Size: 14.58 MB
Language: en
Added: May 18, 2024
Slides: 16 pages
Slide Content
Abstract Classes and Interfaces
Group # 8 Adnan Haider M Ismail Rimsha Ali Sofia Bashir 2
Overview ♦️ Introduction ♦️Abstract Classes ♦️Interfaces ♦️Comparison between Abstract Classes and Interfaces ♦️Real World Examples and use cases 3
1. Introduction: Abstract Methods: The Functions that are declared in a class but not implemented in that class are called abstract methods . They Serves as placeholder for methods that must be implemented by subclasses. These methods are typically declared in abstract classes or interfaces.(Abstract methods are achieved by declaring virtual functions without providing any implementation) . They are useful for defining a common interface that implemented in their own way.
Concrete Class: A concrete class is a class that provides implementations for all its member functions, including inherited abstract methods. It can be instantiated to create objects. Concrete classes can also have their own non-abstract methods and member variables. They serve as building blocks for creating objects with specific behavior and attributes
Pure Virtual Function: “Pure virtual function is a virtual function with no body .” A pure virtual function is a special type of virtual function in C++ that is declared in a base class but has no implementation. It is marked with = 0 at the end of its declaration. Classes containing pure virtual functions are abstract classes, meaning they cannot be instantiated directly. Instead, they serve as interfaces or blueprints for derived classes to provide implementations for the pure virtual functions. Subclasses must override all pure virtual functions to become concrete classes and be instantiated. Pure virtual functions are a key feature of achieving polymorphism and defining abstract interfaces in C++. 6
Abstract classes: “The classes that contain pure virtual functions are called abstract classes” These classes can not be instantiated directly ; they are meant to be subclassed. Abstracts methods are declared but not implemented in the abstract class itself. It serves as blueprints for other classes and can contain one or more abstract methods. 7
syntax example : # include <iostream> // Abstract class class Shape { public: // Pure virtual function (abstract method) virtual void draw() = 0; }; int main() { // Attempting to create an object of the abstract class will result in a compilation error // Shape shape ; return 0;} . 8
Example: #include <iostream >// Abstract class using namespace std; class Shape { public : // Abstract method virtual void draw() = 0; }; // Concrete subclass implementing the abstract method class Circle : public Shape {public: void draw() override { cout << "Drawing Circle\n"; }} ;// Concrete subclass implementing the abstract method class Rectangle : public Shape {public: void draw() override { cout << "Drawing Rectangle\n"; }}; 9
Example: int main() { // Create objects of concrete subclasses Circle circle ; Rectangle rectangle ; // Call methods circle.draw (); rectangle.draw (); return 0; } 10 output: Drawing Circle Drawing Rectangle
Interfaces: “An interface is a blueprint that defines a set of methods and properties that a class must implement ,without providing the actual implementation”. interfaces are typically defined using abstract classes with pure virtual functions. An abstract class is a class that cannot be instantiated on its own and contains one or more pure virtual functions, which are declared using the virtual keyword followed by = 0; syntax. 11
Rule of interfaces: 13 Interfaces in OOP define what classes can do without providing any implementation details, while abstract classes can provide both method declarations and some implementations but cannot be instantiated directly.
Abstract class Abstract class contain both virtual methods and concrete methods(with implementations) These cannot be instantiated directly. Abstract Classes Support Single inheritance. Abstract classes are used when a base class needs to provide some default behavior along with abstract methods that subclasses must implement. Interfaces contain only pure virtual methods (method without implementations). These also cannot be instantiated at all. Interfaces support multiple inheritance Interfaces are used when defining a common behavior of multiple functions or methods. INTERFACES
Real world example: . Virtual void getName ()=0; Void getName () { } Void getName () { } Class Manager Class Accountant Class Costumer Virtual void getName ()=0; Class Database
advantages in oop : Defining a Common Interface : Abstract classes set a blueprint for subclasses, ensuring they share common methods. Organizing Code : They help group related classes under a common umbrella, improving code structure. Extending Functionality : New features can be easily added by creating new subclasses of abstract classes. Enhancing Maintainability: Abstract classes make code easier to understand and maintain by encapsulating related behavior. 16