©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C++ Programming Languages
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Reusing Code Private or Protected inheritance. A cool class for array valarray class deals with numeric values, and it supports operation such as summing.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Programming Languages and Paradigms Object-Oriented Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
OBJECT-ORIENTED PROGRAMMING (OOP) WITH C++ Instructor: Dr. Hany H. Ammar Dept. of Electrical and Computer Engineering, WVU.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance and Composition Reusing the code and functionality Unit - 04.
CS212: Object Oriented Analysis and Design Lecture 22: Generic Class Design.
1 CSC241: Object Oriented Programming Lecture No 17.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
Overview of C++ Polymorphism
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CS 342: C++ Overloading Copyright © 2004 Dept. of Computer Science and Engineering, Washington University Overview of C++ Overloading Overloading occurs.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
A First Book of C++ Chapter 12 Extending Your Classes.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 2 Objects and Classes
Constructors and Destructors
7. Inheritance and Polymorphism
Inheritance, Polymorphism, and Virtual Functions
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Inheritance Basics Programming with Inheritance
CS212: Object Oriented Analysis and Design
Constructors and Destructors
Computer Programming with JAVA
CISC/CMPE320 - Prof. McLeod
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
Introduction to Classes and Objects
Lecture 10 Concepts of Programming Languages
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Presentation transcript:

©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department of Computer Science and Engineering Seoul National University

Outline Object Oriented Programming (OOP)  Basic Terms  Class in OOP  C++ Examples  C++ Constructor and Destructor  Other Stuff (Part of Overview)  Summary  Some Differences between C & C++ Q&A

Basic Terms Object  Collection of Data and Operations on This Data Type  Characteristics Associated with Objects or Data Elements OOP  Programming with Objects User-defined types

Class Means to Define Data Types  Collection of Members: Data Elements and Operations E.g., Music CD Class Title Price SalePrice() Possibly with Access Control  Used for Instantiation of Objects Means to Realize OOP Concepts  Abstraction  Encapsulation  Inheritance  Polymorphism

Why Class or Why OOP Abstraction  To Extract the Essential Characteristics Encapsulation  To Hide Internal, Detailed Information Inheritance  To Reuse Existing Elements Polymorphism  To Select Class-Specific Implementations at Runtime

C++ Example: Abstraction Online Retailer Such as Amazon.Com  Item: Type, Title, Maker, Price, Availability, etc. class Item { // Class definition public: String title; // String is a class defined earlier double price; // double is a predefined data type double SalePrice() { return (price*0.9);} }; Item A; // Class object definition // OKAY: A.title, A.price, and A.SalePrice()

C++ Example: Encapsulation Online Retailer Example Cont’d class Item { // Class definition public: String title; double price; double SalePrice() { return (price*0.9);} bool isAvailable() { return (inStockQuantity > 0); } private: int inStockQuantity; }; Item A; // Class object definition // NOT OKAY: A.inStockQuantity // OKAY: A.isAvailable()

C++ Example: Inheritance Online Retailer Example Cont’d class MusicCDItem : public Item { public: String singer_name; }; MusicCDItem B; // Class object definition // OKAY: B.singer_name, B.title, B.price, B.SalePrice(), // and B.isAvailable() // NOT OKAY: B.inStockQuantity  Friendship class Item { friend class MusicCDItem; … Derivation -private: public & protected -> private -protected: public & protected -> protected

C++ Example: Polymorphism Online Retailer Example Cont’d class Item { // Class definition public: String title; // String is a class defined earlier double price; // double is a predefined data type double SalePrice() { return (price*0.9);} int isAvailable() { return (inStockQuantity > 0 ? 1 : 0); } virtual void specificInfo() { cout << "no Info: a base-class object" << endl; } private: int inStockQuantity; }; virtual void specificInfo() = 0; // pure virtual function: // making this class be used // only as a base class

C++ Example: Polymorphism Cont’d Online Retailer Example Cont’d class MusicCDItem : public Item { public: String singer_name; void specificInfo() { cout << "singer name = " << singer_name << " : a derived-class object" << endl; } }; void printSpecificInfo(Item *P) { P-> specificInfo(); } Item A; // Class object definition MusicCDItem B; // Class object definition printSpecificInfo(&A); // Call Item::specificInfo() printSpecificInfo(&B); // Call MusicCDItem::specificInfo() // - Another derived class (e.g., MovieDVDItem) with specificInfo()

C++ Constructor and Destructor Example #include class String { public: String(const char *s) { len = strlen(s); str = new char[len + 1]; assert(str != 0); strcpy(str,s); } ~String() { delete [] str; } private: int len; char *str; }; String name0 = String("Andrew"); // Definition String name1("Karl"); String *name_ptr = new String("Thomas"); delete name_ptr; // Explicit destruction String(int ln) { … } // Function overloading // String buf = 1024; String() { … } // Default constructor // String st(); -> Error

Other Stuff Overloading (w/ Distinguished Argument Lists)  Function Operator String& String::operator+=(const String &s) { len += s.len; char *p = new char[len+1]; assert(p != 0); strcpy(p, str); strcat(p, s.str); delete str; str=p; return *this; } String s1(“Thank “); s1 += “you!”; Reference type E.g., int *&ipr; Address of the invoking class object call String(const char *s) first

Other Stuff Cont’d Reference Type  Reference Object to Be Initialized Unable to alias another object once initialized Class Template  Automatic Generation of Class Instances Bound to a Particular Type int &refVal = val; // int &const refVal = val; const int &cir = 1024; OK: uc, d1+d2 // unsigned char uc; // double d1, d2; template class Stack { … Stack s; // typedef int SDT

Other Stuff Cont’d Multiple Inheritance  Child Class as a Composite of Its Multiple Base Classes Qualification to resolve ambiguity Class C : public A, public B { … } e.g., A::a or B::a in C::func() e.g., C::func() dominates over A::func() Dominance in the Inheritance Chain  Most Derived Instance Dominating

Summary Class  To Define New Types in OOP  To Realize OOP Concepts: Abstraction Encapsulation Inheritance Polymorphism cf, C++ Primer by Stanley B. Lippman, Addison Wesley

Some Differences between C & C++ Type Checking (Regarding Function Declarations)  Meaning of No Argument ANSI C: zero or more arguments of any data type C++: no argument  Effect of No Declaration ANSI C: permitted C++: error C++ Support for Default Arguments void new_line(int n =1) { while (n-- > 0) putchar(‘ \ n’); } Dynamic Memory Allocation new_line(2); new_line();