SlideShare una empresa de Scribd logo
1 de 17
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;
};                              };

Más contenido relacionado

Destacado

9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
9781285852744 ppt ch11
9781285852744 ppt ch119781285852744 ppt ch11
9781285852744 ppt ch11Terry Yoast
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and outputgourav kottawar
 
File handling in C++
File handling in C++File handling in C++
File handling in C++Hitesh Kumar
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Adair Dingle
 

Destacado (9)

9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781285852744 ppt ch11
9781285852744 ppt ch119781285852744 ppt ch11
9781285852744 ppt ch11
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
File handling
File handlingFile handling
File handling
 
file handling c++
file handling c++file handling c++
file handling c++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)Object-Oriented Design: Multiple inheritance (C++ and C#)
Object-Oriented Design: Multiple inheritance (C++ and C#)
 

Similar a Lec 30.31 - inheritance

Inheritance
InheritanceInheritance
InheritanceTech_MX
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programmingDavid Giard
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritanceNurhanna Aziz
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3mohamedsamyali
 
ReviewConstructorDestructorof cplusplus.ppt
ReviewConstructorDestructorof cplusplus.pptReviewConstructorDestructorof cplusplus.ppt
ReviewConstructorDestructorof cplusplus.pptaavvv
 
Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30myrajendra
 
Review constdestr
Review constdestrReview constdestr
Review constdestrrajudasraju
 
Game modeling
Game modelingGame modeling
Game modelingpriyank09
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data TypesEelco Visser
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewPaulo Morgado
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and VariablesEelco Visser
 

Similar a Lec 30.31 - inheritance (20)

Lecture4
Lecture4Lecture4
Lecture4
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Inheritance
InheritanceInheritance
Inheritance
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
Intro to object oriented programming
Intro to object oriented programmingIntro to object oriented programming
Intro to object oriented programming
 
Chapter 04 inheritance
Chapter 04 inheritanceChapter 04 inheritance
Chapter 04 inheritance
 
Refactoring
RefactoringRefactoring
Refactoring
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 
Lecture19
Lecture19Lecture19
Lecture19
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Mini-curso JavaFX Aula2
Mini-curso JavaFX Aula2Mini-curso JavaFX Aula2
Mini-curso JavaFX Aula2
 
ReviewConstructorDestructorof cplusplus.ppt
ReviewConstructorDestructorof cplusplus.pptReviewConstructorDestructorof cplusplus.ppt
ReviewConstructorDestructorof cplusplus.ppt
 
Implementation of interface9 cm604.30
Implementation of interface9 cm604.30Implementation of interface9 cm604.30
Implementation of interface9 cm604.30
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
 
Lecture18
Lecture18Lecture18
Lecture18
 
Game modeling
Game modelingGame modeling
Game modeling
 
Lecture 4: Data Types
Lecture 4: Data TypesLecture 4: Data Types
Lecture 4: Data Types
 
C# 6.0 - April 2014 preview
C# 6.0 - April 2014 previewC# 6.0 - April 2014 preview
C# 6.0 - April 2014 preview
 
Lecture 3: Storage and Variables
Lecture 3: Storage and VariablesLecture 3: Storage and Variables
Lecture 3: Storage and Variables
 
C# for Java Developers
C# for Java DevelopersC# for Java Developers
C# for Java Developers
 

Más de Princess Sam

Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-filesPrincess Sam
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointersPrincess Sam
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloadingPrincess Sam
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Más de Princess Sam (12)

Lec 50
Lec 50Lec 50
Lec 50
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Lec 36 - pointers
Lec 36 -  pointersLec 36 -  pointers
Lec 36 - pointers
 

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; }; };

Notas del editor

  1. Student Book
  2. Student Book
  3. Student Book
  4. Student Book
  5. Student Book
  6. Student Book
  7. Student Book
  8. Student Book
  9. Student Book
  10. Student Book
  11. Student Book
  12. Student Book
  13. Student Book
  14. Student Book
  15. Student Book
  16. Student Book
  17. Student Book