Sunteți pe pagina 1din 25

Anidra Katiyar

anidrakatiyar@sistec.ac.in
SISTec-R Ratibad Campus
Department of Computer
Science & Engineering

Object Oriented Programming &


July 15, 2020 1
Methodology
References

The presentation is based on


Asst.Prof.Anidra Katiyar Lecture Note
Reference1: Mastering C++
K R VENUGOPAL
University Visvesvaraya College of Engineering
Bangalore University, Bangalore India
RAJKUMAR BUYYA
The University of Melbourne and Manjrasoft Pvt. Ltd.,
Melbourne, Australia
Page : 394 to 402
Reference1: Programming in C++
By: LAXMISHA RAI(page 280-283)

Object Oriented Programming &


July 15, 2020 2
Methodology
Subject,Code,Sem,Unit

Subject : Object Oriented


Programming & Methodology
Unit-III
Subject Code: CS-305
Semester: 3rd

Object Oriented Programming &


July 15, 2020 3
Methodology
Lecture Topics

Lecture on
Topic:
1-Relationship- Inheritance

Object Oriented Programming &


July 15, 2020 4
Methodology
Relationship
Relationship:
A relationship describes the bond betweenobjects. Examples of
this are dependency, association, and generalization.
Realationships are not subclassified.
A relationship between classes whereby one class can be said to
be “a kind of” (AKO) of other class.

Object Oriented Programming &


July 15, 2020 5
Methodology
Inheritance
Inheritance is the most power ful features of OOP
By organizing classes into a classification hierarchy
It give the extra dimension to the encapsulation of abstract
data types because it enables classes the to inherits the
attributes and methods form others classes.
The inheriting class can then add extra attributes and /or
methods its own.
Terminology used for inheritance encompasses a number of
terms.
1-Derived Class or SubClass or ChildClass
2-BaseClass or SuperClass or ParentClass
3-Ancestor: A class’s ancestor are those from which its own
super classes inherits.
Object Oriented Programming &
July 15, 2020 6
Methodology
Inheritance
4-Descendent: A class’s descendent are those which inherits
from its subclasses.
Questions: What is the purpose of inheritance? Why should
we wish to inherits the attributes and methods one class into
another.
Answer: The two complementary role of inheritance in an
object oriented application:
1-Specialization:-Extending the functionality of existing class.
2-Generalization:-Sharing commonality between two or more
classes.
These two are not by any means mutually exclusive.
The product of inheritance of this kind is known as a
classification hierarchy.
Object Oriented Programming &
July 15, 2020 7
Methodology
Inheritance
Inhertance:Question. What is the object oriented way of
becoming rich?
Answer:By way of inheritance.
It involve by sharing of attributes and methods among a
numbrs of classes based on hierarchical relationship. This is not
same as the association or aggregation.
A class can be created at a generic level and the refind
successively into finer subclasses.
Each subclass automatically retains or inherits all the
characteristics of its superclass and can add its own.
We know it is interesting to note that driving the common
attributes of more than one object create a class.
Inheritance takes this basic concept further so that the class
themselves can be generalized.
Object Oriented Programming &
July 15, 2020 8
Methodology
Inheritance
Example:
Diagram

Circle Triangle Square


Diagram can be defined as a class at a broad level. It would be
super class. Circle triangle and square ect are all kind of
diagrams. Suppose the super calss diagram has an attributes
called as angle .This attributes value indicates the angle
inside a diagram. This attribute would be common to all
diagrams.

Object Oriented Programming &


July 15, 2020 9
Methodology
Inheritance
Inheritance in object-oriented programming derives a more
specific concept from a moregeneral one. That is what we do in
real life. For example, the concept of animal in biology is more
general than the concept of horse. We can give the definition of
an animal; we can then add to the definition to create the
definition of a horse. There is an is-a relation from the more
specific to the more general. All horses are animals, but not all
animals are horses.

Object Oriented Programming &


July 15, 2020 10
Methodology
Inheritance
General Idea
To show the relationship between classes in inheritance, we use
the Unified Modeling Language (UML). UML is a language
that shows the relationship between classes and objects
graphically. The classes are shown as rectangular boxes in
UML. The inheritance relation is shown by a line ending in a
hollow triangle that goes from the more specific class to a more
general class.

