C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

Similar documents
C++ Programming: Polymorphism

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Java Object Oriented Design. CSC207 Fall 2014

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

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

Inheritance and Substitution (Budd chapter 8, 10)

Inheritance and Polymorphism

Concepts of Programming Languages

Structured Programming

Chapter 1: Object-Oriented Programming Using C++

Object oriented programming Concepts

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

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

What are the characteristics of Object Oriented programming language?

1. Write two major differences between Object-oriented programming and procedural programming?

CPS 506 Comparative Programming Languages. Programming Language

Programming Languages 2nd edition Tucker and Noonan"

Object Oriented Programming: Based on slides from Skrien Chapter 2

Programming in C# Inheritance and Polymorphism

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Defining Classes and Methods

OVERRIDING. 7/11/2015 Budditha Hettige 82

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

C++ Programming: Inheritance

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

What is Inheritance?

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Data Abstraction. Hwansoo Han

Classes, Objects, and OOP in Java. June 16, 2017

ITI Introduction to Computing II

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

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

Object-Oriented Programming

Object Oriented Programming

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Data Structures (list, dictionary, tuples, sets, strings)

ITI Introduction to Computing II

Chapter3: Introduction to Classes and Objects

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

PROGRAMMING IN C++ COURSE CONTENT

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

Object Fundamentals. Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/6448 Lecture 2 08/30/2007

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Object Orientated Analysis and Design. Benjamin Kenwright

About 1. Chapter 1: Getting started with oop 2. Remarks 2. Examples 2. Introduction 2. OOP Introduction 2. Intoduction 2. OOP Terminology 3.

Inheritance. Transitivity

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

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

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Introduction to Inheritance

Lecture Notes on Programming Languages

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Inheritance and Substitution עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מובסס על הרצאות של אותו קורס שניתן בשנים הקודמות ע"י ד"ר גרא וייס

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

CS-202 Introduction to Object Oriented Programming

Chapter 2: Java OOP I

Object-Oriented Concepts and Design Principles

JAVA MOCK TEST JAVA MOCK TEST II

Object-Oriented Programming in C++/Handout 01 Page 1 of 8

OBJECT ORIENTED PROGRAMMING

1. Software Systems Complexity, OO Paradigm, UML

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance -- Introduction

DATA TYPES. CS 403: Types and Classes DATA TYPES (CONT D)

C++ Quick Guide. Advertisements

Object- Oriented Design with UML and Java Part I: Fundamentals

CS 520 Theory and Practice of Software Engineering Fall 2017

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

An Introduction to Object Orientation

CS 320 Introduction to Software Engineering Spring March 06, 2017

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

ob-ject: to feel distaste for something Webster's Dictionary

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

Programmazione. Prof. Marco Bertini

Object Model. Object Oriented Programming Spring 2015

Procedure and Object- Oriented Abstraction

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

Method Invocation. Zheng-Liang Lu Java Programming 189 / 226

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

C++ Important Questions with Answers

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Unit 1 : Principles of object oriented programming

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

Inheritance, Polymorphism and the Object Memory Model

Object-oriented Programming. Object-oriented Programming

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Lecture 23: Object Lifetime and Garbage Collection

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Classes, subclasses, subtyping

Object-Oriented Technology. Rick Mercer

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

1st Semester MTCE 601A COMPUTER SYSTEM SOFTWARE

Chapter 6 Introduction to Defining Classes

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Transcription:

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming) 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering

Contents Brief introduction to C++ OOP vs. Procedural Programming Fundamental concepts of OOP Information Hiding Encapsulation Inheritance Polymorphism 2

Brief Introduction to C++ C++ is originated from C language C language is a procedural programming language Consists of functions: main / library functions / user-defined functions Data types and structures: built-in / user-defined Prehistory of C++ language 1960 s: Multics written in Assembler, i.e., A (AT&T Bell Labs) 1970: Ken Thompson developed language B Improvement of Multics (UNIX) is written in Assembler and B B is higher level than A, but has no data types and structures 1973: Dennis Ritchie turned B into C language Eventually, UNIX is rewritten in C C++ is based on C, i.e., A à B à C à C++ 3

