Object Oriented Programming Language (OOP)

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Learners Support Publications Inheritance: Extending Classes.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
Road Map Introduction to object oriented programming. Classes
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Computer Programming & Applications Mohamed Iqbal Pallipurath Lecture 02P. 1 Inheritance and Overloading Lecture 28.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Learners Support Publications Object Oriented Programming.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 07 classes 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
NESTED CLASS. Apa itu nested 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.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Modern Programming Tools And Techniques-I
Classes (Part 1) Lecture 3
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Class and Objects UNIT II.
Inheritance Concept of Inheritance . Types of Inheritance .
2.7 Inheritance Types of inheritance
Review: Two Programming Paradigms
Inheritance in Java.
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Lecture 1 Introduction.
Lecture 4-7 Classes and Objects
Object Oriented Analysis and Design
CS5253 Workshop I Lecturer: Dr. Lusheng Wang
Object-oriented Design in Processing
Testing with OO OO has several key concepts:
Object-oriented Design in Processing
C++ Inheritance.
Principles of object – oriented programming UNIT-1 Chapter-1.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CPS120: Introduction to Computer Science
Inheritance:Concept of Re-usability
Institute of Petroloeum Technology, Gandhinagar
Object-oriented Design in Processing
CPS120: Introduction to Computer Science
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Object-oriented Design in Processing
ITE “A” GROUP 2 ENCAPSULATION.
Object Oriented Programming (OOP) Lecture No. 12
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Object Oriented Programming Language (OOP) بسم الله الرحمن الرحيم Object Oriented Programming Language (OOP)

Course Outline OOP Concept OOP History Basic Concept Classes Types Encapsulation Inheritance Private Inheritance Protected Inheritance Public Inheritance

Multiple Inheritance Polymorphism OOP With C ++ Language

Explanation Syntax: class class-name 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 { access:specifier private data and functions }

In the above syntax the every class has a unique name, the "access:specifier" can either private, public, protected. The "protected" is used when inhertinace is applied.

Example: #include <iostream.h> class dat { private: int sdata; public: void setdat( int a) sdata =a; } void show( ) cout << "\nSet data is " << sdata; }; void main() { dat x,y; x.setdat(1000); y.setdat(1245); x.show(); y.show(); } Result: Set Data is:1000 Set Data is:1245

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".

Access 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.

Nested 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 "Nest::Display".

Example: void main() { Nest::Display x; x.sum(12, 10); x.show(); } #include <iostream.h> class Nest { public: class Display private: int s; void sum( int a, int b) s =a+b; } void show( ) cout << "\nSum of a and b is:: " << s; }; void main() { Nest::Display x; x.sum(12, 10); x.show(); } Result: Sum of a and b is::22

In the above example, the nested class "Display" is given as "public" member of the class "Nest".

Local 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.

Example: Result: The value assigned to y is::20 #include <iostream.h> int y; void g(); int main() { g(); return 0; } void g() class local { public: void put( int n) ::y=n; int get() {return ::y;} } ab; ab.put(20); cout << "The value assigned to y is::"<< ab.get(); } Result: The value assigned to y is::20

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.

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.

Following are the basic elements of Object oriented programming(OOPS) Classes Inheritance Dynamic Binding Polymorphism Message Passing Encapsulation

Objects 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

Example: In the above example "x" is an object of the class "Cube“ #include <iostream.h> class Cube { public: int cub( val) r = val*val*val; return r; } void show() { cout << "The cube is::" << r; } private: int val, r; }x; void main() { Cube x; x.cub(2); x.show(); } Result: The cbe is :: 8 In the above example "x" is an object of the class "Cube“ used to access the functions inside the class.

Classes 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".

Syntax: class class-name { private: variable declaration; function declaration; public: };

Example: void main() { Square x; x.area(10); x.show(); } #include <iostream.h> class Square { private: int side, a; public: int area( side) a =side*side; return a; } void show() cout << "The area is::" << a; }; void main() { Square x; x.area(10); x.show(); } Result: The area is:: 100

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".

