SlideShare a Scribd company logo
Chapter: 09


          Inheritance
              Lecture: 30 & 31
              Date: 08.10.2012
Inheritance:
Base and Derived Classes

   Inheritance is the
    process of creating a
    new classes (derived
    classes) from existing
    classes (based classes).
Inheritance:
Base and Derived Classes
Inheritance: Advantages

   Permits code reusability

   Reusing existing code saves time and money and
    increases a program’s reliability
Inheritance: Shapes
            Polygon


Rectangle
                      Triangle
No-Inheritance: Shapes
                                      class Rectangle{
            Polygon                        private:
                                             int numVertices;
                                             int xCoord, yCoord;
                                           public:
Rectangle                                    void set(int x, int y, int nV);
                           Triangle
                                             int area();
                                      };
class Polygon{
   private:                           class Triangle{
    int numVertices;                     private:
    int xCoord, yCoord;                    int numVertices;
                                          int xCoord, yCoord;
   public:
                                         public:
    void set(int x, int y, int nV);
                                          void set(int x, int y, int nV);
    int area();
                                          int area();
};                                    };
Inheritance: Shapes
                                class Polygon{
            Polygon                protected:
                                    int numVertices;
                                    float xCoord, yCoord;
                                   public:
                                    void set(int x, int y, int nV);
                                };
Rectangle
                  Triangle




                             class Rectangle : public Polygon{
                                 public:
                                   int area();
                             };
Inheritance: Shapes
                                      class Polygon{
             Polygon                     protected:
                                          int numVertices;
                                          float xCoord, yCoord;
                                         public:
                                          void set(float x, float y, int nV);
                                      };
 Rectangle
                        Triangle




class Triangle : public Polygon{   class Rectangle : public Polygon{
    public:                            public:
     int area();                         float area();
};                                 };
Inheritance: Syntax
   Syntax:
         class DerivedClassName : access-level BaseClassName
    where
       access-level specifies the type of derivation
          private by default, or

          Public



   Any class can serve as a base class
       Thus a derived class can also be a base class
Inheritance: Syntax
   Syntax:
       class DerivedClassName : access-level BaseClassName


Example:
       class BaseClass
       {

          }
         class DerivedClass: public BaseClass
         {

         }
Access Specifier without Inheritance
Access Specifier with Inheritance
Inheritance and Accessibility
Overriding member functions
   Member functions in a derived class can have the same
    names as in the base class
   When the same function exists in both the base and
    derived classes, the function in the derived class will be
    executed.
       In order to call a function in the base class use the use the scope resolution
        operator, e.g.,

                             Stack :: push()

               Name                Scope
                                                  Call to a member function
            (base class)          resolution
                                                  in base class
Stack Applications
   Memory management
   Tower of Hanoi
Stack Applications
   Converting a decimal number to binary
Inheritance: Shapes
                          x
                               class Point{
             Point
                          y        protected:
                                     int x, y;
                                   public:
     Circle      3D-Point            void set (int a, int b);
         x            x
                               };
         y            y
         r            z


class Circle : public Point{    class 3D-Point: public Point{
    private:                        private:
          double r;                       int z;
};                              };

More Related Content

Viewers also liked (9)

PPT
9781285852744 ppt ch10
Terry Yoast
 
PPT
9781285852744 ppt ch11
Terry Yoast
 
PPT
Lec 47.48 - stream-files
Princess Sam
 
PPTX
Managing console input and output
gourav kottawar
 
PPT
File handling in C++
Hitesh Kumar
 
PPT
File handling
Nilesh Dalvi
 
PDF
file handling c++
Guddu Spy
 
