Object Oriented Programming with C++/ Session 1/ 1 of 50 Basic Object Oriented Concepts Session 1.

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.
Classes & Objects Computer Science I Last updated 9/30/10.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ASP.NET Programming with C# and SQL Server First Edition
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MCA, MSc[IT], MTech[IT],MPhil (Comp.Sci), PGDCA, ADCA, Dc. Sc. & Engg.
C++ fundamentals.
OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
1 INTRODUCTION TO OOP Objective: Know the difference between functional programming and OOP Know basic terminology in OOP Know the importance of OOP Know.
Introduction To System Analysis and design
Object Oriented Software Development
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
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.
Introduction to Object-oriented programming and software development Lecture 1.
BCS 2143 Introduction to Object Oriented and Software Development.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
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.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
SNPL1 Woochang Lim What (Variable) + How (Function) = Object Objects are the physical and conceptual things we find in the universe around us. Object-Oriented.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
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 Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Object-Oriented Programming Chapter Chapter
ISBN Object-Oriented Programming Chapter Chapter
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Introduction to OOP CPS235: Introduction.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Chapter 11 An introduction to object-oriented design.
ISBN Chapter 12 Support for Object-Oriented Programming.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Orientated Programming
Visit for more Learning Resources
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Chapter 3: Using Methods, Classes, and Objects
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
C++.
Basic Object Oriented Approach
Object-Oriented PHP (1)
Lecture 8 Object Oriented Programming (OOP)
C++ Object Oriented 1.
Presentation transcript:

Object Oriented Programming with C++/ Session 1/ 1 of 50 Basic Object Oriented Concepts Session 1

Object Oriented Programming with C++/ Session 1/ 2 of 50 Session Objectives n Discuss the following: The Object-Oriented approach Drawbacks of traditional programming Object-Oriented programming n Discuss basic Object-Oriented concepts such as: Objects Classes Properties

Object Oriented Programming with C++/ Session 1/ 3 of 50 Session Objectives (Contd.) Methods Abstraction Inheritance Encapsulation Polymorphism n Compare Classes with Structures n Describe Private and Public sections of Classes

Object Oriented Programming with C++/ Session 1/ 4 of 50 Session Objectives (Contd.) n Define Member functions n Use the Objects and Member functions of a Class Define Objects Access Member Functions Pass and return Objects n Discuss briefly the features of C++ and another OO language (Smalltalk)

Object Oriented Programming with C++/ Session 1/ 5 of 50 The Object-Oriented approach n Can classify living beings as objects just as we classify things we use as objects of different types. n Any application can be defined in terms of entities or objects so that the process replicates human thought process as closely as possible.

Object Oriented Programming with C++/ Session 1/ 6 of 50 Departments as objects in an organisation n People in each department control and operate on that department's data. Sales PersonnelAccounts

Object Oriented Programming with C++/ Session 1/ 7 of 50 Traditional programming: Drawbacks n Unmanageable programs Traditional programs: List of instructions written in a language that tells the computer to perform some actions. When programs become larger they become unmanageable. Functions/procedures/subroutines adopted to make programs more comprehensible. As programs grow larger and more complex, even use of functions can create problems.

Object Oriented Programming with C++/ Session 1/ 8 of 50 Problems in modification of data n Data plays a crucial role in traditional programming techniques. n Adding new data items means modifying all the tasks or functions that access the data. As program size increases it is difficult to locate and to modify all functions which use the data n Restrict access to the data so that only a few critical functions act upon it. n Next to impossible to separate parts of a program from revisions made in another part.

Object Oriented Programming with C++/ Session 1/ 9 of 50 Difficulty in implementation n Focus of traditional programming approach: On implementation details. n Focus of a person’s thinking: In terms of things or entities, their properties and actions. n OOP techniques help to correspond real- world entities and actions to functions and data at the programming level.

Object Oriented Programming with C++/ Session 1/ 10 of 50 Introduction to OOP n OOP allows for analysis and design of an application in terms of entities or objects. n Process replicates the human thought process as closely as possible. n Code and data are merged into a single indivisible thing -- an object. n Close match between objects in the programming sense and objects in the real world.

Object Oriented Programming with C++/ Session 1/ 11 of 50 Data and Functions of an Object Accounts Data: Number of employees Salary statements Bills Vouchers Receipts Petty cash records Banking data Functions: Calculate salary Pay salary Pay bills Tally accounts Transact with banks

Object Oriented Programming with C++/ Session 1/ 12 of 50 Objects n Represent an entity in the real world. n A concept or thing with defined boundaries that is relevant to the problem we are dealing with. n Objects serve two purposes: They help to understand the real world They provide a practical basis for a computer application

Object Oriented Programming with C++/ Session 1/ 13 of 50 Objects (Contd.) n Each object has its own properties or characteristics that describe what it is or does. Vehicles in a traffic-monitoring application Menus The mouse and the keyboard A personnel file A table of marks relating to an examination Time Complex numbers

