0% found this document useful (0 votes)
38 views56 pages

Unit-2 Inheritance & Pointers

Uploaded by

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

Unit-2 Inheritance & Pointers

Uploaded by

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

Sinhgad College of Engineering

Vadgaon Bk. Pune


Approved by AICTE New Delhi Recognized by Govt. of Maharashtra
Affiliated to Savitribai Phule Pune University
Accredited by NAAC with A+ Grade

Subject:- 210243: Object Oriented Programming.


Unit-2 Inheritance & Pointers.
Mr. AJIT M. KARANJKAR
Assistant. Professor.
Department of Computer Engineering,
Sinhgad College Of Engineering Vadgaon Bk, Pune-041
Email:- amkaranjkar.scoe@sinhgad.edu

24-11-2022 1
UNIT-2
INHERITANCE & POINTERS

24-11-2022 UNIT-I Object Oriented Programming 2


Department Of Computer Engineering
Contents:
Inheritance.
Base class & Derived class, Protected members, relationship between
base class & derived class , constructor & destructor in derived class,
overriding member functions, class hierarchies, public and private
inheritance, Ambiguity in multiple inheritance, virtual base class,
Abstract class, Nested class.

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 3
Department Of Computer Engineering
Contents:
Pointer.
Declaring and initializing pointers, indirection operators, Memory
management, new and delete, pointers to object, this pointer, pointer
vs arrays, accessing arrays using pointers, arrays of pointers, function
pointer, pointer to pointer, pointer to derived class, passing pointer to
functions, return pointer to from functions, null pointer , void pointer.

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 4
Department Of Computer Engineering
2. Inheritance

UNIT-I Problem Solving ,Programming and Python Programming


24-11-2022 5
Department Of Computer Engineering
1 Inheritance.
Inheritance
Inheritance is the process by which objects of one class acquire the properties of objects of
another class.

• 1.1 Base Class & Derived Class In Inheritance:

Super Class(Parent): The class whose properties are inherited by sub class is called Base
Class or Super class.

Sub Class (Child):


The class that inherits properties from another class is called Sub class or Derived Class.

UNIT-II Inheritance & Pointer


24-11-2022 6
Department Of Computer Engineering
1 Inheritance.

Fig. 1 Base class & Derived class

UNIT-II Inheritance & Pointer


24-11-2022 7
Department Of Computer Engineering
1 Inheritance.
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class sample
{
char name[30];
public:
void getdata();
void putdata();
};

UNIT-II Inheritance & Pointer


24-11-2022 8
Department Of Computer Engineering
1 Inheritance.
void sample::getdata()
{
cout<<“Enter name of Employee”;
cin>>name;
};
void sample::putdata()
{
cout<<“Name of Employee”<<name;
};

UNIT-II Inheritance & Pointer


24-11-2022 9
Department Of Computer Engineering
1 Inheritance.
class emp : public sample
{
int age;
public:
void getdata1();
void putdata1();
};
void emp::getdata1()
{
getdata( );
cout<<“Enter Age of Employee”
cin>>age;
};

UNIT-II Inheritance & Pointer


24-11-2022 10
Department Of Computer Engineering
1 Inheritance.
void emp::putdata1()
{
Putdata( );
cout<<“ Age of Employee”<<age;
};

void main( )
{
clrscr( );
emp s;
s.getdata1( );
s.putdata1();
getch();
}

UNIT-II Inheritance & Pointer


24-11-2022 11
Department Of Computer Engineering
1 Inheritance.

Output:

Enter name of Employee Ramesh


Enter age of Employee 25
Name of Employee Ramesh
Age of Employee 25

UNIT-II Inheritance & Pointer


24-11-2022 12
Department Of Computer Engineering
1 Inheritance.
1.2 Protected Members:

• A class in C++ has public, private and protected sections which contain the corresponding
class members.

• Protected members in a class are similar to private members as they cannot be accessed
from outside the class.

• But they can be accessed by derived classes or child classes while private members cannot.

UNIT-II Inheritance & Pointer


