0% found this document useful (0 votes)
0 views

Overloading+ Inheritance

The document discusses key concepts of object-oriented programming in C++, including function overloading, operator overloading, and inheritance. It explains function overloading with examples, outlines operator overloading and its limitations, and details different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Additionally, it describes visibility modes (public, protected, and private) in the context of class inheritance.

Uploaded by

Anil Pachauri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Overloading+ Inheritance

The document discusses key concepts of object-oriented programming in C++, including function overloading, operator overloading, and inheritance. It explains function overloading with examples, outlines operator overloading and its limitations, and details different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance. Additionally, it describes visibility modes (public, protected, and private) in the context of class inheritance.

Uploaded by

Anil Pachauri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OVERLOADING-

Function Overloading- Function overloading is a feature of object-oriented programming where


two or more functions can have the same name but different parameters.
If multiple functions having same name but parameters of the functions should be different is known as
Function Overloading.
// same name different arguments
int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }

#include <iostream>
using namespace std;

void add(int a, int b)


Output-
{
sum = 12
cout << "sum = " << (a + b);
sum = 15
}
void add(int a, int b, int c)
{
cout << endl << "sum = " << (a + b + c);
}
// Driver code
int main()
{
add(10, 2);
add(5, 6, 4);
return 0;
}
Operator overloading- ability to provide the operators with a special meaning for a data type, this
ability is known as operator overloading. Operator overloading is a compile-time polymorphism. For
example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings
by just using +. Other example classes where arithmetic operators may be overloaded are Complex
Numbers, Fractional Numbers, Big integers, etc.

there are some operators that cannot be overloaded. They are


 Scope resolution operator (::)
 Member selection operator
 Member selection through *

 Conditional operator (?:)


 Sizeof operator sizeof()

INHERITANCE
Inheritance is one of the most important features of Object Oriented Programming
in C++. The capability of a class to derive properties and characteristics from
another class is called Inheritance.
Syntax-
class derived_class_name : access-specifier base_class_name
{
// body ....
};

Example-
// C++ program to demonstrate inheritance

#include <iostream>
using namespace std;
// base class
class Animal {
public:
void eat() {
cout << "I can eat!" << endl;
}
void sleep() {
cout << "I can sleep!" << endl;
} };

// derived class
class Dog : public Animal {

public:
void bark() {
cout << "I can bark! Woof woof!!" << endl;
} };

int main() {
// Create object of the Dog class
Dog dog1;
// Calling members of the base class
dog1.eat();
dog1.sleep();
// Calling member of the derived class
dog1.bark();
return 0;
}
TYPES OF INHERITANCE

1. Single inheritance- In single inheritance, a class is allowed to inherit from only one class. i.e. one
base class is inherited by one derived class only.
class A
{ ... .. ... };
class B: public A
{ ... .. ... };

2. Multiple inheritance- Multiple Inheritance is a feature of C++ where a class can inherit from more
than one class. i.e one subclass is inherited from more than one base class.
class A
{ ... .. ... };
class B
{ ... .. ... };
class C: public A, public B
{ ... ... ... };

3. Multilevel Inheritance- In this type of inheritance, a derived class is created from another derived
class and that derived class can be derived from a base class or any other derived class. There can be
any number of levels.
class C
{ ... .. ... };
class B : public C
{ ... .. ... };
class A: public B
{ ... ... ... };

4.Hierarchical Inheritance- In hierarical type of inheritance, more than one subclass is inherited
from a single base class. i.e. more than one derived class is created from a single base class .

class A
{ // body of the class A. }
class B : public A
{ // body of class B. }
class C : public A
{ // body of class C. }
class D : public A
{ // body of class D. }

5. Hybrid Inheritance- It is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance
in C++.
class F
{ ... .. ... }
class G
{ ... .. ... }
class B : public F
{ ... .. ... }
class E : public F, public G
{ ... .. ... }
class A : public B
{ ... .. ... }
class C : public B
{ ... .. ... }

VISIBILITY
Public Visibility mode: If we derive a subclass from a public base class. Then the public
member of the base class will become public in the derived class and protected members of the
base class will become protected in the derived class.
Protected Visibility mode: If we derive a subclass from a Protected base class. Then both
public member and protected members of the base class will become protected in the derived
class.
Private Visibility mode: If we derive a subclass from a Private base class. Then both public
Cmember and protected members of the base class will become Private in the derived class.

You might also like