Inheritance

COMPOSITION AND INHERITANCE

Posted on Updated on

  • In real-life, complex objects are often built from smaller, simpler objects. For example, a car is built using a metal frame, an engine, some tires, a transmission, a steering wheel, and a large number of other parts.
  • A PC is built from a CPU, a motherboard, some memory, etc. This process of building complex objects from simpler ones is called composition (also known as object composition).
  • Composition is used for objects that have a has-a relationship to each other.
  • A car has-a metal frame, has-an engine, and has-a
  • A personal computer has-a CPU, a motherboard, and other components.
  • Composition is nothing but relationships between the different objects. Sometimes Object made up from another objects like Airplane is Wings, Landing gears, engines etc. this relationship is called composition
  • Inheritance is extends one class to another class like

Public class A

{//Here methods and variable etc.}

public class B:A

{//here methods and properties variable etc}

public static void main()

{B b = new B();

b.method();

b.Variable ..}

 INHERITANCE

  • 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
  • The idea of inheritance implements the is a For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well

            Base & Derived Classes:

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

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.h>
// Base class
class Shape
{ public:
voidsetWidth(int w)
{ width = w; }
voidsetHeight(int h)
{ height = h; }
protected:
int width;
int height;};
// Derived class
class Rectangle: public Shape
{ public:
intgetArea()
{ return (width * height); }};
void main()
{ Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<< “Total area: ” <<Rect.getArea() <<endl;}

      When the above code is compiled and executed, it produces the following result:

 Total area: 35

 Total paint cost: $2450

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Inheritance

Posted on

Inheritance

The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is inheritance.

Inheritance lets you create new classes from existing class. Any new class that you create from an existing class is called derived class; existing class is called base class.The inheritance relationship enables a derived class to inherit features from its base class. Therefore, rather than create completely new classes from scratch, you can take advantage of inheritance and reduce software complexity.

Forms of Inheritance

Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

 

In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format :

class derived_class: memberAccessSpecifier base_class

{

};

Where derived_class is the name of the derived class and base_class is the name of the class on which it is based. The member Access Specifier may be public, protected or private. This access specifier describes the access level for the members that are inherited from the base class.

Member Access Specifier How Members of the Base Class Appear in the Derived Class
Private Private members of the base class are inaccessible to the derived class.
Protected members of the base class become private members of the derived class.
Public members of the base class become private members of the derived class.
Protected Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become protected members of the derived class.
Public Private members of the base class are inaccessible to the derived class.
Protected members of the base class become protected members of the derived class.
Public members of the base class become public members of the derived class.

 

class Shape

{

protected:

float width, height;

public:

void set_data (float a, float b)

{

width = a;

height = b;

}

};

 

class Rectangle: public Shape

{

public:

float area ()

{

return (width * height);

}

};

 

class Triangle: public Shape

{

public:

float area ()

{

return (width * height / 2);

}

};

 

int main ()

{

Rectangle rect;

Triangle tri;

rect.set_data (5,3);

tri.set_data (2,5);

cout << rect.area() << endl;

cout << tri.area() << endl;

return 0;

}

 

output :

15
5

 

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com