The End of lecture

Explanation 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.

Derived Class Visibility The following table lists the visibility of the base class members in the derived classes   Derived Class Visibility Base Class Visibility Public derivation Private derivation Protected derivation Private Not inherited Protected Public

Following are the different types of inheritance followed in C++. Single Inheritance Multiple Inheritance Hierarchical Inheritance Multilevel Inheritance Hybrid Inheritance

Example: #include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Square: public Value int square() { return (val*val); } int main () { Square sq; sq.set_values (5); cout << "The square of 5 is::" << sq.square() << endl; return 0; } Result: The square of 5 is:: 25 In the above example the object "val" of class "Value" is inherited in the derived class "Square".

Explanation Single Inheritance is method in which a derived class has only one base class.

Example: # include <iostream.h> class Value { protected: int val; public: void set_values (int a) { val=a;} }; class Cube: public Value int cube() { return (val*val*val); } int main () { Cube cub; cub.set_values (5); cout << "The Cube of 5 is::" << cub.cube() << endl; return 0; } Result: The 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.

Multiple Inheritance Explanation Multiple Inheritance is a method by which a class is derived from more than one base class.

Example: Result: The area of the square is:: 25 int main () { Area r; #include <iostream.h> using namespace std; class Square { protected: int l; public: void set_values (int x) { l=x;} }; class CShow void show(int i); void CShow::show (int i) cout << "The area of the square is::" << i << endl; } class Area: public Square, public CShow int area() { return (l *l); } int main () { Area r; r.set_values (5); r.show(r.area()); return 0; } Result: The area of the square is:: 25

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++.

Hierarchical Inheritance Explanation Hierarchical Inheritance is a method of inheritance where one or more derived classes is derived from common base class.

cout << "The square value is::" << s.sq() << endl; Example: #include <iostream.h> class Side { protected: int l; public: void set_values (int x) { l=x;} }; class Square: public Side int sq() { return (l *l); } class Cube: public Side public: int cub() { return (l*l*l); } int main () { Square s; s.set_values (10); cout << "The square value is::" << s.sq() << endl; Cube c; c.set_values (20); cout << "The cube value is::" << c.cub() << endl; return 0; } Result: The square value is:: 100 The cube value is::8000

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++.

Multilevel Inheritance Explanation Multilevel Inheritance is a method where a derived class is derived from another derived class.

#include <iostream #include <iostream.h> class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:\n"<< rollno << "\n"; } }; class marks : public mm int sub1; int sub2; void get_marks(int x,int y) { sub1 = x; sub2 = y; }

void put_marks(void) { cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; } }; class res : public marks { protected: float tot; public: void disp(void) tot = sub1+sub2; put_num(); put_marks(); cout << "Total:"<< tot; } int main() { res std1; std1.get_num(5); std1.get_marks(10,20); std1.disp(); return 0; } Result: Roll 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++.

Hybrid Inheritance Explanation "Hybrid Inheritance" is a method where one or more types of inheritance are combined together and used.

#include <iostream #include <iostream.h> class mm { protected: int rollno; public: void get_num(int a) { rollno = a; } void put_num() { cout << "Roll Number Is:"<< rollno << "\n"; } }; class marks : public mm int sub1; int sub2; void get_marks(int x,int y) { sub1 = x; sub2 = y; } void put_marks(void) cout << "Subject 1:" << sub1 << "\n"; cout << "Subject 2:" << sub2 << "\n"; }

class extra int main() { res std1; std1.get_num(10); protected: float e; public: void get_extra(float s) {e=s;} void put_extra(void) { cout << "Extra Score::" << e << "\n";} }; class res : public marks, public extra{ float tot; void disp(void) { tot = sub1+sub2+e; put_num(); put_marks(); put_extra(); cout << "Total:"<< tot; } int main() { res std1; std1.get_num(10); std1.get_marks(10,20); std1.get_extra(33.12); std1.disp(); return 0; } Result: Roll Number Is: 10 Subject 1: 10 Subject 2: 20 Extra score:33.12 Total: 63.12