UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS

Slides:



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

Understand and appreciate Object Oriented Programming (OOP) Objects are self-contained modules or subroutines that contain data as well as the functions.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Lecture 9 Concepts of Programming Languages
C++ fundamentals.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
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.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to Object-Oriented Programming Lesson 2.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
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.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming Logic and Design Seventh Edition
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Abstract Data Types and Encapsulation Concepts
Classes and OOP.
Friend Class 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.
Object-Oriented Techniques
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Lecture 1 Introduction.
This pointer, Dynamic memory allocation, Constructors and Destructor
(5 - 1) Object-Oriented Programming (OOP) and C++
Object Oriented Analysis and Design
Subject Name: Object Oriented Programming with C++
Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types and Encapsulation Concepts
Procedural Programming
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object-Oriented Programming
(5 - 1) Object-Oriented Programming (OOP) and C++
Principles of object – oriented programming UNIT-1 Chapter-1.
CPS120: Introduction to Computer Science
COP 3330 Object-oriented Programming in C++
Introduction to Object-Oriented Programming
Object-Oriented Programming
Submitted By : Veenu Saini Lecturer (IT)
CPS120: Introduction to Computer Science
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS

Introduction: Object Oriented Paradigm 1.The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach. 2.Treats data as a critical element and does not move data to flow free around the system.

1.It ties data more closely to the function that operate on it, and protects it from accidental modification from outside function. 2.OOP allows decomposition of a problem into a number of entities called objects and then builds data and function around these objects. 3.The data of an object can be accessed only by the function associated with that object. However, function of one object can access the function of other objects.

Key features of Object Oriented Programming Emphasis is on data rather than procedure. Programs are divided into what are known as objects. Data structures are designed such that they characterize the objects. Functions that operate on the data of an object are ties together in the data structure.  

5.Data is hidden and cannot be accessed by external function. 6.Objects may communicate with each other through function. 7.New data and functions can be easily added whenever necessary. 8.Follows bottom up approach in program design.

Basic Concepts of Object Oriented Programming Objects Classes Data abstraction and encapsulation Inheritance Polymorphism Dynamic binding Message passing

Objects Objects are the basic run time entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program has to handle. They may also represent user-defined data such as vectors, time and lists

Classes 1.Objects contain data, and code to manipulate that data. 2. The entire set of data and code of an object can be made a user-defined data type called class. 3. Objects are variables of the type class. 4. We can create any number of objects belonging to that class.

5. A class is thus a collection of objects similar types 5.A class is thus a collection of objects similar types. For examples, Mango, Apple and orange members of class fruit. Fruit Mango; Will create an object mango belonging to the class fruit

Data Abstraction and Encapsulation The wrapping up of data and function into a single unit (called class) is known as encapsulation. The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program.  

4. This insulation of the data from direct access by the program is called data hiding or information hiding. 5.Abstraction refers to the act of representing essential features without including the background details or explanation. 6.Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, wait, and cost, and function operate on these attributes.

Inheritance Inheritance is the process by which objects of one class acquired the properties of objects of another classes. It supports the concept of hierarchical classification. Provides the idea of reusability

Polymorphism Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the ability to take more than one form. An operation may exhibit different behaviors in different instances.  

Dynamic Binding 1.Binding refers to the linking of a procedure call to the code to be executed in response to the call.   2.Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time.

Message Passing An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, involves the following basic steps: Creating classes that define object and their behavior.  

4. Creating objects from class definitions 5 4.Creating objects from class definitions 5.Establishing communication among objects. 6.Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another

Constructors   Definition: 1.It is a special member function. 2.Its task is to initialize the objects. 3.Its name is the same as the class name. 4.It is invoked when an object is created.

