Ch 2 ADTs and C++ Classes

Similar documents
Lecture 3 ADT and C++ Classes (II)

Lecture 7. Log into Linux New documents posted to course webpage

Abstract Data Types (ADT) and C++ Classes

pointers & references

ADTs & Classes. An introduction

SFU CMPT Topic: Classes

Due Date: See Blackboard

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Object-Oriented Programming

C++ Constructor Insanity

CS11 Introduction to C++ Fall Lecture 1

OBJECT ORIENTED PROGRAMMING USING C++

CS 247: Software Engineering Principles. ADT Design

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CS11 Intro C++ Spring 2018 Lecture 1

Scope. Scope is such an important thing that we ll review what we know about scope now:

III. Classes (Chap. 3)

CSI33 Data Structures

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

(3) Some memory that holds a value of a given type. (8) The basic unit of addressing in most computers.

Implementing an ADT with a Class

Chapter 18 - C++ Operator Overloading

The Class Construct Part 2

Review: C++ Basic Concepts. Dr. Yingwu Zhu

PIC 10A. Lecture 15: User Defined Classes

CSCE 110 PROGRAMMING FUNDAMENTALS

Overloading Operators in C++

EECE.3220: Data Structures Spring 2017

Introduction to C++ Systems Programming

Object Oriented Design

Cpt S 122 Data Structures. Introduction to C++ Part II

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Circle all of the following which would make sense as the function prototype.

Inheritance and Polymorphism

EL2310 Scientific Programming

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

12/2/2009. The plan. References. References vs. pointers. Reference parameters. const and references. HW7 is out; new PM due date Finish last lecture

Object Oriented Design

Part of the Picture: Simulation

IS 0020 Program Design and Software Tools

Object Oriented Design

CSCI 123 Introduction to Programming Concepts in C++

Object-Oriented Programming

Chapter 15 - C++ As A "Better C"

Operator Overloading

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

CS 247: Software Engineering Principles. Modules

Module 7 b. -Namespaces -Exceptions handling

2 ADT Programming User-defined abstract data types

CSE 214 Computer Science II Java Classes and Information Hiding

Abstraction in Software Development

Introduction to Classes

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

Introduction to C++ (Extensions to C)

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Ch 8. Operator Overloading, Friends, and References

Operator Overloading in C++ Systems Programming

Lab 2: ADT Design & Implementation

Set Implementation Version 1

C++ STREAMS; INHERITANCE AS

A <Basic> C++ Course

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Introduction Of Classes ( OOPS )

A <Basic> C++ Course

Classes: A Deeper Look

Tutorial letter 202/2/2018

Due Date: See Blackboard

Fast Introduction to Object Oriented Programming and C++

Interview Questions of C++

Module Operator Overloading and Type Conversion. Table of Contents

Object-Oriented Design (OOD) and C++

Understanding main() function Input/Output Streams

Recharge (int, int, int); //constructor declared void disply();

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

Friends and Overloaded Operators

Chapter 9 Classes : A Deeper Look, Part 1

Introduction to Programming session 24

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

CIS 190: C/C++ Programming. Classes in C++

Introduction to the C programming language

Friends and Overloaded Operators

Intermediate Programming, Spring 2017*

Functions, Arrays & Structs

Operator overloading

Shahram Rahatlou. Computing Methods in Physics. Overloading Operators friend functions static data and methods

Introducing C++ to Java Programmers

W8.2 Operator Overloading

Unit IV Contents. ECS-039 Object Oriented Systems & C++

Program construction in C++ for Scientific Computing

Introduction to C++ Introduction to C++ 1

Computing and Statistical Data Analysis Lecture 3

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CMSC 202 Midterm Exam 1 Fall 2015

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Introduction to the C programming language

CS3157: Advanced Programming. Outline

Chapter 19 C++ Inheritance

Lecture 8. Xiaoguang Wang. February 13th, 2014 STAT 598W. (STAT 598W) Lecture 8 1 / 47

Transcription:

Ch 2 ADTs and C++ Classes Object Oriented Programming & Design Constructing Objects Hiding the Implementation Objects as Arguments and Return Values Operator Overloading 1

Object-Oriented Programming & Design 2

3 Classes & Objects