Object Oriented Programming with C++/ Session 1/ 14 of 50 Different Objects Name: Jack Age: 28 Weight: 65 kgs Model: Ferrari Colour: Red Year: 1995 Actions: Walk Talk Sleep Actions: Start Stop Accelerate

Object Oriented Programming with C++/ Session 1/ 15 of 50 Classes n Grouping of objects that have the same properties, common behaviour and common relationships. n The term class is an abbreviation of “class of objects”. Example, A class of persons, class of animals, class of processes. n Each object is said to be an instance of its class.

Object Oriented Programming with C++/ Session 1/ 16 of 50 Objects and Classes Abstract into Polygon class Properties: Vertices Border colour Fill colour Methods: Draw Erase Move Polygon objects

Object Oriented Programming with C++/ Session 1/ 17 of 50 Property/Attribute n A characteristic required of an object or entity when represented in a class is called a property. n A class is a prototype and not an actual specimen of the entity. n Each instance of the class or object has its own value for each of its properties but it shares the property names or operations with other instances of the class.

Object Oriented Programming with C++/ Session 1/ 18 of 50 Method n An action required of an object or entity when represented in a class is called a method. In a polygon class "draw", "erase" and "move" are examples of the methods that are part of the class. n Object is a "black box" which receives and sends messages.

Object Oriented Programming with C++/ Session 1/ 19 of 50 Method (Contd.) The black box actually contains code (sequences of computer instructions) and data (information which the instructions operates on). Information passed to and retrieved from each department either by inter-departmental memos or verbal instructions are the messages between objects. These messages can be translated to function calls in a program. Sales Accounts What is the salary of Jack? Jack's salary is $2000

Object Oriented Programming with C++/ Session 1/ 20 of 50 Abstraction n Process of examining certain aspects of a problem. n An abstract specification tells us what an object does independent of how it works. n Data Abstraction Process of examining all the available information about an entity to identify information that is relevant to the application.

Object Oriented Programming with C++/ Session 1/ 21 of 50 Data Abstraction n Used to identify properties and methods of each object as relevant to the application at hand. n By grouping objects into classes, we are doing data abstraction of a problem. n Common definitions are stored once per class rather than once per instance of the class. n Methods can be written once for a class, so that all the objects in a class benefit from code reuse.

Object Oriented Programming with C++/ Session 1/ 22 of 50 Inheritance n It is the property that allows the reuse of an existing class to build a new class. A class of animals can be divided into mammals, amphibians, insects, reptiles, etc. n The superclass is the class from which another class inherits its behaviour. n The class that inherits the properties and methods of another class is called the subclass.

Object Oriented Programming with C++/ Session 1/ 23 of 50 Inheritance (Contd.) n Each subclass shares common properties with the class from which it is derived. For example, all vehicles in a class may share similar properties of having wheels and a motor n Subclass may have its own particular characteristics. For example, a bus may have seats for people, while trucks have space for carrying goods.

Object Oriented Programming with C++/ Session 1/ 24 of 50 Class Animals and its subclasses Animals InsectsMammals Reptiles Amphibians

Object Oriented Programming with C++/ Session 1/ 25 of 50 Encapsulation n Providing access to an object only through its messages, while keeping the details private is called information hiding. An equivalent buzzword is encapsulation. A class has many properties and methods. It is not necessary for a user to have access to all of them. All communication to an object is done via messages. Messages define the interface to the object.

Object Oriented Programming with C++/ Session 1/ 26 of 50 Encapsulation (Contd.) n When we properly encapsulate some code we achieve two objectives: We build an impenetrable wall to protect the code from accidental corruption due to the silly little errors that we are all prone to make. We also isolate errors to small sections of code to make them easier to find and fix.

Object Oriented Programming with C++/ Session 1/ 27 of 50 Reusability n Object-oriented development allows: Information to be shared within an application Reuse of designs and code on future projects n Programs are broken down into reusable objects. These objects can then be grouped together in different ways to form new programs. n Inheritance also promotes reuse.

Object Oriented Programming with C++/ Session 1/ 28 of 50 Felines and Subclasses Felines Actions: Make sounds Eat/drink Hunt prey Cats Actions: Mew Drink milk Hunt mice Lions Actions: Roar Eat flesh Hunt big game

Object Oriented Programming with C++/ Session 1/ 29 of 50 Polymorphism n Polymorphism means that the same functions may behave differently on different classes. n The existing object stays the same, and any changes made are only additions to it.

Object Oriented Programming with C++/ Session 1/ 30 of 50 Polymorphism n Polymorphism promotes encapsulation. The Draw method is implemented for different cases but how it is to be implemented need not be visible to the user. Class: Shape Methods: Draw Move Initialise Subclasses

Object Oriented Programming with C++/ Session 1/ 31 of 50 Class Construct n The general syntax of the class construct is: class user_defined name{ private: data_type members; implementation operations; public: data_type members; implementation operations; };

Object Oriented Programming with C++/ Session 1/ 32 of 50 Classes and Structures n A structure contains one or more data items (called members) which are grouped together as a single unit. n Class consists of not only data elements but also functions, which operate on the data elements. n All the variables and the functions declared in a class, whether in the public or private section, are the members of the class. n In a structure all elements are public by default.