PDF
Object-Oriented Design: Multiple inheritance (C++ and C#)
Adair Dingle
 
9781285852744 ppt ch10
Terry Yoast
 
9781285852744 ppt ch11
Terry Yoast
 
Lec 47.48 - stream-files
Princess Sam
 
Managing console input and output
gourav kottawar
 
File handling in C++
Hitesh Kumar
 
File handling
Nilesh Dalvi
 
file handling c++
Guddu Spy
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Adair Dingle
 

Similar to Lec 30.31 - inheritance (20)

PPT
C plus plus Inheritance a complete guide
University of Sindh
 
PPT
Lecture4
Ravi Kant Kumar
 
PDF
Object-oriented Basics
Jamie (Taka) Wang
 
PPTX
Inheritance
Tech_MX
 
PDF
Java OO Revisited
Jussi Pohjolainen
 
PPT
ReviewConstDestr FOR CONSRUCTOR AND DESTRUCTORppt
gowrikcom
 
PPTX
Intro to object oriented programming
David Giard
 
PDF
Chapter 04 inheritance
Nurhanna Aziz
 
PDF
Refactoring
Yaniv Pessach
 
PPT
Constructor and Destructor PPT
Shubham Mondal
 
PPT
Lecture19
elearning_portal
 
PDF
C# Summer course - Lecture 3
mohamedsamyali
 
PDF
Mini-curso JavaFX Aula2
Raphael Marques
 
PPT
ReviewConstructorDestructorof cplusplus.ppt
aavvv
 
PPT
Implementation of interface9 cm604.30
myrajendra
 
PPT
Review constdestr
rajudasraju
 
PPT
Lecture18
elearning_portal
 
PPTX
Game modeling
priyank09
 
PDF
Lecture 4: Data Types
Eelco Visser
 
PPSX
C# 6.0 - April 2014 preview
Paulo Morgado
 
C plus plus Inheritance a complete guide
University of Sindh
 
Lecture4
Ravi Kant Kumar
 
Object-oriented Basics
Jamie (Taka) Wang
 
Inheritance
Tech_MX
 
Java OO Revisited
Jussi Pohjolainen
 
ReviewConstDestr FOR CONSRUCTOR AND DESTRUCTORppt
gowrikcom
 
Intro to object oriented programming
David Giard
 
Chapter 04 inheritance
Nurhanna Aziz
 
Refactoring
Yaniv Pessach
 
Constructor and Destructor PPT
Shubham Mondal
 
Lecture19
elearning_portal
 
C# Summer course - Lecture 3
mohamedsamyali
 
Mini-curso JavaFX Aula2
Raphael Marques
 
ReviewConstructorDestructorof cplusplus.ppt
aavvv
 
Implementation of interface9 cm604.30
myrajendra
 
Review constdestr
rajudasraju
 
Lecture18
elearning_portal
 
Game modeling
priyank09
 
Lecture 4: Data Types
Eelco Visser
 
C# 6.0 - April 2014 preview
Paulo Morgado
 
Ad

More from Princess Sam (12)

PPT
Lec 50
Princess Sam
 
PPT
Lec 49 - stream-files
Princess Sam
 
PPT
Lec 42.43 - virtual.functions
Princess Sam
 
PPT
Lec 40.41 - pointers
Princess Sam
 
PPT
Lec 38.39 - pointers
Princess Sam
 
PPT
Lec 45.46- virtual.functions
Princess Sam
 
PPT
Lec 37 - pointers
Princess Sam
 
PPT
Lec 33 - inheritance
Princess Sam
 
PPT
Lec 28 - operator overloading
Princess Sam
 
PPT
Lec 26.27-operator overloading
Princess Sam
 
PPT
Lec 25 - arrays-strings
Princess Sam
 
PPT
Lec 36 - pointers
Princess Sam
 
Lec 50
Princess Sam
 
Lec 49 - stream-files
Princess Sam
 
Lec 42.43 - virtual.functions
Princess Sam
 
Lec 40.41 - pointers
Princess Sam
 
Lec 38.39 - pointers
Princess Sam
 
Lec 45.46- virtual.functions
Princess Sam
 
Lec 37 - pointers
Princess Sam
 
Lec 33 - inheritance
Princess Sam
 
Lec 28 - operator overloading
Princess Sam
 
Lec 26.27-operator overloading
Princess Sam
 
Lec 25 - arrays-strings
Princess Sam
 
Lec 36 - pointers
Princess Sam
 
Ad

Lec 30.31 - inheritance

  • 1. Chapter: 09 Inheritance Lecture: 30 & 31 Date: 08.10.2012
  • 2. Inheritance: Base and Derived Classes  Inheritance is the process of creating a new classes (derived classes) from existing classes (based classes).
  • 4. Inheritance: Advantages  Permits code reusability  Reusing existing code saves time and money and increases a program’s reliability
  • 5. Inheritance: Shapes Polygon Rectangle Triangle
  • 6. No-Inheritance: Shapes class Rectangle{ Polygon private: int numVertices; int xCoord, yCoord; public: Rectangle void set(int x, int y, int nV); Triangle int area(); }; class Polygon{ private: class Triangle{ int numVertices; private: int xCoord, yCoord; int numVertices; int xCoord, yCoord; public: public: void set(int x, int y, int nV); void set(int x, int y, int nV); int area(); int area(); }; };
  • 7. Inheritance: Shapes class Polygon{ Polygon protected: int numVertices; float xCoord, yCoord; public: void set(int x, int y, int nV); }; Rectangle Triangle class Rectangle : public Polygon{ public: int area(); };
  • 8. Inheritance: Shapes class Polygon{ Polygon protected: int numVertices; float xCoord, yCoord; public: void set(float x, float y, int nV); }; Rectangle Triangle class Triangle : public Polygon{ class Rectangle : public Polygon{ public: public: int area(); float area(); }; };
  • 9. Inheritance: Syntax  Syntax: class DerivedClassName : access-level BaseClassName where  access-level specifies the type of derivation  private by default, or  Public  Any class can serve as a base class  Thus a derived class can also be a base class
  • 10. Inheritance: Syntax  Syntax: class DerivedClassName : access-level BaseClassName Example: class BaseClass { } class DerivedClass: public BaseClass { }
  • 12. Access Specifier with Inheritance
  • 14. Overriding member functions  Member functions in a derived class can have the same names as in the base class  When the same function exists in both the base and derived classes, the function in the derived class will be executed.  In order to call a function in the base class use the use the scope resolution operator, e.g., Stack :: push() Name Scope Call to a member function (base class) resolution in base class
  • 15. Stack Applications  Memory management  Tower of Hanoi
  • 16. Stack Applications  Converting a decimal number to binary
  • 17. Inheritance: Shapes x class Point{ Point y protected: int x, y; public: Circle 3D-Point void set (int a, int b); x x }; y y r z class Circle : public Point{ class 3D-Point: public Point{ private: private: double r; int z; }; };

Editor's Notes