Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming
B.Ramamurthy 11/28/2018 Ramamurthy
2
Object-Oriented Principles
OOP Inheritance -- Hierarchy -- Reusability -- Extensibility -- Expressive power -- Reflects many real-world problems Polymorphism -- Many forms of same function -- Virtual functions -- Abstract Base Classes Encapsulation (class) -- Information Hiding -- Separation of Interface and Implementation -- Standardization -- Access Control mechanisms (private /public) 11/28/2018 Ramamurthy
3
Why Object-oriented paradigm?
Separation of interface and implementation: Any implementation change should not affect user interface. To ease design process: Separation of Design definition, Implementation, Usage through data encapsulation. Software reuse : To allow (1) sharing, (2) upgrades (3) modification using inheritance and polymorphism. Parameterized classes in templates. 11/28/2018 Ramamurthy
4
OO Concepts sampler Consider the automobile (car) you drive.
List the behaviors (functions) of this automobile. The ones you have control over define the user-interface of the automobile. The internal behavior defines the private functionality. As a user you have no control over the implementation of these. List the attributes (parts) of the automobile. You have access to some. Example: steering wheel, brakes, head-lights, wipers…. You do not have access to some others: Odometer 11/28/2018 Ramamurthy
5
Encapsulation : class car
public: //interface // behavior or functions startEngine; accelerate; brake; park; shiftGear; // attributes or parts color; seatBelt; private: //under the hood displaySpeed(); Odometer; Engine;}; 11/28/2018 Ramamurthy
6
The notion of class Things discussed above are in general applicable to any car or Shall we say to a class of cars? Some functionality and parts are to be hidden. An interface containing a set of functions and components have to be presented to the user of the car. 11/28/2018 Ramamurthy
7
From problem to OO solution
Identify the objects (“nouns”) in the problem. Define and implement classes to represent the objects. Class name Data members : public, private, (protected) Function members Service functions (usually public) Utility functions (usually private) Predicate functions. Example: warnings, status indicators, error indicators, etc. Write a driver that carries out the interaction among the objects. 11/28/2018 Ramamurthy
8
Member functions Member functions are based on “use cases”
Ask the question: What is an object of this class used for? And enumerate its uses. This list will indicate to the designed one or more functions that need to be implemented to fulfill the “use” case 11/28/2018 Ramamurthy
9
Object instantiation If you are interested in a specific car: Then an instance of the class can be created: Example : car mycar, rentedCar; mycar and rentedCar are objects of class car. It is also possible to set a specific characteristic such as color, if it is public. Example: mycar.color = green; If an attribute is private then an access function is needed to set its values. 11/28/2018 Ramamurthy
10
Basic syntax : class definition
class class_name { public: list of class attributes (variables, types, constants, and so on) that may be accessed by name from outside the class. list of prototypes for each member function that may be accessed by name from outside the class. private: list of class attributes (variables, types, constants, and so on) that are intended to be hidden for reference from outside the class. list of prototypes for each member function intended to be hidden from outside of the class. }; 11/28/2018 Ramamurthy
11
Access specifiers- private and public
The scope of a specifier begin with the keyword private or public and ends when at the end of the class or at another access specifier. There can be more than one private or public sections. But it is a good programming style to pool all the public items into a single public section and all private into a single private section. By default all items in a class are private! So don’t forget to “public”ize. The items in a private section of a class are available only to the member function of the class. 11/28/2018 Ramamurthy
12
Simple definition of class Counter
public: Counter(); //constructor void setCount( int val); int getCount(); void increment ( int n); void decrement ( int n); private: int count; }; 11/28/2018 Ramamurthy
13
Class constructor(s) A class constructor is used to create (instantiate) an object of the class. The constructor is automatically executed each time an object (of class type) is declared. A class constructor has the same name as the class. Ensures all objects start in a consistent state and contains the initialization code. A constructor cannot specify a return type or explicitly return a value. A class typically has more than one constructor: default constructor, copy constructor and (converter) or initializing constructor. 11/28/2018 Ramamurthy
14
constructor implementation
:: operator is called the class resolution operator. The class name before :: defines the class context in which a function is defined. Counter::Counter()// default constructor { count = 0; } Counter::Counter(int value) // initializing constructor { count = value;} 11/28/2018 Ramamurthy
15
Member Function implementations
syntax: type class_name::fname (list of formal parameter types and names) { function body } 11/28/2018 Ramamurthy
16
Object Instantiation and Invoking Member Functions
Counter score; score.setCount(45); score.increment(); score.decrement(); int x = score.getCount(); 11/28/2018 Ramamurthy
17
Files Involved #include “Counter.h” Counter.cpp Counter.h
Application.cc 11/28/2018 Ramamurthy
18
Summary Class definition, implementation, usage were illustrated
Three principles of OO design: encapsulation, inheritance, polymorphism. We looked at examples for the first two. Problem statement to Object-oriented design. Notion of class and object instantiation were illustrated. Access control specifiers were introduced. 11/28/2018 Ramamurthy
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.