Brief Introduction to C++ (Cont.) C++ language first developed by Dr. Bjarne Stroustrup (AT&T) in 1980 s Extending and improving standard C language with Classes Thus, first name was C with Classes Hybrid language Supporting both procedural and object-oriented programming C++ has many distinctive programming constructs that are not found in C language Constructs for extended C programming Constructs for object-oriented programming 4

Brief Introduction to C++ (Cont.) Basic constructs of C++ Standard C C++ OO Programming Extended C Programming 5

Modules Programs consist of modules Module is building block of program that can be designed, coded, and tested separately Modules are assembled to form a program In procedural programming Modules are procedures E.g., Functions in C language In OOP Modules are classes E.g., Classes in C++ and Java 6

Procedural Programming Based on top-down design" We can solve a problem with a procedure When a problem that is too complex to solve straightforwardly with one procedure Functionally decompose the problem (procedure) into subproblems (subprocedures) And a subproblem may be further decomposed until it is straightforward enough to be solved in one procedure Pros and cons Intuitive way to solve many complex problems Hard to maintain software Cascading changes: a change in the top-level procedure (e.g., main function) may cascade down to its subprocedures, subprocedures, and so on 7

Procedural Programming (Cont.) Functional decomposition example Main problem: to build an automobile Problem decomposition MainProblem: Build Automobile Build Chassis Build Engine Build DriveTrain Assemble Inspect Procedure decomposition main() buildc() builde() buildd() assemble() inspect() 8

Object-Oriented Programming Object-Oriented Design Definition: the process of planning a system of interacting objects for the purpose of solving a software problem UML (Unified Modeling Language): a standardized generalpurpose modeling language in the field of object-oriented software design Object-oriented design addresses major problems of the topdown design such as cascading changes Focuses on classes designed to model the entities with which a problem deals, not procedures A class is a blueprint for objects ó an object is an instance of class (i.e., class = collection of possible objects) Objects in a class share properties (= features or attributes) A class has operations (= actions or processes) 9

Object-Oriented Programming (Cont.) Object-Oriented Programming (OOP) A programming paradigm based on object-oriented design OOP languages: Smalltalk, Eiffel, Delphi, Object-C, C#, Python, Ruby, C++, Java, C++, Java and Python are most popular object-oriented programming languages nowadays Modules of OOP are classes Properties are called member variables or data members Operations are called methods or function members Development of an OO program To define classes and their relationships (e.g., inheritance) To declare objects and make them interact with each other by invoking their methods 10