Object Oriented Programming with C++/ Session 1/ 33 of 50 Public and Private n Class members can be declared in the public or the private sections of a class. n To prevent data from unrestricted access, the data members of a class are normally declared in the private section. n The public part constitutes the interface to objects of the class. n The data members and functions declared in the public section, can be accessed by any function in the outside world (outside of the class).

Object Oriented Programming with C++/ Session 1/ 34 of 50 Private and Public (Contd.) n Private data is not available outside the class to any program n Also, private data of other classes is hidden and not available within the functions of this class. Class Data or functions Private Public Not accessible from outside class Accessible from outside class

Object Oriented Programming with C++/ Session 1/ 35 of 50 Member functions n In C++, a function declared as a member of a class is called a member function. Member functions are usually put in the public part of the class because they have to be called outside the class either in a program or in a function. n Declaration of a member function must define its return value as well as the list of its arguments. For example, the member function void setdate(int, int, int) has no return value or a void return value, and has three integer arguments.

Object Oriented Programming with C++/ Session 1/ 36 of 50 Member functions (Contd.) n Member functions can be more complex as they can have local variable, parameters etc. n Should not have the same name as a data member.

Object Oriented Programming with C++/ Session 1/ 37 of 50 Using the class begin program class exampleclass{// specify a class private: object_data is an integer; // class data public: member_function1(parameter1, parameter2…) {assign value to object_data} member_function2(){display data} };

Object Oriented Programming with C++/ Session 1/ 38 of 50 Using the class (Contd.) main program{ //define the objects of class exampleclass exampleclass object1,object2; //call member fn to assign value 200 //to object_data object1.member_function1(200); //call member function to display data object1.member_function2(); object2.member_function1(350); object2.member_function2(); }

Object Oriented Programming with C++/ Session 1/ 39 of 50 Defining Objects exampleclass object1,object2; defines two objects, object1 and object2, of class exampleclass. n The definition actually creates objects that can be used by the program. n When we define an object, space is set aside for it in memory.

Object Oriented Programming with C++/ Session 1/ 40 of 50 Calling member functions We communicate with objects by calling their member functions. object1.member_function1(200); object1.member_function2(); n A member function is always called to act on a specific object, not on the class in general. n Associated with a specific object with the dot operator ( the period).

Object Oriented Programming with C++/ Session 1/ 41 of 50 Calling member functions n The general syntax for accessing a member function of a class is class_object.function_member() n The dot operator is called the class member operator.

Object Oriented Programming with C++/ Session 1/ 42 of 50 Two objects with different values exampleclass class specifier object_data 200 object_data 200 object1 object_data 350 object2 Objects of the class exampleclass object_data Specifications for exampleclass objects

Object Oriented Programming with C++/ Session 1/ 43 of 50 Passing Objects n Objects can be passed to a function and returned back just like normal variables. n The compiler creates another object as a formal variable in the called function. It copies all the data members from the actual object. int function1(exampleclass obj1) {return object_data;} Formal argument

Object Oriented Programming with C++/ Session 1/ 44 of 50 Passing Objects (Contd.) n The above function can be invoked using a function call such as, int variable = object1.function1(object2); actual argument n When a member function is called, it is given access to the object of which the function is a member.

Object Oriented Programming with C++/ Session 1/ 45 of 50 Returning Objects n A return statement in a function is considered to initialise a variable of the returned type. exampleclass object3 = object1.function1(object2); n Passing and returning of objects is not very efficient since it involves passing and returning a copy of the data members.

Object Oriented Programming with C++/ Session 1/ 46 of 50 Object-Oriented Languages n C++, Smalltalk, Eiffel, CLOS, Java n Vary in their support of object-oriented concepts. n No single language that matches all styles and meets all needs.

Object Oriented Programming with C++/ Session 1/ 47 of 50 An introduction to C++ n Developed by Bjarne Stroustrup at AT&T Bell Laboratories. n Derived from the C language. n It is compatible with C (it is actually a superset). Most important elements added to C to create C++ are concerned with classes, objects and object-oriented programming.

Object Oriented Programming with C++/ Session 1/ 48 of 50 An introduction to C++ (Contd.) n C++ programs are fast and efficient but it sacrifices some flexibility in order to remain efficient. n C++ uses compile-time binding. Provides high run-time efficiency and small code size, but it trades off some of the power to reuse classes.

Object Oriented Programming with C++/ Session 1/ 49 of 50 Smalltalk n Pure object-oriented language. n While C++ makes some practical compromises to ensure fast execution and small code size, Smalltalk makes none. n Uses run-time binding, which means that nothing about the type of an object need be known before a Smalltalk program is run. n Significantly faster to develop than C++ programs.

Object Oriented Programming with C++/ Session 1/ 50 of 50 Smalltalk n Has a rich class library that can be easily reused via inheritance n Has a dynamic development environment. n It is not explicitly compiled, like C++. n Smalltalk generally takes longer to master than C++. It is syntactically very simple.