sas

ASP-Active Server Pages
Computer Fundamentals
What is computer?
Software
Hardware
Operating System
Programming Language
C and C++
Java
C#
Python
Visual Basic
Jscript.NET
Web Development
HTML
ASP
PHP
JavaScript
VBScript
.NET
.NET Micrsoft
ASP.NET
.NET Mobile
Forum
VB/VB.NET
ASP/ASP.NET
VBScript
JavaScript

Topic: C++-Language


Inheritance
One reason to use inheritance is that it permits you to reuse code from a previous project, but gives you the flexibility to slightly modify it if the old code doesn't do exactly what you need for the new project. It doesn't make sense to start every new project from scratch since some code will certainly be repeated in several programs and you should strive to build on what you did previously. However, it is easy to make an error if you try to modify the original class. You are less likely to make an error if you leave the original alone and only add to it. Another reason for using inheritance is if the project requires the use of several classes which are very similar but slightly different.

In this chapter we will concentrate on the mechanism of inheritance and how to build it into a program. A better illustration of why you would use inheritance will be given in later chapters where we will discuss some practical applications of object oriented programming. The principle of inheritance is available with several modern programming languages and is handled slightly differently with each. C++ allows you to inherit all or part of the members and methods of a class, modify some, and add new ones not available in the parent class. You have complete flexibility, and as usual, the method used with C++ has been selected to result in the most efficient code execution.

Basics
Definition-Inheritance - Inheritance is the process of creating new classes called derived classes from existing classes.

 These existing classes are called as base classes. The derived
class inherts all the capabilities of the base class but can add new
features and refinements of its own. By adding these refinements
the base class remains unchanged.

  Inheritance permits code reusability. This is one important use
and advantage of inheritance that it offers to Object Oriented
Programming.

  Once a base class is written and debugged it need not be touched
again but at the same time it can be adapted to work in different
situations. Reusing existing code saves time and increases the
programs reliability.

  Inheritance can also help in the original conceptualization of a
programming problem and in overall design of the program.

** Some important points that have to be noted about inheritance are
as follows : :

 1. A derived class inherits all the capabilities of the base class, but
     can add new features of its own. By making these additions the
     base class remains unchanged.

 2. protected members behave just like private members until a new
     class is derived from the base class that has protected members.

 3. If a base class has private members those members are not
     accessible  to the derived class. protected members are public
     to derived classes but private to the rest of the program.

 4. A derived class can specify that a base class is public or private
     by using following notation as given below,

       class c : public b ;
       class a : private b ;

Example

// derived classes
#include <iostream.h>
class CPolygon { 
protected:    int width, height; 
public:   
void set_values (int a, int b)   
   {
width=a; height=b
;} 
};
class CRectangle: public CPolygon
public:   
int area (void)      {
return (width * height);
}
  };
class CTriangle: public CPolygon
public:   
int area (void)    
{
return (width * height / 2);

};
int main () { 
CRectangle rect; 
CTriangle trgl; 
rect.set_values (4,5);
  trgl.set_values (4,5); 
cout << rect.area() << endl; 
cout << trgl.area() << endl;
  return 0;
}




































Prev

 

















footer back link


 

Error in my_thread_global_end(): 1 threads didn't exit