24-11-2022 13
Department Of Computer Engineering
1 Inheritance.
Example:
#include <iostream>
using namespace std;
class Base
{
protected :
int num = 7;
};
class Derived : public Base
{
public :
void func()
{
cout << "The value of num is: " << num;
}
};
UNIT-II Inheritance & Pointer
24-11-2022 14
Department Of Computer Engineering
1 Inheritance.

int main()
{
Derived obj;
obj.func();
return 0;
}
Output:
The value of num is: 7

UNIT-II Inheritance & Pointer


24-11-2022 15
Department Of Computer Engineering
1 Inheritance.
1.3 Relationship Between Base class & Derived Class:
Consider inheritance hierarchy containing types of bank accounts in bank.
Consider that there are two types of bank accounts(classes) :

• Base class Account.


• Derived class Saving Account.

 Saving account class derived from Base class as a public inheritance.


 This relationship between Base class and Derived class can be exhibited from the
following programming Example:

UNIT-II Inheritance & Pointer


24-11-2022 16
Department Of Computer Engineering
1 Inheritance.
1.4 Constructor & Destructor in Derived Class:
A constructor is a special member function whose task is to initialize the object of its class.
Constructor is automatically called when object(instance of class) create.

A constructor is declared and defined as follows:


//class with a constructor.
class integer
{
int m , n ;
public:
integer (void); //constructor declared
---------
--------
};
UNIT-II Inheritance & Pointer
24-11-2022 17
Department Of Computer Engineering
1 Inheritance.
integer : : integer(void) //constructor defined
{
m=0; n=0;
}

UNIT-II Inheritance & Pointer


24-11-2022 18
Department Of Computer Engineering
1 Inheritance.
Destructor:

A destructor as the name implies is used to destroy the objects that have been created by a
constructor.

The destructor is a member function whose name is the same as the class name but is the
same as the class name but is preceded by a tilde.