Object-Oriented Programming (Cont.) [UML] OO Design class object property operation Human + age : integer + dance() mary:human age = 20 OO Programming class object data member (member variable) method (function member) [C++] class Human { // class definition public: int age; // a data member void dance() { // a method } } Human mary; // object declaration mary.age = 20; 11

OOP vs. Procedural Programming Problem: calculating rectangular area Prodecdural programming to define a calculation function // calcrectarea function definition int calcrectarea(int width, int length) { int area; area = width * length; return area; }... int area = calcrectarea(10, 20); // function invocation area = width * length length width 12

OOP vs. Procedural Programming (Cont.) Problem: calculating rectangular area OOP to define Rectangle class class Rectangle { // Rectangle class definition private: int width; // data member 1 int length; // data member 2 public: setrect(int w, int l) { // method 1 width = w; length = l; } int calcarea() { // method 2 return width * length; } }... Rectangle rec; rec.setrect(10, 20); int area = rec.calcarea(); // Rectangle object declaration // method 1 invocation // method 2 invocation 13

Classes, Objects and Their Relationships Relationships among classes is-a (subclass-of) relationship Eg., Human is-a Mammal, Mammal is-an Animal is-a relationships create an inheritance hierarchy (will be discussed in detail later) Relationship by a property Eg., Human s biological-mother is Human Relationships for objects instance-of (object-of) relationship Eg., mary is an instance-of Human Relationship among objects Eg., john s biological-mother is mary Mammal is-a instance-of is-a Human mary : Class Animal biomother is-a Reptile bio-mother instance-of john : Object 14

Fundamental Concepts of OOP üinformation hiding and Encapsulation Common OOP concepts üinheritance üpolymorphism To learn how to implement OOP concepts in terms of C++ 15

Information Hiding Programmers using a class method need NOT know the details on the implementation You only need to know what the method does Information hiding To design a method so programmers can use the method without knowing the details on how it works To separate what from how when designing methods Also referred to as abstraction E.g., public and private modifiers of C++ Used to control access to a class s data members and methods from the outside of the class public: expose to the outside world private: hide from the outside world 16

Encapsulation An OO design technique to achieve the principle of information hiding Divides a class into Exposed part ( what it does ): interface Hidden part ( how it does ): implementation Real world example: driving a car Interface: we see and use brake, accelerator, and steering wheel Implementation: we do not know mechanical details Example in C++ Interface Public data members and headings of public methods Documentations and manuals Implementation Bodies of public methods Private data members and methods 17

Encapsulation Example (1) Class String class String { // Data members private: char value[]; int len; // represents chars in the string // represents string s length } // Methods public: String substring(int, int) { /* hidden */ } void concatstring(string) { /* hidden */ } void setchar(char, int) { /* hidden */ } bool containchar(char) { /* hidden */ } int length() { /* hidden */ } 18

Encapsulation Example (2) Class IntStack Stack A data structure to store arbitrary objects using LIFO (Last-In First- Out) mechanism Basic operations push(object o): inserts data object o into the Stack if it is not full object pop(): removes and return the most recently inserted object from the Stack if it is no empty class IntStack { // Data members (hidden) private: int stack[10]; // int stack int top = -1; // empty // Exposed methods public: void push(int o) { top++; } int pop() { top--; } } top top 3 1 top 1 empty after push(1) & push(3) after pop() 19

Inheritance Inheritance hierarchy Set of parent-child (i.e., is-a) relationships among classes Parent class a.k.a. super class or base class Child class a.k.a. subclass or derived class A class should be within an inheritance hierarchy (otherwise called stand-alone) * Subclasses inherit properties and methods from the super class Window MenuWindow DialogWindow EditWindow Code reuse with inheritance Inheritance promotes a form of code reuse by specialization (or extension) of a super class into subclasses InformationDialog QuestionDialog * In addition, subclass can define their own properties and methods 20

Types of Inheritance Single inheritance A child class has exactly one parent Every OOP language must support at least single inheritance class SportCar : public Car { // members inherited from Car // members newly defined in SportCar } Car SportCar Multiple inheritance A child class may have multiple parents C++ supports multiple inheritance (c.f., no multiple class inheritance in Java * ) Car Toy class RC_Car : public Car, public Toy { // members inherited from both Car and Toy // members newly defined in RC_Car } RC_Car * Java has no multiple inheritance. But we can simulate with multiple interfaces in Java. 21

Polymorphism Definition of polymorphism In a dictionary The ability to appear in many forms ( 다형성 ) In computer science To allow a program code to work with various types In OOP The ability of an interface to be implemented in multiple ways In C++ Actually refers to runtime binding of a method I.e., in C++, a method is polymorphic if its binding occurs at runtime rather than compile time 22

Polymorphism (Cont.) A powerful tool for programmers A programmer can invoke an object s polymorphic method without exactly knowing which class type or subtype is involved E.g., a Window class hierarchy Imagine base class Window and its 20 or so derived classes Each class has a polymorphic display method with the same signature, which performs some operation specific to the class (window type) With polymorphic methods, programmers can invoke 20 or so different (differently implemented) methods with the same interface according to the situation 23

Example of Polymorphism Various versions of the display method of Window classes, e.g., Class Window has a display method that draws basic window frame on the screen Class MenuWindow has a display method that draws a list of choices in addition to the basic window frame A programmer can use a single display method for any type of Window object E.g., w->display(); If w points to a Window object, its display is invoked Else if w points to a MenuWindow object, its display is invoked Window* w Window ::display() MenuWindow DialogWindow ::display() ::display() v Note that the system determines which version of display to be invoked at runtime (not compile time) InformationDialog ::display() 24