In C++, the most general class is called the base class and a more
specific class iscalled the derived class. A more general class is also
known as a superclass; a more specificclass is also known as a subclass.
Object Oriented Programming &
July 15, 2020 11
Methodology
Inheritance
A horse should first be an animal, then a horse. That is why C+
+ saysthat a derived class extends its base class. The term
extends here means the derived class must have all of the data
members and member functions defined in the base class, but it
canadd to the list. In other words, the derived class inherits all
of the data members and memberfunctions of the base class
(with the exception of constructors, destructor, and
assignmentoperators that need to be redefined), and it can
create new data members and member functions.We will
discuss later why constructors, the destructor, and assignment
operators can not be inherited.
The derived class inherits all members (with some
exceptions) from the base class, and it can add to them.

Object Oriented Programming &


July 15, 2020 12
Methodology
Inheritance
To create a derived class from a base class, we have three
choices in C++: private inheritance,protected inheritance, and
public inheritance. To show the type of the inheritancewe want
to use, we insert a colon after the class, followed by one of the
keywords (private, protected, or public). Figure shows these
three types of inheritance in which B is the base class and D is
the derived class

Object Oriented Programming &


July 15, 2020 13
Methodology
Inheritance
The default type of inheritance is private. In other words, if we
do not specify the type(public, protected, or private), the system
assumes that we want private inheritance. Sinceprivate
inheritance, as we will see later, is seldom used, we need to
explicitly define the typeof the inheritance. The most common
is the public inheritance.

Object Oriented Programming &


July 15, 2020 14
Methodology
Inheritance
“a kind of “or “a part of”
Each level of classification hierarchy contain more specific
types of class, each of which must be “ a kind of” of the class
from which it inherits.
It is important to make this distinction between a class which is
a kind of other class and one which is “ a part of” another
class.
Example: we would not make “apartment” a derived class of
“apartment block” because it does not make sense to say “ an
apartment is a kind of apartment block” an apartment is not “a
kind of “ apartment block but “a part of “ it

Object Oriented Programming &


July 15, 2020 15
Methodology
Inheritance
Different classes or different states?
When designing classification hierarchy we have to make
distinction between objects which need to be represented by
different classes, and those which belong to the same class but
may have different state.
An appropriate classification, since it actually relates to the
state of an attributes common to all building- their height .
Since all building have a height ,this should be an inherited
attribute in the base class. While “tall building” may be said to
be “a kind of building” it does not represent a distinct type.
After all what specific meanings can be given to “short and
“tall” . We are usually talking about differences in state
attributes values rather than differences in fundamental type.

Object Oriented Programming &


July 15, 2020 16
Methodology
Inheritance
What do object inherits?
We have talked about the ability of classes to “ inherits” both
attributes and methods. From ‘base class’, but what dose this
means in practical terms?
The key point is that we are talking about inheritance between
classes, not object . Remember that class does not contain any
state values, it only act as a ‘blueprint’ to define what attribute
each object in the class will have.
The state values of those attributes are contained in individual
object.When we say that derived class inherits from a base
class, it means that all the attributes and methods in the base
class are automatically included in derived class. As for as
object concerned there is no hierarchy. This is an appropriate
distinction, since it means that derived class object do not
inherits any state values from base class objects.

Object Oriented Programming &


July 15, 2020 17
Methodology
Destructor

Object Oriented Programming &


July 15, 2020 18
Methodology
Destructor Example

Object Oriented Programming &


July 15, 2020 19
Methodology
Destructor Example

Object Oriented Programming &


July 15, 2020 20
Methodology
Destructor Example

Object Oriented Programming &


July 15, 2020 21
Methodology
Construction & Destruction

Object Oriented Programming &


July 15, 2020 22
Methodology
Construction & Destruction

Object Oriented Programming &


July 15, 2020 23
Methodology
Construction & Destruction

Object Oriented Programming &


July 15, 2020 24
Methodology
Destructor Example

Object Oriented Programming &


July 15, 2020 25
Methodology

S-ar putea să vă placă și