~ sample( ) //destructor
{

UNIT-II Inheritance & Pointer


24-11-2022 19
Department Of Computer Engineering
1 Inheritance.
Programming Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class parent
{
public:
parent() //constructor
{
cout<<“\n parent class constructor called”;
}
~ parent()
{ cout<<“\n Parent class destructor called”;
}
UNIT-II Inheritance & Pointer
}; 24-11-2022 Department Of Computer Engineering
20
1 Inheritance.
class child : public parent
{
public:
child()
{
cout<<“\n child class constructor called”;
}
~ child()
{ cout<<“\n child class destructor called”;
}
};
int main()
{
Child a;
return 0;
UNIT-II Inheritance & Pointer
} 24-11-2022
Department Of Computer Engineering
21
1 Inheritance.
Output:
Parent class constructor called.
Child class constructor called.
Parent class destructor called.
child class destructor called.

UNIT-II Inheritance & Pointer


24-11-2022 22
Department Of Computer Engineering
1 Inheritance.
2.5 Overriding Member Function:
•Overloading means use of same thing for different purpose.

•It is one of the main features of object oriented programming.

•Function overriding in C++ is a feature that allows us to use a function in the child class
that is already present in its parent class.

•The child class inherits all the data members, and the member functions present in the
parent class.

UNIT-II Inheritance & Pointer


24-11-2022 23
Department Of Computer Engineering
1 Inheritance.
Example:
// C++ program to demonstrate function overriding
#include <iostream>
using namespace std;
class Base
{
public: void print()
{
cout << "Base Function" << endl;
}
};
class Derived : public Base
{
public: void print()
{
cout << "Derived Function" << endl;
}
};

UNIT-II Inheritance & Pointer


24-11-2022 24
Department Of Computer Engineering
1 Inheritance.
int main()
{
Derived derived1;
derived1.print( );
return 0;
}
Output:
Derived Function

UNIT-II Inheritance & Pointer


24-11-2022 25
Department Of Computer Engineering
1 Inheritance.
2.6 Class Hierarchies:

Vehicle

Car Bike

Automatic Manual Gear Non Gear

Fig. Class Hierarchies

UNIT-II Inheritance & Pointer


24-11-2022 26
Department Of Computer Engineering
1 Inheritance.
2.7 Public & Private Inheritance:

Public Inheritance means derived class can access the public members of base class.
•Example

Class derived : public base

Public keyword is above statement signifies all public members of base class are accessible
in the derive class.

Private keyword in above statement signifies no member of base class will be accessible.
•Example:
Class derived : private base
UNIT-II Inheritance & Pointer
24-11-2022 27
Department Of Computer Engineering
1 Inheritance.
2.8 Member Access Control:

UNIT-II Inheritance & Pointer


24-11-2022 28
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
2.9 Types of Inheritance:

UNIT-V Object Oriented Programming


24-11-2022 29
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
Single Inheritance:
•Single inheritance have only one base class and one derived class.
•Derived class inherits the base class member’s also.
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class company
{
char Com_name[30];
int year;
public:
void getdata();
void putdata();
};
void company :: getdata()
{
cout<<"\n Enter Name of Company:";
cin>>Com_name;
cout<<"\n Enter Year of Company: ";
cin>>year;
}
void company::putdata()
{
cout<<"\n Name Of company:"<<Com_name; UNIT-V Object Oriented Programming
24-11-2022
cout<<"\n Year Of Company:"<<year; Department Of Computer Engineering
30
};
OBJECT ORIENTED PROGRAM:
class worker : public company
{
char name[30];
int age;
public:
void get()
{
getdata();
cout<<"\n Enter Name of Employee:";
cin>>name;
cout<<"\n Enter Age of Employee:";
cin>>age;
}
void put()
{
putdata();
cout<<"\n Name of Employee:"<<name;
cout<<"\n Age of Employee:"<<age;
}
};
void main()
{
clrscr();
worker w1;
w1.get();
w1.put(); UNIT-V Object Oriented Programming
24-11-2022 31
getch(); Department Of Computer Engineering
}
OBJECT ORIENTED PROGRAM:
Multiple Inheritance:
•Multiple inheritance Class can be derived from more than one base class.
•In multiple inheritance derived class can access the members of both the base classes.
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class company
{
char Com_name[30];
int year;
public:
void getdata();
void putdata();
};
void company :: getdata()
{
cout<<"\n Enter Name of Company:";
cin>>Com_name;
cout<<"\n Enter Year of Company: ";
cin>>year;
}
void company::putdata()
{
cout<<"\n Name Of company:"<<Com_name; UNIT-V Object Oriented Programming
24-11-2022
cout<<"\n Year Of Company:"<<year; Department Of Computer Engineering
32
};
OBJECT ORIENTED PROGRAM:
class worker
{
char name[30];
int age;
public:
void get()
{
cout<<"\n Enter Name of Employee:";
cin>>name;
cout<<"\n Enter Age of Employee:";
cin>>age;
}
void put()
{ cout<<"\n Name of Employee:"<<name;
cout<<"\n Age of Employee:"<<age;
}
};
class product: public company, public worker
{
char prod_name[30];
float price;
public: UNIT-V Object Oriented Programming 24-11-2022 33
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
void get1()
{ getdata();
get();
cout<<"\n Enter the product name:";
cin>>prod_name;
cout<<"\n Enter the price:";
cin>>price;
}
void put1()
{ putdata();
put();
cout<<"\n The Product name is:"<<prod_name;
cout<<"\n The Product Price:"<<price;
}
}; void main()
{
clrscr();
product p1;
p1.get1();
p1.put1();
getch();
} UNIT-V Object Oriented Programming 24-11-2022 34
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
Hierarchical Inheritance:
•Hierarchical inheritance one base class creates many subclasses.
•If more than one base class is inherited from the base class its known as hierarchical inheritance.
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class company
{
char Com_name[30];
int year;
public:
void getdata();
void putdata();
};
void company :: getdata()
{ cout<<"\n Enter Name of Company:";
cin>>Com_name;
cout<<"\n Enter Year of Company: ";
cin>>year; UNIT-V Object Oriented Programming 24-11-2022 35
} Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
void company::putdata()
{
cout<<"\n Name Of company:"<<Com_name;
cout<<"\n Year Of Company:"<<year;
};
class worker:public company
{ char name[30];
int age;
public:
void get()
{ getdata();
cout<<"\n Enter Name of Employee:";
cin>>name;
cout<<"\n Enter Age of Employee:";
cin>>age;
}
void put()
{ putdata();
cout<<"\n Name of Employee:"<<name;
cout<<"\n Age of Employee:"<<age;
}
UNIT-V Object Oriented Programming 24-11-2022
}; Department Of Computer Engineering
36
OBJECT ORIENTED PROGRAM:
class product: public company
{
char prod_name[30];
float price;
public:
void get1()
{
getdata();
cout<<"\n Enter the product name:";
cin>>prod_name;
cout<<"\n Enter the price:";
cin>>price;
}
void put1()
{
putdata();
cout<<"\n The Product name is:"<<prod_name;
cout<<"\n The Product Price:"<<price;
}
};
UNIT-V Object Oriented Programming 24-11-2022 37
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
void main()
{
clrscr();
worker w1;
product p1;
w1.get();
p1.get1();
w1.put();
p1.put1();
getch();
}

UNIT-V Object Oriented Programming 24-11-2022 38


Department Of Computer Engineering
1 Inheritance.
2.10 Ambiguity in Multiple Inheritance:
• in Object oriented Programming multiple inheritance faces the problem of ambiguity.

•This situation arises when multiple base class have members with same name.

•Derived class faces the ambiguity as which base class version of the member to use.

•Ambiguity can be resolved by using scope resolution operator.

•At the time of calling the member we can specify the name class from we wanted to access
the members.

Object _name.BASE::abc()
UNIT-II Inheritance & Pointer
24-11-2022 39
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
Example:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class company
{
char Com_name[30];
int year;
public:
void getdata();
void putdata();
};
void company :: getdata()
{ cout<<"\n Enter Name of Company:";
cin>>Com_name;
cout<<"\n Enter Year of Company: ";
cin>>year;
}
void company::putdata()
{ cout<<"\n Name Of company:"<<Com_name;
cout<<"\n Year Of Company:"<<year;
UNIT-V Object Oriented Programming 24-11-2022
}; Department Of Computer Engineering
40
OBJECT ORIENTED PROGRAM:
class worker
{
char name[30];
int age;
public:
void getdata()
{
cout<<"\n Enter Name of Employee:";
cin>>name;
cout<<"\n Enter Age of Employee:";
cin>>age;
}
void putdata()
{ cout<<"\n Name of Employee:"<<name;
cout<<"\n Age of Employee:"<<age;
}
};
class product: public company, public worker
{
char prod_name[30];
float price;
UNIT-V Object Oriented Programming 24-11-2022
public: Department Of Computer Engineering
41
OBJECT ORIENTED PROGRAM:
void get1()
{

//getdata();
//get();
cout<<"\n Enter the product name:";
cin>>prod_name;
cout<<"\n Enter the price:";
cin>>price;
}

void put1()
{
//putdata();
//put();
cout<<"\n The Product name is:"<<prod_name;
cout<<"\n The Product Price:"<<price;
}
};
UNIT-V Object Oriented Programming 24-11-2022 42
Department Of Computer Engineering
OBJECT ORIENTED PROGRAM:
void main()
{

clrscr();
cout<<"\n Ambiguity in multiple inheritance";
product p1;
p1.get1();
p1.company::getdata();// ambiguity in multiple inheritance
p1.worker::getdata();

p1.company::putdata();
p1.worker::putdata();
p1.put1();
getch();
}

UNIT-V Object Oriented Programming 24-11-2022 43


Department Of Computer Engineering
1 Inheritance.
2.11 Virtual Base Class:

•Virtual base classes are used in virtual inheritance in a way of preventing multiple “instances” of a
given class appearing in an inheritance hierarchy when using multiple inheritances.

•Virtual base class is used to remove the ambiguity in multiple inheritance.

•This situation arises when there is multiple path to access member of base class.

UNIT-II Inheritance & Pointer


24-11-2022 44
Department Of Computer Engineering
1 Inheritance.
Example:
#include <iostream>
using namespace std;
class A {
public:
void show()
{
cout << "Hello from A \n";
}
};
class B : public virtual A {
};
class C : public virtual A {
};
class D : public B, public C {
};
UNIT-II Inheritance & Pointer
24-11-2022 45
Department Of Computer Engineering
1 Inheritance.

int main()
{
D object;
object.show();
}

Output:
Hello from A.

UNIT-II Inheritance & Pointer


24-11-2022 46
Department Of Computer Engineering
1 Inheritance.
2.12 Abstract Class:

•abstract class in C++ is a class that has at least one pure virtual function (i.e., a function that has
no definition).
•The classes inheriting the abstract class must provide a definition for the pure virtual function;
otherwise, the subclass would become an abstract class itself.

•Sometimes implementation of all function cannot be provided in a base class because we don’t
know the implementation. Such a class is called abstract class.

•A pure virtual function (or abstract function) in C++ is a virtual function for which we can have
implementation, But we must override that function in the derived class, otherwise the derived class
will also become abstract class

UNIT-II Inheritance & Pointer


24-11-2022 47
Department Of Computer Engineering
1 Inheritance.
Example:
#include <iostream>
using namespace std;
class Shape {
public:
virtual int Area() = 0; // Pure virtual function is declared as follows.
// Function to set width.
void setWidth(int w) {
width = w;
} // Function to set height.
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
UNIT-II Inheritance & Pointer
24-11-2022 48
}; Department Of Computer Engineering
1 Inheritance.
// A rectangle is a shape; it inherits shape.
class Rectangle: public Shape {
public:
// The implementation for Area is specific to a rectangle.
int Area() {
return (width * height);
}
};
// A triangle is a shape too; it inherits shape.
class Triangle: public Shape {
public:
// Triangle uses the same Area function but implements it to
// return the area of a triangle.
int Area() {
return (width * height)/2;
}
UNIT-II Inheritance & Pointer
24-11-2022 49
}; Department Of Computer Engineering
1 Inheritance.
int main() {
Rectangle R;
Triangle T;
R.setWidth(5);
R.setHeight(10);
T.setWidth(20);
T.setHeight(8);
cout << "The area of the rectangle is: " << R.Area() << endl;
cout << "The area of the triangle is: " << T.Area() << endl;
}
Output:
The area of the rectangle is: 50
The area of the triangle is: 80

UNIT-II Inheritance & Pointer


24-11-2022 50
Department Of Computer Engineering
1 Inheritance.
2.12 Friend Classes:

•Friend Class A friend class can access private and protected members of other class in which it is declared as
friend.

• It is sometimes useful to allow a particular class to access private members of other class.

•Friend Function Like friend class, a friend function can be given a special grant to access private and
protected members. A friend function can be:

•a) A member of another class