Some OOP Features of C++ Accessor functions can be declared to be const. Methods (member functions) may be represented by prototypes in the class definition. Their definition may include the inline request. Methods defined within a class definition request inline implicitly. External method definition requires the scope resolution operator. 4

Constructor Properties and Rules Constructors are methods whose names are the same as the class. They may not have a return type. Not even void. The default constructor (one with no parameters) is invoked with no argument list (no () ). If no constructors are written, the default constructor is provided automatically. Otherwise not. The default constructor should be written if other constructors are defined. 5

Class Definition class throttle { public: throttle( ); throttle(int size); void shut_off( ) { position = 0; void shift(int amount); double flow( ) const { return position / double(top_position); bool is_on( ) const { return (position > 0); private: int top_position; int position; ; Class Implementation throttle::throttle( ) { top_position = 1; position = 0; throttle::throttle(int size) { assert(size > 0); top_position = size; position = 0; void throttle::shift(int amount) { position += amount; if (position < 0) position = 0; else if (position > top_position) position = top_position; 6

Client Code #include <iostream> #include <cstdlib> #include "throttle.h" using namespace std; const int DEMO_SIZE = 5; int main( ) { throttle sample(demo_size); int user_input; cout << "I have a throttle with " << DEMO_SIZE << " positions." << endl; cout << "Where would you like to set the throttle?" << endl; cout << "Please type a number from 0 to " << DEMO_SIZE << ": "; cin >> user_input; sample.shift(user_input); while (sample.is_on( )) { cout << "The flow is now " << sample.flow( ) << endl; sample.shift( -1); cout << "The flow is now off" << endl; return EXIT_SUCCESS; 7

Using <ctime> 8

Timing Processes #include <ctime> #include <iostream> #include <cassert> using namespace std; long fib(long n); void showtime(clock_t t); int main() { clock_t start, stop; long f, n; do { cout << "Number: "; cin >> n; if (n > 0) { start = clock(); f = fib(n); stop = clock(); cout << "The " << n << "th Fibbonocci Number is " << f << endl << endl; showtime(stop - start); while (n > 0); 9

Timing Processes, cont d long fib(long n) { assert(n > 0); if (n > 2) return fib(n - 1) + fib(n - 2); else return 1; void showtime(clock_t t) { cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(5); cout << " time: " << (double)t/clocks_per_sec << " sec" << endl << " ticks: " << (long)t << endl << "ticks/sec: " << (long)clocks_per_sec << endl << endl; 10

Hiding the Implementation Private Protected Friend Public Designer Client 11

The Interface File The class definition should be provided in a separated header file. Begin with the class documentation. Include a statement documenting the class value semantics. Wrap the definition in a preprocessor conditional to prevent duplicate inclusion a namespace formed as edu_santarosa_staff_gbrown_myclass Define only those methods intended to be inlined. Avoid using statements in the interface file. 12

Value Semantics The value semantics of a class determines how values are copied from one object to another. The assignment operator may be the automatic (default) one or overloaded. Likewise for the copy constructor. It is one which has a single parameter whose type is the same as the class. It is invoked automatically when passing and returning objects, cloning during instantiation as in throttle y(x); or throttle y = x; >> 13

The Implementation File A class method definitions should be provided in a separate implementation file. The file is compiled and its corresponding object file is distributed along with the interface file. Begin with an appropriate comment, any necessary preprocessor includes and appropriate using statements. Wrap the method definitions in the same namespace that was used in the interface file. 14

Using a Namespace To use all identifiers from a namespace, put a using statement after all include directives: using <namespacename>; To use a specific identifier use scope resolution: using <namespacename>::<identifier>; With no using statement, fully qualify the identifer: <namespacename>::<identifer> myvar; >> 15

Development Flow Editor Object Object Object Object Librarian Library Interface Interface Interface Interface Standard Headers Compiler Compiler Object Object Implementation Client Linker Executable 16

Objects as Arguments and Return Values 17

Arguments & Parameters An argument is a value or reference passed to a function when it is invoked. A value parameter is a special local variable that is automatically initialized to the value of the argument passed to the function. A refernce parameter is a special local identifier that serves as an alias to the variable passed to the function. It may be modified with const. Except for upcasting, the types must match exactly. 18

Default Arguments The argument s default(s) is/are specified only once, in the prototype and not in the definition. If all the arguments don t have defaults, those with defaults must be the rightmost ones. In a function call, arguments with default values may be omitted from the right end of the argument list. 19

point.h #ifndef MAIN_SAVITCH_POINT_H #define MAIN_SAVITCH_POINT_H namespace main_savitch_2a { class point { public: point(double initial_x = 0.0, double initial_y = 0.0); void shift(double x_amount, double y_amount); void rotate90( ); double get_x( ) const { return x; double get_y( ) const { return y; private: double x; double y; ; // end namespace main_savitch_2a #endif 20

point.cpp #include "point.h" namespace main_savitch_2a { point::point(double initial_x, double initial_y) { x = initial_x; y = initial_y; void point::shift(double x_amount, double y_amount) { x += x_amount; y += y_amount; void point::rotate90( ) { double new_x; double new_y; new_x = y; new_y = -x; x = new_x; y = new_y; // end namespace main_savitch_2a 21

Operator Overloading 22

Rules At least one parameter must be a class object A friend function must be used if the left parameter is not an object of the same class. If the function is a class member its number of parameters is one less than the operator s arity. The missing parameter is the current object. New operators cannot be created. The arity, precedence and associativity cannot be changed. = [] ++ and -- must be overloaded in special ways. Some operators cannot be overloaded: membership, scope resolution and decision. 23

newpoint.h #ifndef MAIN_SAVITCH_NEWPOINT_H #define MAIN_SAVITCH_NEWPOINT_H #include <iostream> namespace main_savitch_2b { class point { public: point(double initial_x = 0.0, double initial_y = 0.0); void shift(double x_amount, double y_amount); void rotate90( ); double get_x( ) const { return x; double get_y( ) const { return y; friend std::istream& operator >>(std::istream& ins, point& target); private: double x, y; ; double distance(const point& p1, const point& p2); point middle(const point& p1, const point& p2); point operator +(const point& p1, const point& p2); bool operator ==(const point& p1, const point& p2); bool operator!=(const point& p1, const point& p2); std::ostream& operator <<(std::ostream & outs, const point& source); #endif 24

newpoint.cpp... #include <iostream> #include <math.h> #include "newpoint.h" using namespace std; namespace main_savitch_2b { point::point(double initial_x, double initial_y) { x = initial_x; y = initial_y; void point::shift(double x_amount, double y_amount) { x += x_amount; y += y_amount; void point::rotate90( ) { double new_x; double new_y; new_x = y; new_y = -x; x = new_x; y = new_y; 25

...newpoint.cpp... bool operator ==(const point& p1, const point& p2) { return (p1.get_x( ) == p2.get_x( )) && (p1.get_y( ) == p2.get_y( )); bool operator!=(const point& p1, const point& p2) { return!(p1 == p2); point operator +(const point& p1, const point& p2) { double x_sum, y_sum; x_sum = (p1.get_x( ) + p2.get_x( )); y_sum = (p1.get_y( ) + p2.get_y( )); point sum(x_sum, y_sum); return sum; ostream& operator <<(ostream& outs, const point& source) { outs << source.get_x( ) << " " << source.get_y( ); return outs; istream& operator >>(istream& ins, point& target) { ins >> target.x >> target.y; return ins; 26

...newpoint.cpp int rotations_needed(point p) { int answer = 0; while ((p.get_x( ) < 0) (p.get_y( ) < 0)) { p.rotate90( ); ++answer; return answer; void rotate_to_upper_right(point& p) { while ((p.get_x( ) < 0) (p.get_y( ) < 0)) p.rotate90( ); double distance(const point& p1, const point& p2) { double a, b, c_squared; a = p1.get_x( ) - p2.get_x( ); b = p1.get_y( ) - p2.get_y( ); c_squared = a * a + b * b; return sqrt(c_squared); point middle(const point& p1, const point& p2) { double x_midpoint, y_midpoint; x_midpoint = (p1.get_x( ) + p2.get_x( )) / 2; y_midpoint = (p1.get_y( ) + p2.get_y( )) / 2; point midpoint(x_midpoint, y_midpoint); return midpoint; // end namespace main_savitch_2b 27

28