0% found this document useful (0 votes)
22 views7 pages

Oop Unit III Unit 3 Notes

The document provides an overview of C++ inheritance, explaining its types and advantages, particularly code reusability. It also introduces polymorphism, including function and operator overloading, and distinguishes between compile-time and run-time polymorphism. Examples of single inheritance and derived classes are included to illustrate these concepts.

Uploaded by

seema.chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Oop Unit III Unit 3 Notes

The document provides an overview of C++ inheritance, explaining its types and advantages, particularly code reusability. It also introduces polymorphism, including function and operator overloading, and distinguishes between compile-time and run-time polymorphism. Examples of single inheritance and derived classes are included to illustrate these concepts.

Uploaded by

seema.chaudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

IAMR COLLEGE

OopUNIT III -unit 3 notes

object oriented programming using C++(Chaudhary Charan Singh


University)
Unit-III

C++ Inheritance

In C++, inheritance is a process in which one object acquires all the properties and
behaviors of its parent object automatically. In such way, you can reuse, extend or modify
the attributes and behaviors which are defined in other class.

In C++, the class which inherits the members of another class is called derived class and
the class whose members are inherited is called base class. The derived class is the
specialized class for the base class.

Advantage of C++Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is no
need to define the member again. So less code is required in the class.

Types of Inheritance

C++ supports five types of inheritance:

o Single inheritance
o Multiple inheritance
o Hierarchical inheritance
o Multilevel inheritance
o Hybrid inheritance

Single inheritance is defined as the inheritance wchich a derived class is inherited from
the only one base class.

Multilevel inheritance is a process of deriving a class from another derived class.

Multiple inheritance is the process of deriving an class that inherits the attributes from
two or more classes.

Hybrid inheritance is a combination of more than one type of inheritance.

Hierarchicalinheritanceis defined as the process of deriving more than one class from a
base class.
Single inheritance ->
#include <iostream>
using namespace std;

// Base class that is to be inherited


class Parent {
public:
// base class members
int id_p;
void printID_p()
{
cout << "Base ID: " << id_p << endl;
}
};

// Sub class or derived publicly inheriting from Base


// Class(Parent)
class Child : public Parent {
public:
// derived class members
int id_c;
void printID_c()
{
cout << "Child ID: " << id_c << endl;
}
};

// main function
int main()
{
// creating a child class object
Child obj1;

// An object of class child has all data members


// and member functions of class parent
// so we try accessing the parents method and data from
// the child class object.
obj1.id_p = 7;
obj1.printID_p();

// finally accessing the child class methods and data


// too
obj1.id_c = 91;
obj1.printID_c();

return 0;
}

#include <iostream>
using namespace std;

// Base class
class Base {
public:
// data member
int publicVar;

// member method
void display()
{
cout << "Value of publicVar: " << publicVar;
}
};

// Derived class
class Derived : public Base {
public:
// Function to display inherited member
void displayMember()
{
// accessing public base class member method
display();
}

// Function to modify inherited member


void modifyMember(int pub)
{
// Directly modifying public member
publicVar = pub;
}
};

int main()
{
// Create an object of Derived class
Derived obj;

// Display the initial values of inherited member


obj.modifyMember(10);

// Display the modified values of inherited member


obj.displayMember();

return 0;
}
C++Polymorphism

The term "Polymorphism" is the combination of "poly" + "morphs" which means many
forms. It is a greek word. In object-oriented programming, we use 3 main concepts:
inheritance, encapsulation, and polymorphism.

Real Life Example Of Polymorphism


Let's consider a real-life example of polymorphism. A lady behaves like a teacher in a
classroom,motherordaughterinahomeandcustomerinamarket.Here,asinglepersonis behaving
differently according to the situations.

C++Overloading(Function and Operator)

If we create two or more members having the same name but different in number or typeof
parameter, it is known as C++ overloading. In C++, we can overload:

o methods,
o constructors,and
o indexed properties

It is because these members have parameters only.

Types of overloading in C++ are:

o Functionoverloading
o Operator overloading

C++FunctionOverloading

Function Overloading is defined as the process of having two or more function with the
same name, but different in parameters is known as function overloading in C++. In
function overloading, the function is redefined by using either different types of arguments
or a different number of arguments. It is only through these differences compiler can
differentiate between the functions. The advantage of Function overloading is that it
increases the readability of the program because you don't need to use different names for
the same action.
Differences b/w compile time and run time polymorphhism.

Compiletimepolymorphism Runtimepolymorphism

The function to be invoked Is known at the The function to be invoked is known


compile time. at the runtime.

It is also known as early It is also known as


overriding,Dynamic binding and
overloading,bindingandstaticbinding. latebinding.
Overloading is a compile time Overriding is a runtime
polymorphism where more than one polymorphism where more than one
method is having the same name bu with method is having the same
the different number of parameter or the name,number of parameters and the
type of the parameters. type of the parameters.

It is achieved by function overloading and It is achieved by virtual functions


operator overloading. and pointers.
It provides fast execution as it is known at It provides low execution as it is
the compile time. known at the run time.

It is less flexible as mainly all the things It is more flexible as all the thing
s
Execute at the compile time. Execute at the
runtime.

You might also like