There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism.

Similar documents
Chapter 2 Data Design and Implementation

Chapter 1: Principles of Programming and Software Engineering

Program Correctness and Efficiency. Chapter 2

Part 7 - Object Oriented Concepts Classes, Objects, Properties and Methods

Problem Solving with C++

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CSE 331 Midterm Exam 2/13/12

Object Oriented Programming with c++ Question Bank

For example, when we first started Java programming we described the HelloWorld class:

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Data Structures and Other Objects Using C++

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

C++ (Non for C Programmer) (BT307) 40 Hours

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CMSC 132: Object-Oriented Programming II

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

CSC Advanced Object Oriented Programming, Spring Specification

CGS 2405 Advanced Programming with C++ Course Justification

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

C++ Important Questions with Answers

Assertions, pre/postconditions

Inheritance and Polymorphism

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Basics of Object Oriented Programming. Visit for more.

Lecture Notes on Programming Languages

What are the characteristics of Object Oriented programming language?

STRUCTURING OF PROGRAM

Data Structures. Subodh Kumar. Dept of Computer Sc. & Engg. IIT Delhi

EXAMINATIONS 2009 MID-TERM TEST. COMP 202 / SWEN 202 Formal Methods of Computer Science / Formal Foundations of Software Engineering WITH ANSWERS

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

Absolute C++ Walter Savitch

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Assertions. Assertions - Example

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Reasoning about programs

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

Last time. Reasoning about programs. Coming up. Project Final Presentations. This Thursday, Nov 30: 4 th in-class exercise

What does it mean by information hiding? What are the advantages of it? {5 Marks}

JAVA CONCEPTS Early Objects

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Use the scantron sheet to enter the answer to questions (pages 1-6)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Chapter 1: Programming Principles

Software Design and Analysis for Engineers

15CS45 : OBJECT ORIENTED CONCEPTS

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

COP 3330 Final Exam Review

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Chapter 10 Classes Continued. Fundamentals of Java

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Object Oriented Programming: Based on slides from Skrien Chapter 2

What is Inheritance?

Inheritance -- Introduction

Advanced Programming Using Visual Basic 2008

Largest Online Community of VU Students

EXCEPTION HANDLING. Summer 2018

Main concepts to be covered. Testing and Debugging. Code snippet of the day. Results. Testing Debugging Test automation Writing for maintainability

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CMSC 132: Object-Oriented Programming II

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

n Specifying what each method does q Specify it in a comment before method's header n Precondition q Caller obligation n Postcondition

Java Object Oriented Design. CSC207 Fall 2014

Chapter 5 Object-Oriented Programming

CS 162, Lecture 25: Exam II Review. 30 May 2018

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lecture Chapter 2 Software Development

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Object-Oriented Programming (OOP) Fundamental Principles of OOP

UC Santa Barbara. CS189A - Capstone. Christopher Kruegel Department of Computer Science UC Santa Barbara

CLASSES AND OBJECTS IN JAVA

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Object Oriented Programming. Solved MCQs - Part 2

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Chapter 4 Defining Classes I

14. Exception Handling

Binary Search Trees. Contents. Steven J. Zeil. July 11, Definition: Binary Search Trees The Binary Search Tree ADT...

C++_ MARKS 40 MIN

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Trees. Chapter 6. strings. 3 Both position and Enumerator are similar in concept to C++ iterators, although the details are quite different.

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Overview of the lecture. Some key issues in programming Java programming. CMPT Data and Program Abstraction. Some of the key programming issues

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

Programming in C# Inheritance and Polymorphism

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

EL2310 Scientific Programming

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

CSE 331 Final Exam 3/12/12

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Transcription:

More on Object Oriented Programming Concepts Functional, structured programming often results in programs that describe a hierarchy of tasks to be performed. Object oriented design, however, results in a hierarchy of objects. Concepts: An object is a class object or a class instance. A method is public member function An instance variable is a private data member Sending a message means calling a public member function There are three basic elements in object oriented programming: encapsulation, inheritance and polymorphism. Inheritance: This is a mechanism whereby a hierarchy of classes is constructed such that each descendant class inherits the properties (data and operations) of its ancestor class. A child class automatically inherits all properties of the parent class and can in addition have more specialized properties. The inheritance relationship can be viewed as an is-a relationship with objects becoming more specialized as we go lower in the hierarchy. Object-oriented languages provide a way for creating inheritance relationships among classes. You can take an existing class (called base class) and create a new class from it (called derived class). The derived

