Title: Object Oriented Programming Language (OOP)
1Object Oriented Programming Language (OOP)
??? ???? ?????? ??????
- Presented by Ustaz Mutaz Elradi Saad alla Saeed
- Faculty of Science Technologies
- Nile Valley University
- Sudan , Atbara
2Course Outline
- OOP Concept
- OOP History
- Basic Concept
- Classes Types
- Encapsulation
- Inheritance
- Private Inheritance
- Protected Inheritance
- Public Inheritance
3- Multiple Inheritance
- Polymorphism
- OOP With C Language
4Explanation
- A Class is a way to bind the data and its
associated functions together. All the elements
of a class are private by default, even elements
can be declared as public or protected. An object
is an instance of a class. - Syntax
- class class-name
-
- accessspecifier
- private data and functions
-
5- In the above syntax the every class has a unique
name, the "accessspecifier" can either private,
public, protected. The "protected" is used when
inhertinace is applied.
6Example
- include ltiostream.hgt
- class dat
-
- private
- int sdata
- public
- void setdat( int a)
-
- sdata a
-
- void show( )
-
- cout ltlt "\nSet data is " ltlt sdata
-
-
void main() dat x,y x.setdat(1000)
y.setdat(1245) x.show() y.show()
Result Set Data is1000 Set Data is1245
7- In the above class example the "private" object
"sdata" is used only within the function. But the
functions "setdat", "show" are used in the main
function since they are "public".
8Access specifiers
- defines the access rights for the statements or
functions that follows it until another access
specifier or till the end of a class. The three
types of access specifiers are "private",
"public", "protected". - private The members declared as "private" can be
accessed only within the same class and not from
outside the class. - public The members declared as "public" are
accessible within the class as well as from
outside the class. - protected The members declared as "protected"
cannot be accessed from outside the class, but
can be accessed from a derived class. This is
used when inheritaance is applied to the members
of a class.
9Nested class
- Nested class is a class defined inside a class,
that can be used within the scope of the class in
which it is defined. In C nested classes are
not given importance because of the strong and
flexible usage of inheritance. Its objects are
accessed using "NestDisplay".
10Example
- include ltiostream.hgt
- class Nest
-
- public
- class Display
-
- private
- int s
- public
- void sum( int a, int b)
-
- s ab
-
- void show( )
-
- cout ltlt "\nSum of a and b is " ltlt s
-
-
-
void main() NestDisplay x
x.sum(12, 10) x.show()
Result Sum of a and b is22
11- In the above example, the nested class
"Display" is given as "public" member of the
class "Nest".
12Local class
- Explanation
- Local class is a class defined inside a
function. Following are some of the rules for
using these classes. - Global variables declared above the function can
be used with the scope operator "". - Static variables declared inside the function can
also be used. - Automatic local variables cannot be used.
- It cannot have static data member objects.
- Member functions must be defined inside the local
classes. - Enclosing functions cannot access the private
member objects of a local class.
13Example
include ltiostream.hgt int y void
g() int main() g()
return 0 void g()
class local public void
put( int n) yn
int get() return y ab
ab.put(20) cout ltlt "The value assigned
to y is"ltlt ab.get()
ResultThe value assigned to y is20
14- In the above example, the local class
"local" uses the variable "y" which is declared
globally. Inside the function it is used using
the "" operator. The object "ab" is used to
set, get the assigned values in the local class.
15- Explanation
- Object Oriented programming is method of
programming where - - a system is considered as a collection of
objects that interact together to accomplish
certain tasks. Objects are entities that
encapsulate data and procedures that operate on
the data. - In OOPS first a concept known as "Object Oriented
Analysis (OOA)" is used to specify the objects in
term of real world requirements, their behavior
and interactions required. The next concept would
be the "Object Oriented Design (OOD)" that
converts these real-time requirements as a
hierarchy of objects in terms of software
development requirement. Finally OOPS is used to
implement the requirements using the C
programming language. - The main purpose of object oriented programming
is to simplify the design, programming and most
importantly debugging a program. So to modify a
particular data, it is easy to identify which
function to use. To add additional features it is
easy to identify where to add functions and its
related data.
16- Following are the basic elements of Object
oriented programming(OOPS) - Object
- Classes
- Inheritance
- Dynamic Binding
- Polymorphism
- Message Passing
- Encapsulation
-
17Objects
- Explanation
- Objects are instance of a class, that
interact with each other at runtime. In OOPs,
Objects are declared at the end of the class
definition or after the "" braces. They can be
of any type based on its declaration
18Example
- include ltiostream.hgt
- class Cube
-
- public
- int cub( val)
-
- r valvalval return r
-
- void show()
- cout ltlt "The cube is" ltlt r
- private
- int val, r x
void main() Cube x x.cub(2)
x.show()
ResultThe cbe is 8
In the above example "x" is an object of the
class "Cube used to access the functions inside
the class.
19Classes
- Explanation has the data and its associated
function wrapped in it. Classes are also known as
a collection of similar objects or objects of
same type. In the OOPs concept the variables
declared inside a class are known as "Data
Members" and the functions are known as "Member
Functions".
20Syntax
- class class-name
-
- private
- variable declaration
- function declaration
- public
- variable declaration
- function declaration
-
21Example
- include ltiostream.hgt
- class Square
-
- private
- int side, a
- public
- int area( side)
-
- a sideside
- return a
-
- void show()
-
- cout ltlt "The area is" ltlt a
-
-
void main() Square x
x.area(10) x.show()
ResultThe area is 100
22- In the above OOPs example the class "square" has
functions "area" and "show" to calculate the area
of the square and to display the area. so all
these are objects that are related to the class
"Square".
23The End of lecture
24Explanation
- Inheritance is a method by which new classes are
created or derived from the existing classes.
Using Inheritance some qualities of the base
classes are added to the newly derived class,
apart from its own features The advantage of
using "Inheritance" is due to the reusability of
classes in multiple derived classes. The ""
operator is used for inheriting a class.
25The following table lists the visibility of the
base class members in the derived classes
Derived Class Visibility Derived Class Visibility Derived Class Visibility
Base Class Visibility Public derivation Private derivation Protected derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public Public Private Protected
26Following are the different types of inheritance
followed in C.
- Single Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
- Hybrid Inheritance
27Example
- include ltiostream.hgt
- class Value
-
- protected
- int val
- public
- void set_values (int a)
- vala
-
- class Square public Value
-
- public
- int square()
- return (valval)
-
-
int main () Square sq sq.set_values (5)
cout ltlt "The square of 5 is" ltlt sq.square()
ltlt endl return 0
ResultThe square of 5 is 25
In the above example the object "val" of class
"Value" is inherited in the derived class
"Square".
28Explanation
- Single Inheritance is method in which a derived
class has only one base class.
29Example
- include ltiostream.hgt
- class Value
-
- protected
- int val
- public
- void set_values (int a)
- vala
-
- class Cube public Value
-
- public
- int cube()
- return (valvalval)
-
int main () Cube cub cub.set_values (5)
cout ltlt "The Cube of 5 is" ltlt cub.cube() ltlt
endl return 0
ResultThe Cube of 5 is 125
In the above example the derived class "Cube" has
only one base class "Value". This is the single
inheritance OOP's concept.
30Multiple Inheritance
- Explanation
- Multiple Inheritance is a method by which a
class is derived from more than one base class.
31Example
- include ltiostream.hgt
- using namespace std
- class Square
-
- protected
- int l
- public
- void set_values (int x)
- lx
-
- class CShow
-
- public
- void show(int i)
-
- void CShowshow (int i)
-
- cout ltlt "The area of the square
is" ltlt i ltlt endl -
int main () Area r r.set_values
(5) r.show(r.area()) return 0
ResultThe area of the square is 25
32- In the above example the derived class
"Area" is derived from two base classes "Square"
and "CShow". This is the multiple inheritance
OOP's concept in C.
33Hierarchical Inheritance
- Explanation
- Hierarchical Inheritance is a method of
inheritance where one or more derived classes is
derived from common base class.
34Example
- include ltiostream.hgt
- class Side
-
- protected
- int l
- public
- void set_values (int x)
- lx
-
- class Square public Side
-
- public
- int sq()
- return (l l)
-
- class Cube public Side
-
- public int cub()
- return (lll)
int main () Square s s.set_values (10)
cout ltlt "The square value is" ltlt
s.sq() ltlt endl Cube c c.set_values (20) cout
ltlt "The cube value is" ltlt c.cub() ltlt
endl return 0
ResultThe square value is 100 The cube value
is8000
35- In the above example the two derived classes
"Square", "Cube" uses - a single base class "Side". Thus two
classes are inherited from a single class. This
is the hierarchical inheritance OOP's concept in
C.
36Multilevel Inheritance
- Explanation
- Multilevel Inheritance is a method where a
derived class is derived from another derived
class.
37- include ltiostream.hgt
- class mm
-
- protected
- int rollno
- public
- void get_num(int a)
- rollno a
- void put_num()
- cout ltlt "Roll Number Is\n"ltlt rollno
ltlt "\n" -
- class marks public mm
-
- protected
- int sub1
- int sub2
- public
- void get_marks(int x,int y)
- sub1 x
38- void put_marks(void)
- cout ltlt "Subject 1" ltlt sub1 ltlt "\n"
- cout ltlt "Subject 2" ltlt sub2 ltlt "\n"
-
- class res public marks
-
- protected
- float tot
- public
- void disp(void)
-
- tot sub1sub2
- put_num()
- put_marks()
- cout ltlt "Total"ltlt tot
-
int main() res std1 std1.get_num(5)
std1.get_marks(10,20) std1.disp()
return 0
ResultRoll Number Is 5 Subject 1 10 Subject
2 20 Total 30
In the above example, the derived function "res"
uses the function "put_num()" from another
derived class "marks", which just a level
above. This is the multilevel inheritance OOP's
concept in C.
39Hybrid Inheritance
- Explanation
- "Hybrid Inheritance" is a method where one or
more types of inheritance are combined together
and used.
40- include ltiostream.hgt
- class mm
-
- protected
- int rollno
- public
- void get_num(int a)
- rollno a
- void put_num()
- cout ltlt "Roll Number Is"ltlt rollno ltlt
"\n" -
- class marks public mm
-
- protected
- int sub1
- int sub2
- public
- void get_marks(int x,int y)
- sub1 x
41- class extra
-
- protected
- float e
- public
- void get_extra(float s)
- es
- void put_extra(void)
- cout ltlt "Extra Score" ltlt e ltlt "\n"
-
- class res public marks, public extra
- protected
- float tot
- public
- void disp(void)
- tot sub1sub2e
- put_num()
- put_marks()
- put_extra()
int main() res std1 std1.get_num(10)
std1.get_marks(10,20) std1.get_extra(33.12)
std1.disp() return 0
ResultRoll Number Is 10 Subject 1 10
Subject 2 20 Extra score33.12 Total 63.12
42- In the above example the derived class "res"
uses the function "put_num()". Here the
"put_num()" function is derived first to class
"marks". Then it is derived and used in class
"res". This is an example of "multilevel
inheritance-OOP's concept". But the class "extra"
is inherited a single time in the class "res", an
example for "Single Inheritance". Since this code
uses both "multilevel" and "single" inheritance
it is an example of "Hybrid Inheritance".
43The End of Lecture
44CONTACTS
- Mutaz Saeed mutelr_at_gmail.com
- mutelr_at_hotmail.com
- mutazko2000_at_hotmail.com