Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

download Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

of 7

Transcript of Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    1/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Search thissi

    C++ InheritanceAdvertisements

    One of the most important concepts in object-oriented programming is that of inheritance. Inheritanceallows us to define a class in terms of another class, which makes it easier to create and maintain anapplication. This also provides an opportunity to reuse the code functionality and fast implementationtime.

    When creating a class, instead of writing completely new data members and member functions, theprogrammer can designate that the new class should inherit the members of an existing class. This

    existing class is called the baseclass , and the new class is referred to as the derivedclass.

    The idea of inheritance implements the isarelationship. For example, mammal IS-A animal, dog IS-Amammal hence dog IS-A animal as well and so on.

    Base & Derived Classes:

    A class can be derived from more than one classes, which means it can inherit data and functionsfrom multiple base classes. To define a derived class, we use a class derivation list to specify thebase class (es). A class derivation list names one or more base classes and has the form:

    Previous Page Next Page

    HOME JAVA PHP Python Ruby Perl HTML CSS Javascript MySQL C++ UNIX MORE... REFERENCES | FO

    C++ Basics

    C++ Home

    C++ Overview

    C++ Environment Setup

    C++ Basic Syntax

    C++ Comments

    C++ Data Types

    C++ Variable Types

    C++ Variable Scope

    http://googleads.g.doubleclick.net/aclk?sa=l&ai=Cs7MD9kTtUpmuIqXe0AG0w4CgD__MqYIEl6Sy8aoBkfyC4wkQASCFtdsFUOT35_X9_____wFgyQagAe2_3_4DyAEDqQK_JEO9wVitPqgDAcgDwQSqBKgBT9DRm6wbACEHS-QrF66NoEzJjDwvYVkXuGrHlWoUYbpzFFbkYTWnEysyAJIhwJwtAncGUhTgU8iLEblDQ23cQ6iPidM-jj0OjZNHwlKmsS8jWOjQXNvkoJojK3H1cCY76dpJAE2DYn1eAmknHIKJHUWv0Z99jn_U3k2L4A2_ppcJTkVe-oY4OklGB2mAPbT5ixZF8vbALM-_erJqi674rKN2bhL8BZ2QoAYDgAf7v6AB&num=1&sig=AOD64_2bJ0g8m71fpjWrV0nkV1CTC5hU1Q&client=ca-pub-7133395778201029&adurl=http://www.aspose.com/total-component-suite-java.aspxhttp://googleads.g.doubleclick.net/aclk?sa=l&ai=Cvu7l9kTtUrjVEefQ0AGe7YHYBuXT8_wDvb3J-U6m1qTbNBABIIW12wVQl_jlkv7_____AWDJBqAB3Z6U_wPIAQKoAwHIA8EEqgSkAU_Q5HOlmStkSPRM2m_DJkRdwf6X7VWO4jeQusQhkDRojsbutQgnPwmHEtwj6FH57Nvx_qfYsyzJkQhfV2VWLUSWbKgFIiIujP6PriMcPdZTTQ3dXVHlNaSBn1sJitLw4LGDMHpFnSz0PigcVGcPfJQvrGBmRbcvaTCV4SjEiRsYpNH2kedRqEPA5Kb4M1Fvh5qrgOsA_BoGN2TwS52Xd7xj0orKoAYCgAeL4Ws&num=1&sig=AOD64_0Cwpc-vzXSjVWjLxAJKSFVh2q2_w&client=ca-pub-7133395778201029&adurl=http://www2.smartbear.com/DevCCPRCTrials_collaborator-aw-gray.html%3F_kk%3D%26_kt%3D9c29b181-dce6-40db-b076-240cb338eea3http://googleads.g.doubleclick.net/aclk?sa=l&ai=Cvu7l9kTtUrjVEefQ0AGe7YHYBuXT8_wDvb3J-U6m1qTbNBABIIW12wVQl_jlkv7_____AWDJBqAB3Z6U_wPIAQKoAwHIA8EEqgSkAU_Q5HOlmStkSPRM2m_DJkRdwf6X7VWO4jeQusQhkDRojsbutQgnPwmHEtwj6FH57Nvx_qfYsyzJkQhfV2VWLUSWbKgFIiIujP6PriMcPdZTTQ3dXVHlNaSBn1sJitLw4LGDMHpFnSz0PigcVGcPfJQvrGBmRbcvaTCV4SjEiRsYpNH2kedRqEPA5Kb4M1Fvh5qrgOsA_BoGN2TwS52Xd7xj0orKoAYCgAeL4Ws&num=1&sig=AOD64_0Cwpc-vzXSjVWjLxAJKSFVh2q2_w&client=ca-pub-7133395778201029&adurl=http://www2.smartbear.com/DevCCPRCTrials_collaborator-aw-gray.html%3F_kk%3D%26_kt%3D9c29b181-dce6-40db-b076-240cb338eea3http://googleads.g.doubleclick.net/aclk?sa=l&ai=Cvu7l9kTtUrjVEefQ0AGe7YHYBuXT8_wDvb3J-U6m1qTbNBABIIW12wVQl_jlkv7_____AWDJBqAB3Z6U_wPIAQKoAwHIA8EEqgSkAU_Q5HOlmStkSPRM2m_DJkRdwf6X7VWO4jeQusQhkDRojsbutQgnPwmHEtwj6FH57Nvx_qfYsyzJkQhfV2VWLUSWbKgFIiIujP6PriMcPdZTTQ3dXVHlNaSBn1sJitLw4LGDMHpFnSz0PigcVGcPfJQvrGBmRbcvaTCV4SjEiRsYpNH2kedRqEPA5Kb4M1Fvh5qrgOsA_BoGN2TwS52Xd7xj0orKoAYCgAeL4Ws&num=1&sig=AOD64_0Cwpc-vzXSjVWjLxAJKSFVh2q2_w&client=ca-pub-7133395778201029&adurl=http://www2.smartbear.com/DevCCPRCTrials_collaborator-aw-gray.html%3F_kk%3D%26_kt%3D9c29b181-dce6-40db-b076-240cb338eea3http://www.tutorialspoint.com/ruby/index.htmhttp://www.tutorialspoint.com/perl/index.htmhttp://www.tutorialspoint.com/html/index.htmhttp://www.tutorialspoint.com/css/index.htmhttp://www.tutorialspoint.com/javascript/index.htmhttp://www.tutorialspoint.com/javascript/index.htmhttp://www.tutorialspoint.com/mysql/index.htmhttp://www.tutorialspoint.com/mysql/index.htmhttp://www.tutorialspoint.com/cplusplus/index.htmhttp://www.tutorialspoint.com/unix/index.htmhttp://www.tutorialspoint.com/more.htmhttp://www.tutorialspoint.com/references.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htmhttp://googleads.g.doubleclick.net/aclk?sa=l&ai=Cs7MD9kTtUpmuIqXe0AG0w4CgD__MqYIEl6Sy8aoBkfyC4wkQASCFtdsFUOT35_X9_____wFgyQagAe2_3_4DyAEDqQK_JEO9wVitPqgDAcgDwQSqBKgBT9DRm6wbACEHS-QrF66NoEzJjDwvYVkXuGrHlWoUYbpzFFbkYTWnEysyAJIhwJwtAncGUhTgU8iLEblDQ23cQ6iPidM-jj0OjZNHwlKmsS8jWOjQXNvkoJojK3H1cCY76dpJAE2DYn1eAmknHIKJHUWv0Z99jn_U3k2L4A2_ppcJTkVe-oY4OklGB2mAPbT5ixZF8vbALM-_erJqi674rKN2bhL8BZ2QoAYDgAf7v6AB&num=1&sig=AOD64_2bJ0g8m71fpjWrV0nkV1CTC5hU1Q&client=ca-pub-7133395778201029&adurl=http://www.aspose.com/total-component-suite-java.aspxhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/java/index.htmhttp://www.tutorialspoint.com/php/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.tutorialspoint.com%2fcplusplus%2fcpp_inheritance.htm&id=ma-140201140319-af1a9690http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://googleads.g.doubleclick.net/aclk?sa=l&ai=Cvu7l9kTtUrjVEefQ0AGe7YHYBuXT8_wDvb3J-U6m1qTbNBABIIW12wVQl_jlkv7_____AWDJBqAB3Z6U_wPIAQKoAwHIA8EEqgSkAU_Q5HOlmStkSPRM2m_DJkRdwf6X7VWO4jeQusQhkDRojsbutQgnPwmHEtwj6FH57Nvx_qfYsyzJkQhfV2VWLUSWbKgFIiIujP6PriMcPdZTTQ3dXVHlNaSBn1sJitLw4LGDMHpFnSz0PigcVGcPfJQvrGBmRbcvaTCV4SjEiRsYpNH2kedRqEPA5Kb4M1Fvh5qrgOsA_BoGN2TwS52Xd7xj0orKoAYCgAeL4Ws&num=1&sig=AOD64_0Cwpc-vzXSjVWjLxAJKSFVh2q2_w&client=ca-pub-7133395778201029&adurl=http://www2.smartbear.com/DevCCPRCTrials_collaborator-aw-gray.html%3F_kk%3D%26_kt%3D9c29b181-dce6-40db-b076-240cb338eea3http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htmhttp://www.tutorialspoint.com/cplusplus/cpp_overloading.htmhttp://www.tutorialspoint.com/#http://www.modernindianbabynames.com/http://googleads.g.doubleclick.net/aclk?sa=l&ai=Cs7MD9kTtUpmuIqXe0AG0w4CgD__MqYIEl6Sy8aoBkfyC4wkQASCFtdsFUOT35_X9_____wFgyQagAe2_3_4DyAEDqQK_JEO9wVitPqgDAcgDwQSqBKgBT9DRm6wbACEHS-QrF66NoEzJjDwvYVkXuGrHlWoUYbpzFFbkYTWnEysyAJIhwJwtAncGUhTgU8iLEblDQ23cQ6iPidM-jj0OjZNHwlKmsS8jWOjQXNvkoJojK3H1cCY76dpJAE2DYn1eAmknHIKJHUWv0Z99jn_U3k2L4A2_ppcJTkVe-oY4OklGB2mAPbT5ixZF8vbALM-_erJqi674rKN2bhL8BZ2QoAYDgAf7v6AB&num=1&sig=AOD64_2bJ0g8m71fpjWrV0nkV1CTC5hU1Q&client=ca-pub-7133395778201029&adurl=http://www.aspose.com/total-component-suite-java.aspxhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/index.htmhttp://www.tutorialspoint.com/java/index.htmhttp://www.tutorialspoint.com/php/index.htmhttp://www.tutorialspoint.com/python/index.htmhttp://www.tutorialspoint.com/ruby/index.htmhttp://www.tutorialspoint.com/perl/index.htmhttp://www.tutorialspoint.com/html/index.htmhttp://www.tutorialspoint.com/css/index.htmhttp://www.tutorialspoint.com/javascript/index.htmhttp://www.tutorialspoint.com/mysql/index.htmhttp://www.tutorialspoint.com/cplusplus/index.htmhttp://www.tutorialspoint.com/unix/index.htmhttp://www.tutorialspoint.com/more.htmhttp://www.tutorialspoint.com/references.htmhttp://www.tutorialspoint.com/forums/index.phphttp://www.tutorialspoint.com/cplusplus/index.htmhttp://www.tutorialspoint.com/cplusplus/cpp_overview.htmhttp://www.tutorialspoint.com/cplusplus/cpp_environment_setup.htmhttp://www.tutorialspoint.com/cplusplus/cpp_basic_syntax.htmhttp://www.tutorialspoint.com/cplusplus/cpp_comments.htmhttp://www.tutorialspoint.com/cplusplus/cpp_data_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_variable_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    2/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    classderived-class:access-specifier base-class

    Where access-specifier is one of public, protected, or private, and base-class is the name of aprevious ly defined class. If the access-specifier is not used, then it is private by default.

    Consider a base class Shapeand its derived class Rectangleas follows:

    #include

    usingnamespacestd;

    // Base class

    classShape

    { public:

    voidsetWidth(intw)

    {

    width =w;

    } voidsetHeight(inth)

    {

    height =h;

    }

    protected:

    intwidth; intheight;

    };

    // Derived class

    classRectangle:publicShape{

    public:

    intgetArea()

    {

    return(width *height);

    }

    };

    intmain(void)

    C++ Constants/Literals

    C++ Modifier Types

    C++ Storage Classes

    C++ Operators

    C++ Loop Types

    C++ Decision Making

    C++ Functions

    C++ Numbers

    C++ Arrays

    C++ Strings

    C++ Pointers

    C++ References

    C++ Date & Time

    C++ Basic Input/Output

    C++ Data Structures

    C++ Object Oriented

    C++ Classes & Objects

    C++ Overloading

    C++ Polymorphis m

    C++ Abstraction

    C++ Encapsulation

    C++ Inheritance

    http://googleads.g.doubleclick.net/aclk?sa=l&ai=Cs7MD9kTtUpmuIqXe0AG0w4CgD__MqYIEl6Sy8aoBkfyC4wkQASCFtdsFUOT35_X9_____wFgyQagAe2_3_4DyAEDqQK_JEO9wVitPqgDAcgDwQSqBKgBT9DRm6wbACEHS-QrF66NoEzJjDwvYVkXuGrHlWoUYbpzFFbkYTWnEysyAJIhwJwtAncGUhTgU8iLEblDQ23cQ6iPidM-jj0OjZNHwlKmsS8jWOjQXNvkoJojK3H1cCY76dpJAE2DYn1eAmknHIKJHUWv0Z99jn_U3k2L4A2_ppcJTkVe-oY4OklGB2mAPbT5ixZF8vbALM-_erJqi674rKN2bhL8BZ2QoAYDgAf7v6AB&num=1&sig=AOD64_2bJ0g8m71fpjWrV0nkV1CTC5hU1Q&client=ca-pub-7133395778201029&adurl=http://www.aspose.com/total-component-suite-java.aspxhttp://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.tutorialspoint.com%2fcplusplus%2fcpp_inheritance.htm&id=ma-140201140319-af1a9690http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://googleads.g.doubleclick.net/aclk?sa=l&ai=Cs7MD9kTtUpmuIqXe0AG0w4CgD__MqYIEl6Sy8aoBkfyC4wkQASCFtdsFUOT35_X9_____wFgyQagAe2_3_4DyAEDqQK_JEO9wVitPqgDAcgDwQSqBKgBT9DRm6wbACEHS-QrF66NoEzJjDwvYVkXuGrHlWoUYbpzFFbkYTWnEysyAJIhwJwtAncGUhTgU8iLEblDQ23cQ6iPidM-jj0OjZNHwlKmsS8jWOjQXNvkoJojK3H1cCY76dpJAE2DYn1eAmknHIKJHUWv0Z99jn_U3k2L4A2_ppcJTkVe-oY4OklGB2mAPbT5ixZF8vbALM-_erJqi674rKN2bhL8BZ2QoAYDgAf7v6AB&num=1&sig=AOD64_2bJ0g8m71fpjWrV0nkV1CTC5hU1Q&client=ca-pub-7133395778201029&adurl=http://www.aspose.com/total-component-suite-java.aspxhttp://www.tutorialspoint.com/cplusplus/cpp_constants_literals.htmhttp://www.tutorialspoint.com/cplusplus/cpp_modifier_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_storage_classes.htmhttp://www.tutorialspoint.com/cplusplus/cpp_operators.htmhttp://www.tutorialspoint.com/cplusplus/cpp_loop_types.htmhttp://www.tutorialspoint.com/cplusplus/cpp_decision_making.htmhttp://www.tutorialspoint.com/cplusplus/cpp_functions.htmhttp://www.tutorialspoint.com/cplusplus/cpp_numbers.htmhttp://www.tutorialspoint.com/cplusplus/cpp_arrays.htmhttp://www.tutorialspoint.com/cplusplus/cpp_strings.htmhttp://www.tutorialspoint.com/cplusplus/cpp_pointers.htmhttp://www.tutorialspoint.com/cplusplus/cpp_references.htmhttp://www.tutorialspoint.com/cplusplus/cpp_date_time.htmhttp://www.tutorialspoint.com/cplusplus/cpp_basic_input_output.htmhttp://www.tutorialspoint.com/cplusplus/cpp_data_structures.htmhttp://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htmhttp://www.tutorialspoint.com/cplusplus/cpp_inheritance.htmhttp://www.tutorialspoint.com/cplusplus/cpp_overloading.htmhttp://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htmhttp://www.tutorialspoint.com/cplusplus/cpp_data_abstraction.htmhttp://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htmhttp://www.tutorialspoint.com/cplusplus/cpp_interfaces.htm
  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    3/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    { RectangleRect;

    Rect.setWidth(5);

    Rect.setHeight(7);

    // Print the area of the object.

    cout

  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    4/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    Type of Inheritance:

    When deriving a class from a base class, the base class may be inherited through public, protectedorprivateinheritance. The type of inheritance is specified by the access-specifier as explained above.

    We hardly use protectedor privateinheritance, but publicinheritance is commonly used. While usingdifferent type of inheritance, following rules are applied:

    Public Inheritance: When deriving a class from a public base class, public members of the

    base class become publicmembers of the derived class and protectedmembers of the baseclass become protectedmembers of the derived class. A base class's private members arenever accessible directly from a derived class, but can be accessed through calls to the publicand protectedmembers of the base class.

    Protected Inheritance: When deriving from a protected base class, public and protectedmembers of the base class become protectedmembers of the derived class .

    Private Inheritance:When deriving from a privatebase class, publicand protectedmembers ofthe base class become privatemembers of the derived class .

    Multiple Inheritances:

    A C++ class can inherit members from more than one class and here is the extended syntax:

    classderived-class:access baseA,access baseB....

    Where access is one of public, protected,or privateand would be given for every base clas s and theywill be s eparated by comma as s hown above. Let us try the following example:

    #include

    usingnamespacestd;

    // Base class Shape

    classShape

    {

    public:

    voidsetWidth(intw)

    {

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.tutorialspoint.com%2fcplusplus%2fcpp_inheritance.htm&id=ma-140201140319-af1a9690http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdf
  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    5/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    width =w; }

    voidsetHeight(inth)

    {

    height =h;

    }

    protected:

    intwidth;

    intheight;

    };

    // Base class PaintCost

    classPaintCost{

    public:

    intgetCost(intarea)

    {

    returnarea *70;

    }

    };

    // Derived class

    classRectangle:publicShape,publicPaintCost

    {

    public: intgetArea()

    {

    return(width *height);

    }

    };

    intmain(void)

    {

    RectangleRect;

    intarea;

    Rect.setWidth(5); Rect.setHeight(7);

    area =Rect.getArea();

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.tutorialspoint.com%2fcplusplus%2fcpp_inheritance.htm&id=ma-140201140319-af1a9690http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdf
  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    6/7

    pdfcrowd comopen in browser PRO version Are you a developer? Try out the HTML to PDF API

    // Print the area of the object.

    cout

  • 7/27/2019 Www Tutorialspoint Com Cplusplus Cpp Inheritance Htm

    7/7

    df d mi b PRO i Are you a developer? Try out the HTML to PDF API

    Copyright 2014 by tutorialspoint. All Rights Reserved.

    ASP.NET | jQuery | AJAX | ANT | JSP | Servlets | log4j | iBATIS | Hibernate | JDBC | Struts | HTML5 | SQL | MySQL

    http://pdfcrowd.com/http://pdfcrowd.com/redirect/?url=http%3a%2f%2fwww.tutorialspoint.com%2fcplusplus%2fcpp_inheritance.htm&id=ma-140201140319-af1a9690http://pdfcrowd.com/customize/http://pdfcrowd.com/html-to-pdf-api/?ref=pdfhttp://www.tutorialspoint.com/asp.net/index.htmhttp://www.tutorialspoint.com/jquery/index.htmhttp://www.tutorialspoint.com/ajax/index.htmhttp://www.tutorialspoint.com/ant/index.htmhttp://www.tutorialspoint.com/jsp/index.htmhttp://www.tutorialspoint.com/servlets/index.htmhttp://www.tutorialspoint.com/log4j/index.htmhttp://www.tutorialspoint.com/ibatis/index.htmhttp://www.tutorialspoint.com/hibernate/index.htmhttp://www.tutorialspoint.com/jdbc/index.htmhttp://www.tutorialspoint.com/struts_2/index.htmhttp://www.tutorialspoint.com/html5/index.htmhttp://www.tutorialspoint.com/sql/index.htmhttp://www.tutorialspoint.com/mysql/index.htm