•b) A global function

UNIT-II Inheritance & Pointer


24-11-2022 51
Department Of Computer Engineering
1 Inheritance.
Example:

#include <iostream>
class A {
private:
int a;
public:
A() { a = 0; }
friend class B; // Friend Class
};

class B {
private:
int b;

public:

UNIT-II Inheritance & Pointer


24-11-2022 52
Department Of Computer Engineering
1 Inheritance.
void showA(A& x)
{
// Since B is friend of A, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
};
int main()
{ A a;
B b;
b.showA(a);
return 0;
}
Output:
A::a=0 UNIT-II Inheritance & Pointer
24-11-2022 53
Department Of Computer Engineering
1 Inheritance.
2.13 Static Class:

•Static data members in a class are shared by all the class objects as there is only one copy of them in
the memory, regardless of the number of objects of the class.

•Static methods in a class can only access static data members, other static methods or any methods
outside the class.

•We cannot use new keyword to create object with static class.

•We can access member of class using name of the class itself.

UNIT-II Inheritance & Pointer


24-11-2022 54
Department Of Computer Engineering
1 Inheritance.
2.13 Nested Class:

•Nested class which is declared in another enclosing class.

•A nested class is a member and as such has the same access rights as any other member.

•The member of an enclosing class have no special access to members of a nested class

UNIT-II Inheritance & Pointer


24-11-2022 55
Department Of Computer Engineering

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy