Nothing Special   »   [go: up one dir, main page]

Lecture 6

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Object Oriented

Programming
Today’s Lecture

1. Introduction to Inheritance.

2. Purpose of Inheritance, Inheritance Advantages


C++ Inheritance
C++ Inheritance

One of the most important concepts in object-oriented programming is


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.
C++ Inheritance

• The language mechanism by which one class acquires the properties


(data and operations) of another class
• Base Class (or superclass): the class being inherited from
• Derived Class (or subclass): the class that inherits

• The derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly
created derived class.
C++ Inheritance

Advantages of inheritance

When a class inherits from another class, there are three main benefits:

(1) You can reuse the methods and data of the existing class

(2) You can extend the existing class by adding new data and new methods

(3) You can modify the existing class by overloading its methods with your

own implementations
C++ Inheritance

Features or Advantages of Inheritance:

Reusability:
Inheritance helps the code to be reused in many situations.

Using the concept of inheritance, the programmer can create as many


derived classes from the base class as needed while adding specific
features to each derived class as needed.
C++ Inheritance

Features or Advantages of Inheritance:


Saves Time and Effort:

The above concept of reusability achieved by inheritance


saves the programmer time and effort. The main code
written can be reused in various situations as needed.

Increases Program Structure which results in


greater reliability.
C++ Inheritance

General Syntax:

class derived_classname: access specifier baseclassname


For example, if the base class is MyClass and the derived class is sample it is
specified as:

class sample: public MyClass

The above makes sample have access to both public and protected variables of base
class MyClass
Rules for building a class
hierarchy
• Derived classes are special cases of base classes
• A derived class can also serve as a base class for new classes.
• There is no limit on the depth of inheritance allowed in C++ (as far as
it is within the limits of your compiler)
• It is possible for a class to be a base class for more than one derived
class
Inheritance and
accessibility
• A class inherits the behavior of another class and
enhances it in some way
• Inheritance does not mean inheriting access to
another class’ private members
Access Control/ Visibilty
modes and Inheritance:
• A derived class can access all the non-private
members of its base class. Thus base-class
members that should not be accessible to the
member functions of derived classes should be
declared private in the base class.
• We can summarize the different access types
according to who can access them in the following
way:
Access Control and
Inheritance:
Visibility Modes in Inheritance
Reminder about public, private and protected access
specifiers:

1 If a member or variables defined in a class is private, then


they are accessible by members of the same class only and
cannot be accessed from outside the class.

2 Public members and variables are accessible from outside


the class.

3 Protected access specifier is a stage between private and


public. If a member functions or variables defined in a class
are protected, then they cannot be accessed from outside
the class but can be accessed from the derived class.
Visibility Modes in 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.

We hardly use protected or private inheritance,


but public inheritance is commonly used. While
using different type of inheritance, following rules are
applied:
Visibility Modes in Inheritance

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.
Visibility Modes in Inheritance

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
Class Activity

Create a class Employee with the following attributes and member functions:
 Employee registration number
 Employee name
 Destination
 Basic salary
 Profitable fund
 Get the function set_ data of the registration number, name, and destination.

Inherit (public) a class salary from the employee. Salary should contain the
following member functions:
 Get function of basic salary, and calculate 10% of profitable fund of basic
salary.
 Get sum function to calculate basic salary and profitable fund.
Header.h
class emp { void emp::set_data()
public: {
int reg_no; cout << " Enter your
char name[20]; registration number: ";
char dest[20]; cin >> reg_no;
int b_salary; cout << " Enter your name: ";
int p_f; cin.ignore();
cin.getline(name,20);
void set_data(); cout << " Enter your
destination: ";
}; cin.getline(dest, 20);
}

class salary:public emp{ void salary::set_salary(int BS)


private: {
int t_sum, calculate_pf; b_salary = BS;
public:
void set_salary(int BS); cout << " 10% Profitable fund
void sum(); of Basic Salary: ";
calculate_pf = (BS / 100) * 10;
Header.h
void salary::sum()
{
t_sum = b_salary + calculate_pf;
cout << endl;
cout << " Calculate PF and Basic Salary : " << t_sum;
}
#include<iostream> Main.cpp
#include"Header.h"

using namespace std;

int main()
{
emp object1;
salary object2;

int basic;

object2.set_data();
cout << " Enter your basic salary: ";
cin >> basic;
object2.set_salary(basic);

object2.sum();

cout << "\n";


system("pause");
}
Output

You might also like