WWW Tutorialspoint Com Cplusplus CPP Inheritance HTM
WWW Tutorialspoint Com Cplusplus CPP Inheritance HTM
HOME
JAVA
PHP
Python
Ruby
Perl
HTML
CSS
Javascript
MySQL
C++
UNIX
MORE...
REFERENCES | FORU
C++ Inheritance
Advertisements
Mod
Previous Page
Next Page
C++ Basics
C++ Home C++ Overview C++ Environment Setup C++ Basic Syntax C++ Comments C++ Data Types C++ Variable Types C++ Variable Scope
One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class. The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
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
class derived-class: access-specifier base-class Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default. Consider a base class Shape and its derived class Rectangle as follows: #include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void)
Are you a developer? Try out the HTML to PDF API
C++ Inheritance
C++ Overloading C++ Polymorphism C++ Abstraction C++ Encapsulation
pdfcrowd.com
C++ Interfaces
{ Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; } When the above code is compiled and executed, it produces the following result: Total area: 35
C++ Advanced
C++ Files and Streams C++ Exception Handling C++ Dynamic Memory C++ Namespaces C++ Templates C++ Preprocessor C++ Signal Handling C++ Multithreading C++ Web Programming
Selected Reading
Developer's Best Practices Effective Resume Writing Computer Glossary Who is Who
A derived class inherits all base class methods with the following exceptions: Constructors, destructors and copy constructors of the base class. Overloaded operators of the base class. The friend functions of the base class.
pdfcrowd.com
Type of Inheritance:
When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier as explained above. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different 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 public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.
Multiple Inheritances:
A C++ class can inherit members from more than one class and here is the extended syntax: class derived-class: access baseA, access baseB.... Where access is one of public, protected, or private and would be given for every base class and they will be separated by comma as shown above. Let us try the following example: #include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) {
pdfcrowd.com
width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Base class PaintCost class PaintCost { public: int getCost(int area) { return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea();
pdfcrowd.com
// Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } When the above code is compiled and executed, it produces the following result: Total area: 35 Total paint cost: $2450
Previous Page
Advertisements
Print Version
PDF Version
Next Page
pdfcrowd.com
ASP.NET | jQuery | AJAX | ANT | JSP | Servlets | log4j | iBATIS | Hibernate | JDBC | Struts | HTML5 | SQL | MySQL
pdfcrowd.com