Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming

Similar presentations


Presentation on theme: "Object-Oriented Programming"— Presentation transcript:

1 Object-Oriented Programming
B.Ramamurthy 1/12/2019 Ramamurthy

2 Topics to be covered Why OO? OO concepts sampler Notion of class
From problem to OO solution Access Specifiers Components of OO Design Files in Development Summary 1/12/2019 Ramamurthy

3 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) 1/12/2019 Ramamurthy

4 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. 1/12/2019 Ramamurthy

5 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 1/12/2019 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. 1/12/2019 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. 1/12/2019 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 1/12/2019 Ramamurthy

9 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;}; 1/12/2019 Ramamurthy

10 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. 1/12/2019 Ramamurthy

11 Inheritance Suppose that you want define a class for all Ford cars.
All Ford cars do have the inherent characteristics of a general car described in the class car. They also have special characteristics of their own. So how to reuse general class car? Inherit the general characteristics. class FordCar : Car // meaning it inherits from class car { //special characteristics…}; 1/12/2019 Ramamurthy

12 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. }; 1/12/2019 Ramamurthy

13 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. 1/12/2019 Ramamurthy

14 Simple definition of class Counter
public: Counter(); //constructor void setCount(int); void getCount(); void increment(); void decrement(); private: int count; }; 1/12/2019 Ramamurthy

15 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. 1/12/2019 Ramamurthy

16 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;} 1/12/2019 Ramamurthy

17 Member Function implementations
syntax: type class_name::fname (list of formal parameter types and names) { function body } 1/12/2019 Ramamurthy

18 Object Instantiation and Invoking Member Functions
Counter score; score.setCount(45); score.increment(); score.decrement(); int x = score.getCount(); 1/12/2019 Ramamurthy

19 Files Involved #include “Counter.h” #include “Counter.h” Driver.cc
For testing purposes Counter.cpp Counter.h #include “Counter.h” Application.cc 1/12/2019 Ramamurthy

20 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. 1/12/2019 Ramamurthy


Download ppt "Object-Oriented Programming"

Similar presentations


Ads by Google