Object Oriented Programming: Inheritance and Polymorphism

Object Oriented Programming: Inheritance and Polymorphism
Slide Note
Embed
Share

This content delves into Object Oriented Programming concepts, specifically focusing on Inheritance and Polymorphism. It covers topics like operator overloading, unary and binary operator overloading, and provides examples and explanations using C++ programming language. The material includes code snippets, class definitions, and practical demonstrations to enhance understanding and implementation of OOP principles.

  • Object Oriented Programming
  • Inheritance
  • Polymorphism
  • Operator Overloading
  • C++

Uploaded on Mar 03, 2025 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 053 CS 45 OBJECT ORIENTED PROGRAMMING Degree 2ndSemester of second year By Dr. Prabakaran Narayanan Associate professor, CSISE DEPT

  2. INHERITANCE AND POLYMORPHISM

  3. 3.1. Operator overloading It provides a flexible option for the creation of new definition for most of the c++ operators Syntax: Return Type Classname operator symbol ( argument list) { .. } Ex: Complex Complex operator + ( Complex c1) { Complex temp; . return temp; }

  4. 1. Unary operator overloading 2. Binary operator overloading 1. Unary operator overloading Take just one operand Syntax: operator object; Ex: - S; 2. Binary operator overloading take two operand Syntax: a + b; Ex: Complex (Complex a, Complex b)

  5. #include<iostream.h> Class UnaryMinus { private: public: int x, y, z; void getdata(int a, int b, int c); { x=a; y=b; z=c; }

  6. void operator-() { x=-x; y=-y; z=-z; } void display() { cout << x; cout << y; cout << z; } };

  7. void main() { UnaryMinus minus; minus.getdata(10, -30, 40); minus.display(); -minus; cout << unary minus is ; minus.display(); }

  8. #include<iostream.h> Class complex { private: public: int real; float img; Complex( int r, float f) { real = r; img = f; }

  9. complex complex operator + (Complex c2) { Complex temp; temp. real = real + c2.real; templ.img = img + c2.real; return temp; } void display() { cout << real << i ; cout << img << \n ; } };

  10. Void main() { } output: 4 +i6 7 +i4 Complex c1(4,6), c2(7,4), c3(); c1.display(); c2. display(); c3= c1 + c2; c3.display(); 11 +i 10

  11. #include<iostream.h> Class complex { private: public: int real; float img; Complex( int r, float f) { real = r; img = f; }

  12. Complex c2); { } void display() { } }; friend complex complex operator + (Complex c1, Complex temp; temp. real = c1.real + c2.real; templ.img = c1. img + c2.real; return temp cout << real << +i ; cout << img << \n ;

  13. complex complex :: operator + (Complex c1, Complex c2) { Complex temp; temp. real = c1.real + c2.real; templ.img = c1. img + c2.real; return temp; }

  14. Void main() { } output: 4 +i6 7 +i4 Complex c1(4,6), c2(7,4), c3(); c1.display(); c2. display(); c3= c1 + c2; c3.display(); 11 +i 10

  15. Inheritance The mechanism of deriving a new class from an old class is called inheritance Reusing the properties of the existing classes.

  16. Derived and base classes A derived class automatically has all the members of the base class This includes both data members and member functions It usually also has additional member functions and/or data members Terminology The base class is often called the parent The derived class is call the child

  17. Characteristics of derived classes Since the derived class can add data members and member functions, it is often larger than the base class The derived class is more specific than its base class and represents a smaller group of objects The real strength of inheritance comes from the ability to define additions, replacements or refinements to the features inherited from the base class.

  18. 3.2. Types of Inheritance in C++ In C++, we have 5 different types of Inheritance. Namely, 1. Single Inheritance 2. Multilevel Inheritance 3. Multiple Inheritance 4. Hierarchical Inheritance 5. Hybrid Inheritance (also known as Virtual Inheritance)

  19. 1. Single Inheritance In this type of inheritance one derived class inherits from only one base class. It is the most simplest form of Inheritance.

  20. 2. Multilevel Inheritance in C++ In this type of inheritance the derived class inherits from a class, which in turn inherits from some other class. The Super class for one, is sub class for the other. Super class sub class Derived class

  21. 3.Multiple Inheritance in C++ In this type of inheritance a single derived class may inherit from two or more than two base classes. Base Class Base Class Derived Class

  22. 4.Hierarchical Inheritance in C++ In this type of inheritance, multiple derived classes inherits from a single base class.

  23. 5.Hybrid (Virtual) Inheritance in C++ Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

  24. 3.3.Virtual base class in C++ Virtual base class used in virtual inheritance is a way of preventing multiple instances of a given class appearing in an inheritance hierarchy when using multiple inheritance.

  25. Class A { Int b; }; Class D1: public A { }; Class D2: public A { . }; Class D3: public D1, public D2 { } Void main() { D3 obj; obj.b=40; // error obj.b=30; // error

  26. Class A { Int b; }; Class D1:virtual public A { }; Class D2: virutal public A { . }; Class D3: public D1, public D2 { } Void main() { D3 obj; obj.b=40; // correct obj.b=30; // correct }

  27. 3.4. Abstract class It is class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function You declare a pure virtual function by using a pure specifier (=0) in the declaration of a virtual member function in the class declaration. Other class can inherit Abstract class cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

  28. Class Box { public: virtual double getVolume()=0; Pure Virtual function } private: double length; double breadth; double height;

  29. #include<iostream> using namespace std; class Base { int x; public: virtual void fun() = 0; int getX() { return x; } }; // This class inherits from Base and implements fun() class Derived: public Base { int y; public: void fun() { cout << "fun() called"; } }; int main(void) { Derived d; d.fun(); return 0; } Output: fun() called

  30. 3.5. Exception handling Errors can be broadly categorized into two types . Compile Time Errors Run Time Errors Compile Time Errors Errors caught during compiled time is called Compile time errors. Compile time errors include library reference, syntax error or incorrect class import. Run Time Errors - They are also known as exceptions. An exception caught during run time creates serious issues. An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running such as an attempt to divide by zero. Three keywords try catch throw

  31. Try - a try block identifies a block of code for which particular exceptions will be activated. Its followed by one or more catch blocks Catch- A program catches an exception with an exception handler at the place in a program where you want to handle the problem. Throw- a program throws an exception when a problem shows up.

  32. Three keywords: try, throw, catch: the try statement allows you to define a block of code to be tested for errors while it is being executed. The throw keyword throws an exception when a problem is detected

  33. Try { // protected code .. }catch(ExceptionName1 e1) { // catch block }catch(ExceptionName2 e2) { // catch block }

  34. example Try { z= 50/0; cout << z; }catch(exception e) { cout<< e; }

  35. #inlclude <iostream.h> Double division(int a, int b) { if (b==0) { throw division by zero condition ; } return (a/b) } Int main() { int x=50; int y=0; double z=0; } try { }catch(const char* msg) { cerr << msg; } return 0; z= division(x,y); cout << z;

  36. 3.6. Virtual functions A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword. It is used to tell the compiler to perform dynamic linkage or late binding on the function

  37. Example of virtual functions Class Base { }; Class Derived: }; Void main() { } public: void display() { } cout << I am in Base class ; public: void display() { } cout << I am in Derived Class ; Base *b= new Derived ; b-> show() getch(); Out put : I am in Base class

  38. Example of virtual functions Class Base { }; Class Derived: }; Void main() { } public: virtual void display() { } cout << I am in Base class ; public: void display() { } cout << I am in Derived Class ; Base *b= new Derived ; // Run Time Polymorphism b-> show() getch(); Out put : I am in Derived class

  39. 3.7. Runtime Polymorphism Runtime polymorphism is also known as dynamic polymorphism or late binding. In runtime polymorphism, the function call is resolved at run time. In contrast, to compile time or static polymorphism, the compiler deduces the object at run time and then decides which function call to bind to the object

  40. 3.8. Pure Virtual Function A pure virtual function is a virtual function in C++ for which we need not to write any function definition and only we have to declare it. It is declared by assigning =0 in the declaration. An abstract class is a class in C++ which have at least one pure virtual function.

  41. What is the difference between virtual and pure virtual function in C++? 'virtual function' has its definition in the base class and also the inheriting derived classes redefine it. Ex: virtual void show() The pure virtual function has no definition in the base class, and all the inheriting derived classes has to redefine it. Ex: virtual void show()=0

  42. THANK YOU

More Related Content