class inherits all the properties of its base class, in particular the data and operations defined for the base class. Polymorphism: Polymorphism means various forms. In OOP it is the ability to determine either statically or dynamically which of the several methods with the same name (within the class hierarchy) should be invoked. Overloading means giving the same name to several different functions. The compiler selects the correct method based on the parameter types, values, etc The time at which a function name is associated with the code is called binding time. Binding time can be static if the binding occurs at compile-time, or dynamic if the decision is postponed until run time. C++ constructs for OOP In an object-oriented design of a program, classes typically exhibit one of the following relationships: They are independent of one another, they are related by composition, or they are related by inheritance. Composition: Or containment is a relationship in which a class contains a data member that is an object of another class type. For example, we can define a class PersonType that has a data member birthdate of class DateType #include <string> class PersonType { public:

}; void Initialize(string, DateType); string GetName() const; DateType GetBirthdate() const; private: string name; DateType birthdate; Deriving One Class from Another #include <string> class MoneyType { public: void Initialize(long, long); long GetDollars() const; long GetCents() const; private: long dollars; long cents; }; class ExtMoneyType: public MoneyType { public: string GetCurrency() const; void Initialize(long, long, string); private: string currency; }; ExtMoneyType extmoney; void ExtMoneyType::Initialize (long newdollars, long newcents, string newcurrency)

{ } currency=newcurrency; MoneyType::Initialize(newDollars, newcents); string ExtMoneyType::GetCurrency() const { return currency; } Note: the scope resolution operator (::) appears between the type MoneyType and the member function Initialize in the definition of the member function, because two member functions named Initialize now exist. And we must indicate the class in which one we mean by putting the class type. If we fail to do so explicitly, the computer assumes that we mean the most recently defined. In C++ rule for passing parameters is that the actual parameter and its corresponding formal parameter must be of an identical datatype. With inheritance, however, C++ relaxes the rule, that is the type of the actual parameter must be an object of a derived class of the formal parameter. examples: money. Initialize(20, 66); extmoney.initialize(20, 66, francs ); Constructs for program verification: General Methods for verifying software correctness: The first characteristic of a good program is that it accomplishes its intended function. We can only know that by testing the program.

Testing: The process of executing a program with data sets designed to discover errors. If the program execution does not yield the expected results, we need to debug the program Debugging: the process of removing known errors. When debugging is completed, the software is put into use. Usually it is installed on one or more customer sites so that it can be tested in a real environment with real data. This is the acceptance test phase. After passing the test, the program becomes operational. The maintenance phase of the software include correction of errors, addition of new capabilities. Testing must occur after any program verification, this is called regression testing. Testing is useful for in revealing the presence of bugs in the program, however it does not prove that a program has none. Origin of bugs: Specifications and Design Errors Specification are what the customer wants for your software to perform. Most studies show that it costs 100 times more to correct an error discovered after the software delivery than it does if the problem is discovered early in the software life cycle. Compile time errors: Run time errors: Errors that occur during execution of a program and are usually more difficult to detect than syntax errors.

Designing Correct Programs: The verification of program correctness, independent of data testing, is an important area of theoretical computer science research. The research seeks to establish a method for proving programs that is analogous to the method for proving theorems in geometry. Assertion: An assertion is a logical proposition that can be true or false. We can make assertions about the state of a program For example: sum= part +1 ; we might assert the following the value of sum is greater than the value of part The general concept behind program verification is that we can make assertions about what the program is intended to do, based on its specifications and then prove through a logical argument rather than through the execution of the program that the design or implementation satisfies the assertions. The process is broken down into 2 steps: 1. Correctly assert the intended function of the part of the program to be verified 2. Prove that the actual design or implementation does what is asserted. Preconditions and Postconditions: Imagine you want to design a module or a logical chunk of the program to perform some specific

operation. To ensure that this module (usually implemented as a function) fits into the program as a whole, we must clarify what happens at its boundaries - that is what must be true when we enter the module and what must be true when we exit. We call the assertions that must be true on entry into the function preconditions. Preconditions: Assertions that must be true on entry into an operation or function for the postcondition to be guaranteed. In addition, we must also know what conditions are true when the operation is complete. The postconditions are assertions that describe the results of the operation Postconditions: Assertions that state what results are expected at the exit of an operation or function, assuming that the preconditions are true. Example: Let consider the preconditions and postconditions for an operation that removes the last element of a list and returns its value as an output. The specification for GetLast is: GetLast( ListType list, ValueType lastvalue) Function: Remove the last element in the list and return its value in lastvalue. Precondition: the list is not empty Postcondition: lastvalue is the value of the last element in the list, the last element has been removed, and the list length has been decremented Exceptions:

In the design phase, we should plan how to handle exceptions in our program. Exceptions are exceptional situations. When these situations occur, the flow of control of the program must be altered, usually resulting in the premature end to program execution. Some languages (such as C++ and java) provide built-in mechanisms to manage exceptions. All exceptions mechanisms have three parts: -Defining the exception (try) - Generating (raising) the exception (throw) - Handling the exception (catch) Example: A bounded counter is a software device that maintains and updates a counter value. When the counter increments past a certain limiting value, set when the counter is first created, it wraps around back to zero. Suppose we have a counter that is created with a limiting value of 17. A series of increments might take us through the values 14, 15, 16, 17 and then back to 0. Define an ADT that will implement a bounded counter. Give methods for increment, decrement and accessing the current counter value.