C++ support for Object-Oriented Programming

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
CS-240 Operator Overloading Dick Steflik. Operator Overloading What is it? –assigning a new meaning to a specific operator when used in the context of.
C++ data types. Structs vs. Classes C++ Classes.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 3.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Copyright  Hannu Laine C++-programming Part 4: Operator overloading.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Programming Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Link-Based Implementations
Memory Management.
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Learning Objectives Pointers as dada members
Chapter 13: Overloading and Templates
Introduction to Programming
Abstract Data Types Programmer-created data types that specify
C++ Object-Oriented Programming
Lesson 14 Copy and Assignment
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
The Bag and Sequence Classes with Linked Lists
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Automatics, Copy Constructor, and Assignment Operator
Basic C++ What’s a declaration? What’s a definition?
Automatics, Copy Constructor, and Assignment Operator
Foundational Data Structures
Copy Constructor CSCE 121 J. Michael Moore.
Copy Assignment CSCE 121 J. Michael Moore.
CISC/CMPE320 - Prof. McLeod
Essential Class Operations
COP 3330 Object-oriented Programming in C++
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Essential Class Operations
Copy Assignment CSCE 121.
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
C++ data types.
Destructors, Copy Constructors & Copy Assignment Operators
SPL – PS3 C++ Classes.
Presentation transcript:

C++ support for Object-Oriented Programming 程式設計 潘仁義 CCU COMM

“Donut” Model of Standard Type int

“Donut” Model of an Abstract Data Type

Abstract Data Type supporting C does not allow to define operators Complex + Complex ? C permits only one meaning for a name abs( ), labs( ), llabs( ), fabs( ) ? C++ supports Class definition to group data structure and operations of an abstract data type supports operator overloading to include definitions of arithmetic and input/output operators supports function overloading Ex. Using only one “abs( )”

Comparison of Models of Standard Type int and Abstract Data Type Complex

Header File for Class Complex Complex comp1; Complex comp2( 5.1 ); Complex comp3( 9.1, -7.2);

Implementation File for Class Complex “this” Member functions can access private data member Member operators have direct access to the members of their first operand If member function/operator does not change data members Prototype ends in “const” Why?

Implementation File for Class Complex (cont’d) 有個bug, 猜猜在哪

Driver Function to Test Class Complex

Step-by-Step Evaluation of Multiple << Operations ostream& operator<< (ostream& os, Complex c) { … os << fixed << showpoint << setprecision(2); … return os; }

Q & A Shadow copy vs. Deep copy A shallow copy of an object copies all of the member field values The default copy constructor and assignment operator make shallow copies May not be what you want for fields that point to dynamically allocated memory Deep copies need ... Destructor Copy constructor Overloaded assignment operator