Ex: class circle { public: circle( ){ } //constructor is defined };  

Properties: Should be declared in the public section. Cannot be inherited. Can have default arguments. No return type can be specified for constructors. Not even void type can be specified for return type in constructor. So constructor can't return values.

Types Default constructor Parameterized constructor Copy constructor Dynamic constructor (dynamical initialization of objects) Default argument constructor

Default constructor A Constructor that accepts no parameters is called the Default constructor. Syntax: A constructor is declared and defined as follows class s { public: int a; s( ); // constructor is declared };

s::s( ) // constructor is defined { a=0; } void main() { s o; // constructor is invoked }

Parameterized Constructor: The constructors that can take arguments are called Parameterized constructors. Example: class s { public: int a; s(int x); // constructor is declared };

s::s(int x) // constructor is defined { a=x; } void main() { int a1; a1=12; s o(a1); / s o(12); // constructor is invoked

Copy Constructor A copy constructor is used to declare and initialize an object from another object. s (s &o); 2.There are two ways to invoke a copy constructor as follows i. s o(o1) -define the object o and at the same time initialize it to the values of o1 ii. s o = o1 -define the object o and at the same time initialize it to the values of o1

Example: class s { public: int a; s ( ) cin>>a; } s(s &o); // constructor is declared void display() cout<<a; } };

s::s(s &o) // constructor is defined { a=o s::s(s &o) // constructor is defined { a=o.a; } void main() s o; s o1(o); //copy constructor is called s o2=o; //copy constructor is called o1.display(); o2.display();

Static Member A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. It can be initialized outside the class using the scope resolution operator :: to identify which class it belongs to.

Example: #include <iostream> using namespace std; class Box { public: static int objectCount; // Constructor definition Box(double l=2.0, double b=2.0, double h=2.0)

// Initialize static member of class Box int Box::objectCount = 0; int main(void) { Box Box1(3.3, 1.2, 1.5); // Declare box1 Box Box2(8.5, 6.0, 2.0); // Declare box2 // Print total number of objects. cout << "Total objects: " << Box::objectCount << endl; return 0; } When the above code is compiled and executed, it produces the following result: Constructor called. Total objects: 2

The Role of this pointer The keyword this identifies a special type of pointer. Suppose that you create an object named x of class A, and class A has a nonstatic member function f(). If you call the function x.f(), the keyword this in the body of f() stores the address of x. You cannot declare the this pointer or make assignments to it.

3.A static member function does not have a this pointer. 4.The type of the this pointer for a member function of a class type X, is X* const. 5. If the member function is declared with the const qualifier, the type of the this pointer for that member function for class X, is const X* const. 6.A const this pointer can by used only with const member functions

#include <iostream> using namespace std;   struct X { private: int a; public: void Set_a(int a) {

// The 'this' pointer is used to retrieve 'xobj // The 'this' pointer is used to retrieve 'xobj.a' // hidden by the automatic variable 'a' this->a = a; } void Print_a() { cout << "a = " << a << endl; } }; int main() { X xobj; int a = 5; xobj.Set_a(a); xobj.Print_a();

In the member function Set_a(), the statement this->a = a uses the this pointer to retrieve xobj.a hidden by the automatic variable a. Unless a class member name is hidden, using the class member name is equivalent to using the class member name with the this pointer and the class member access operator (->).

Storage class A storage class defines the visibility and life-time of variables within a Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a Program auto register static extern mutable

i.The auto Storage Class 1.The auto storage class is the default storage class for all local variables. Example:  { int mount; auto int month; }

The register Storage Class 1.The register storage class is used to define local variables that should be stored in a register instead of RAM. 2. This means that the variable has a maximum size equal to the register size and cannot have the unary '&' operator applied to it . Example:   { register int miles; }

3.The register should only be used for variables that require quick access such as counters. 4.It should also be noted that defining 'register' does not mean that the variable will be stored in a register. 5.It means that it might be stored in a register depending on hardware and implementation restrictions.

iii.The static Storage Class The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

Example:   #include <iostream> // Function declaration void func(void); static int count = 10; /* Global variable */ main() { while(count--) func(); } return 0;

} // Function definition void func( void ) { static int i = 5; // local static variable i++; std::cout << "i is " << i ;   the program files.

iv.The extern Storage Class 1.The extern storage class is used to give a reference of a global variable that is visible to all. 2.When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

3.The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

v.The mutable Storage Class 1.The mutable specifier applies only to class objects.It allows a member of an object to override constant member functions. 2.That is, a mutable member can be modified by a constant member function.