Object Oriented Programming Objects and Classes Dr. Khalil Ullah Department of Software Engineering [email protected] University of Malkand Bridging the gap between inspiration and aspiration 1 Class Object
3 Required Textbook H. M. Deitel and P. J. Deitel , C++ How to Program, Fifth Edition . Upper Saddle River, NJ: Prentice Hall, 2005. ISBN 0-13-185757-6 Textbook web page: http://www.deitel.com/books/cppHTP5/index.html A recommended book (not required): Ann R. Ford and Toby J. Teorey , Practical Debugging in C++ . Upper Saddle River, NJ: Prentice Hall, 2002. ISBN 0-13-065394-2 Object Oriented Programming by Robert Lafore Course Introduction
4 How to Succeed Read the chapter before class Participate In class and in the discussions Learn object-oriented programming by writing a lot of small programs To learn C++ programming skills To apply those skills in problem solving Start the assignments early Invest time and work hard! Course Introduction
5 C++ and Object-Oriented Programming Some brief facts
6 Historical Perspective 1968 – The “software crisis” NATO Software Engineering Conference in Garmisch , Germany Late projects, high costs, code was hard to maintain Ad hoc programming Structured programming Adapted in the late 1960s to produce programs that were easy to understand, develop, test, and modify C++ and Object-Oriented Programming ( Deitel ; Randell ; Wirth)
7 Structured Programming Programs should be composed of pieces that have only one starting point and one terminating point Use only three kinds of “pieces” Sequence Selection control structures e.g., if, if/else, switch Repetition control structures e.g., while, do/while, for C++ and Object-Oriented Programming ( Deitel ; Wirth)
8 Object-Oriented Programming Became widely used in the 1990s Provided the foundation for real progress in creating software that is easy to understand, develop, test, and modify A way of solving a problem by writing a program that models the elements in the problem space as objects For example, a program used to support a video rental business would have objects such as a store, videos, and customers Programs are collections of objects Each object can provide a set of services Objects interact by sending messages telling each other what to do C++ and Object-Oriented Programming ( Deitel ; Eckel )
9 Object-Oriented Programming Some advantages Easy to understand and develop because the objects directly model real-world elements Faster development Easier to maintain Object-oriented software is easier to reuse Object-based programming Class – program element that contains both data and the functions that manipulate it Object –instance of a class (like a “variable”) Object-oriented programming Inheritance Polymorphism C++ and Object-Oriented Programming (Deitel)
10 Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s “C with classes” Standardized by ISO in 1997 Includes the C++ standard library Standard Template Library (STL) Part of the C++ standard library Readymade classes for data structures and algorithms C++ and Object-Oriented Programming ( Deitel ; Josuttis )
11 Introduction to C++ Programming C++ Features in Chapter 1
12 A Simple Example Page 42, Fig. 2.4 C++ features Comments Include directive main() Statements end with semicolons ; cout and << for output std namespace Escape characters, e.g. \n Introduction to C++ Programming ( Deitel )
13 1 // Fig. 2.4: fig02_04.cpp 2 // A first program in C++. 3 #include < iostream > 4 5 // function main begins program execution 6 int main() 7 { 8 std :: cout << "Welcome to C++!\n" ; 9 10 return ; // indicate that program ended successfully 11 12 } // end function main Welcome to C++! Single-line comments. Preprocessor directive to include input/output stream header file <iostream> . Function main appears exactly once in every C++ program.. Function main returns an integer value. Left brace { begins function body. Corresponding right brace } ends function body. Statements end with a semicolon ; . Name cout belongs to namespace std . Stream insertion operator. Keyword return is one of several means to exit function; value indicates program terminated successfully.
14 Another Example Page 43, Fig. 2.5 C++ features Declaring variables Input using cin and >> C++ supports many arithmetic operators +, -, *, /, % Precedence, p. 33: parentheses first; then * , / , and % ; and last are + and – . All from left to right. endl to output a newline and flush output buffer << can be concatenated Introduction to C++ Programming (Deitel)
15 1 // Fig. 2.5: fig02_05.cpp 2 // Addition program. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 int integer1; // first number to be input by user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored 11 12 std::cout << "Enter first integer\n" ; // prompt 13 std::cin >> integer1; // read an integer 14 15 std::cout << "Enter second integer\n" ; // prompt 16 std::cin >> integer2; // read an integer 17 18 sum = integer1 + integer2; // assign result to sum 19 20 std::cout << "Sum is " << sum << std::endl; // print sum 21 22 return ; // indicate that program ended successfully 23 24 } // end function main Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations. Calculations can be performed in output statements: alternative for lines 18 and 20: std::cout << "Sum is " << integer1 + integer2 << std::endl;
16 Equality Operators and Relational Operators Page 53, Fig. 2.13 C++ features if equality operator == (don’t confuse with = ) operator != operator < operator > operator <= operator >= Introduction to C++ Programming (Deitel)
What is Object Oriented Programming? OOP is a programming paradigm or programming style centered around objects rather than functions. This style of programming is not new but is followed since 1970s. Object Oriented Programming is not a programming language or a tool, but it is a framework, a style, a way of doing programming There are many languages which support OOP framework 17
Languages C++ C# JAVA Ruby Python JavaScript And many more 18
Many of the new frameworks that are introduced recently in the market and are used in web development like Object Oriented PHP and Angular are designed using OOPs programming concepts OOPs concepts are very Important for a serious programmer who want to be a developer or a successful software engineer in the market 19
Four main pillars of Object Oriented programming 20
Encapsulation Look first into the procedural programming 21 f() x,y f() x,y,z f() w,x,y,z Program
Encapsulation This problem of interdependency of functions in procedural programing is now solved in OOPs 22 f() x f() x Property Method
Solve the same problem using OOP class employee { private: float baseSalary =10 int overTime =20; float rate=30; public: float wage() { return ( baseSalary + overTime *rate); } }; int main(int argc , char** argv ) { employee e1; cout <<e1.wage(); return 0; } 25 Encapsulation - process of binding data members (variables, properties) and member functions (methods) together. - Providing different levels of scope on how classes, methods, and attributes can be accessed and only allowing access on a need-to-know basis. Generally, oop uses private, public and protected keywords to implement encapsulation on attributes, methods and classes. For example, a private method in a class could only be accessed by the class and a public method could be accessed any other class.
Abstraction Abstraction is the process of showing only essential/necessary features of an entity/object to the outside world and hide the other irrelevant information. 26 Simpler Interface Reduce the impact of change
Inheritance The mechanism which allows you to eliminate the redundant code Consider these HTML elements 27 HTML Element Size Color Click() Focus()
Polymorphism Poly means Many and Morph means form It is a technique which allows you to get rid of long if else switch case statements. For example rendring HTML element 28 HTML Element Size Color Click() Focus() In Procedural Language the code for rendering these elements will be: switch(…){ Case ‘select’: renderSelect (); Case ‘text’: renderTextBox () … … }
In OOP We implement Render method For each object and it Will behave differently For each object; Virtual render(){} In main it can be called like: textbox.render (); select.render (); 29 HTML Element Size Color Click() Focus()
30
We begin our introduction to object orientation, a natural way of thinking about the world and writing computer programs. Our goal here is to help you develop an object-oriented way of thinking and to introduce you to the Unified Modeling Language™ (UML™)a graphical language that allows people who design object-oriented software systems to use an industry-standard notation to represent them. 31
analyze a typical requirements document that describes a software system (the ATM) to be built determine the objects required to implement that system determine the attributes the objects will have determine the behaviors these objects will exhibit specify how the objects interact with one another to meet the system requirements 32
Let's begin with a simple analogy to help you reinforce your understanding of classes and their contents. Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design it and build it. A car typically begins as engineering drawings, similar to the blueprints used to design a house. These drawings include the design for an accelerator pedal that the driver will use to make the car go faster. In a sense, the pedal "hides" the complex mechanisms that actually make the car go faster, just as the brake pedal "hides" the mechanisms that slow the car, the steering wheel "hides" the mechanisms that turn the car and so on. This enables people with little or no knowledge of how cars are engineered to drive a car easily, simply by using the accelerator pedal, the brake pedal, the steering wheel, the transmission shifting mechanism and other such simple and user-friendly "interfaces" to the car's complex internal mechanisms. 33