Inheritance in CPLUSPLUS

Inheritance in CPLUSPLUS

inheritance

image credit:codingunit.com

Inheritance in CPLUSPLUS is the biggest milestone in Object Oriented Programming. It is better to rewrite code for each and every function.Thus preventing loads of work from developer a well compiler and the application will become robust.
Further functionality of the classes can be extended without disturbing the existing code . Includes blackbox and whitebox reuse with existing code remains unchanged. Enhances extensibility and comprehensibility.

White Box Reuse

Pro: We can manage the module for specific situation, this allows reuse in more situations
Con: Complexity in the code is a challenge

Black Box Reuse

Pro: Too simple and neat and clean
Con: Mot of the times it is not required

Now comes Inheritance

Inheritance is the technique, in which a child takes some attributes from its parents such as height, complexion etc.
When applied with C++, it is the object which takes some or all attributes from another object(s).
The first one is named as Parent and the second one called derived. Let’s see in action:

#include “stdafx.h”
#include<iostream>
using namespace std;

class Car
{
//to make the variables visible outside, we have used public access modifier
public:
string name;
int gears;
int speed;

};

class Model
{
//to make the variables visible outside, we have used public access modifier
public:

string Condition;
int mileage;
int durability;

};

class Type
{
public:

int heatuptime;
int airconditioners;

};

int main()
{
Car p;

p.gears=5;
p.speed=500;

Model m;

m.durability=100;
m.mileage=10000;

Type t;

t.heatuptime=1200;
t.airconditioners=2;

cout<<p.gears<<endl;
cout<<p.speed<<endl;

cout<<m.durability<<endl;
cout<<m.mileage<<endl;

cout<<t.heatuptime<<endl;
cout<<t.airconditioners<<endl;

}



[ Point to be noted that C++ supports multiple inheritances. It is a good decision but it imposes Diamond Problem]

Constructor

A class includes a special function called its constructor.This function has the same name as that of a class but it has no return type and neither void can be used. Whenever constructor is invoked, it creates an instance of a class
What msdn says about Constructors:

A Constructor may contain parameters surrounded by braces or parentheses but no return type.
constructor is a special member function that initializes an instance of its class. To call a constructor, you use the class name and () brackets

Default constructors

A default constructor is a constructor which calls itself in every program execution with no arguments (We can define them if we want). Go through this quite easier Default Contructor example

Multiple Inheritances
When a class is derived from multiple base classes. this is called Multiple Inheritance which C++ supports.

#include”stdafx.h”
#include <iostream>
using namespace std;

class Cpolygon
{
protected:
int width, height;
public:
void input_values (int one, int two)
{
width=one;
height=two;
}
};

class Crectangle: public Cpolygon
{
public:
int area ()
{
return (width * height);
}
};

class Ctriangle: public Cpolygon
{
public:
int area ()
{
return (width * height / 2);
}
};

int main ()
{
Crectangle rectangle;
Ctriangle triangle;
rectangle.input_values (2,2);
triangle.input_values (2,2);
cout << rectangle.area() << endl;
cout << triangle.area() << endl;
return 0;
}

Code Credit:http://www.codingunit.com/cplusplus-tutorial-inheritance

Output
4
2

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.
Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.