CS 247: Software Engineering Principles. ADT Design

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

Constants, References

A <Basic> C++ Course

A <Basic> C++ Course

CS 247: Software Engineering Principles. Modules

Value vs. Entity Objects, Information Hiding

ADTs & Classes. An introduction

Implementing Abstract Data Types (ADT) using Classes

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

Abstract Data Types. Development and Implementation. Well-defined representations that allow objects to be created and used in an intuitive manner

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

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

CS3157: Advanced Programming. Outline

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

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Overloading Operators in C++

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Copy Constructor, Assignment, Equality

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

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

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

Due Date: See Blackboard

Ch 8. Operator Overloading, Friends, and References

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.

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

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

CS 215 Fundamentals of Programming II Spring 2011 Project 2

Short Notes of CS201

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Object Oriented Programming COP3330 / CGS5409

CS201 - Introduction to Programming Glossary By

Abstract Data Types (ADT) and C++ Classes

PIC10B/1 Winter 2014 Exam I Study Guide

More Advanced Class Concepts

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Module Operator Overloading and Type Conversion. Table of Contents

Friends and Overloaded Operators

Interview Questions of C++

UEE1302 (1102) F10: Introduction to Computers and Programming

Intermediate Programming, Spring 2017*

Friends and Overloaded Operators

11.2. Overloading Operators

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

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

Understanding main() function Input/Output Streams

PIC 10A Objects/Classes

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

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

17. Classes. Encapsulation, Classes, Member Functions, Constructors

More class design with C++ Starting Savitch Chap. 11

Use the dot operator to access a member of a specific object.

Introduction to C++ (Extensions to C)

CS304 Object Oriented Programming Final Term

CS11 Intro C++ Spring 2018 Lecture 5

Constructors.

2 ADT Programming User-defined abstract data types

A First Program - Greeting.cpp

Program construction in C++ for Scientific Computing

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Lecture 3 ADT and C++ Classes (II)

Chapter 2: Basic Elements of C++

C++_ MARKS 40 MIN

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Special Member Functions

Programming Languages: OO Paradigm, Objects

14.1. Chapter 14: static member variable. Instance and Static Members 8/23/2014. Instance and Static Members

Fast Introduction to Object Oriented Programming and C++

Inheritance, and Polymorphism.

Programming. C++ Basics

pointers & references

G52CPP C++ Programming Lecture 13

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Assertions and Exceptions

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

G52CPP C++ Programming Lecture 17

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Friend Functions and Friend Classes

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

Ch. 12: Operator Overloading

Absolute C++ Walter Savitch

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Practice Problems CS2620 Advanced Programming, Spring 2003

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Overloading & Polymorphism

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Operator overloading: extra examples

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

CS250 Final Review Questions

Abstraction in Software Development

CS242 COMPUTER PROGRAMMING

CSCE Practice Midterm. Data Types

Introduction to Programming

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

III. Classes (Chap. 3)

Programming in C++: Assignment Week 8

Chapter 4 Defining Classes I

Transcription:

CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17

Abstract Data Types (ADTs) An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can hold, the operations that manipulate variables of that type. Provides compiler support for your restrictions on values and operations turns programmer errors into type errors (checked by the compiler). Can change value range, data representation without changing client source code. U Waterloo CS247 (Spring 2017) p.2/17

Client Code for Rational ADT #include <iostream> using namespace std; int main () { Rational r, s; cout << "Enter rational number (a/b): "; cin >> r; cout << "Enter rational number (a/b): "; cin >> s; Rational t(r+s); cout << r << " + " << s << " = " << r+s << endl; cout << r << " * " << s << " = " << r*s << endl; cout << r << " == " << s << " is " << (r==s) << endl; } return 0; U Waterloo CS247 (Spring 2017) p.3/17

1. Legal Values public: Rational (); // == 0/1 Rational (int num, int denom) throw (char const*); explicit Rational (int num); // == num/1 private: int numerator_; int denominator_; A constructor initializes new object to a legal value. U Waterloo CS247 (Spring 2017) p.4/17

2. Public Accessors and Mutators public: int numerator() const; int denominator() const; void numeratoris( const int ); void denominatoris( const int ) throw (char const*); Accessors and mutators provide restricted read/update access to data members. - Want some naming convention. Best practice: Mutators check that client-provided values are within ADT value range. Best practice: Whenever possible, - pass parameters by const reference, - use const member functions. U Waterloo CS247 (Spring 2017) p.5/17

3. Function Overloading public: Rational (); // default value == 0/1 Rational (int num); // value = num/1 Rational (int num, int denom); // value = num/denom Function overloading allows you to use the same function name for variants of the same function. functions must have different argument signatures cannot overload functions that differ only by return type U Waterloo CS247 (Spring 2017) p.6/17

4. Default Arguments public: Rational (); // default value == 0/1 Rational (int num); // value = num/1 Rational (int num, int denom); // value = num/denom Rational (int num = 0, int denom = 1); Use default arguments to combine variants that vary in userprovided arguments. must appear only in the function declaration only trailing parameters may have default values once one default argument is used in a function call, all subsequent arguments in call must be defaults U Waterloo CS247 (Spring 2017) p.7/17

5. Operator Overloading Rational Rational::operator+ ( const Rational &r ) const { return Rational( numerator() * r.denominator() + denominator() * r.nominator(), denominator() * r.denominator() ); } bool Rational::operator== ( const Rational &r ) const { Rational a( this->reduce() ); Rational b( r->reduce() ); } return ( (a.numerator()==b.numerator()) && (a.denominator()==b.denominator() ); Design Decision: signature of the operator argument types, return type, const, pass-by-value/pass-by-reference Best Practice: use operator signatures that the client programmer is used to (e.g., operator== returns a bool) Cannot create new operations (e.g., operator**). Cannot change the number of arguments. U Waterloo CS247 (Spring 2017) p.8/17

6. Non-member Functions }; // Arithmetic Operations Rational operator+ (const Rational&, const Rational&); Rational operator* (const Rational&, const Rational&); // Comparison Operations bool operator== (const Rational&, const Rational&); bool operator!= (const Rational&, const Rational&); A non-member function is a critical function of the ADT that is declared outside of the class. Reduces number of functions with direct access to private data members. Some functions have to be non-member functions (e.g., operator>>). U Waterloo CS247 (Spring 2017) p.9/17

operator>>, operator<< friend ostream& operator<< (ostream&, const Rational&); friend istream& operator>> (istream&, Rational&);... }; ostream& operator<< (ostream &sout, const Rational &r); istream& operator>> (istream &sin, Rational &s); Best Practice: Streaming operators should be nonmember functions, so that first operand is reference to stream object. // example client code: cout << r << " + " << s << " = " << r+s << endl; Best Practice: Return value is modified stream, so that stream operations can be chained. U Waterloo CS247 (Spring 2017) p.10/17

7. Type Conversion of ADT objects explicit Rational (int num=0, int denom=1); The compiler uses constructors that have one argument to perform implicit type conversion. Also true of constructors that have more than one argument, if rest of arguments have default values. Can prohibit this use of constructors via keyword explicit. U Waterloo CS247 (Spring 2017) p.11/17

8. Private Data Representation public: Rational( int num=0, int den=1 ); int numerator() const; int denominator() const; void numeratoris( const int ); void denominatoris( const int ) throw (char const*); private: int numerator_; int denominator_; }; Best Practice: Data members should be private, always. ease evolution of the data representation client code can access data via public accessors, mutators derived classes can manipulate data via public or protected methods nonmember functions can manipulate data via public methods or by being a friend U Waterloo CS247 (Spring 2017) p.12/17

Friends // Alternate implementation friend istream& operator>> (istream&, Rational&);... }; istream& operator>> (istream &sin, Rational &r) { char slash; sin >> r.numerator_ >> slash >> r.denominator_; return sin; } Sometimes we want the default access to be private, but to grant access to select code (e.g., class-related nonmember functions). - Friends are easier to track than family (when code changes). U Waterloo CS247 (Spring 2017) p.13/17

9. Helper Functions private: void reduce(); static int gcd( int, int ); }; Best Practice: Hide helper functions as private methods or within a namespace. Helper functions modularize code that is common among multiple class-related functions, but should not pollute the global namespace. U Waterloo CS247 (Spring 2017) p.14/17

10. Override / Final class A : B { virtual int foo ( ) const final; void bar ( ) const override; }; class Rational final { }; Applying override to a method signifies that the method overrides a virtual function in the base class. Applying final to a virtual function prevents the function from being overridden in derived classes. final may also be applied to a class, in which case the class is prohibited from being used as a base class. U Waterloo CS247 (Spring 2017) p.15/17

Rational class Rational final { public: Rational( int num=0, int den=1 ); int numerator() const; int denominator() const; void numeratoris( const int ); void denominatoris( const int ); private: int numerator_; int denominator_; void reduce(); static int gcd ( int, int ); }; bool operator== ( const Rational&, const Rational&); bool operator!= ( const Rational&, const Rational&); bool operator< ( const Rational&, const Rational&); bool operator<= ( const Rational&, const Rational&); bool operator> ( const Rational&, const Rational&); bool operator>= ( const Rational&, const Rational&); Rational operator+ ( const Rational&, const Rational&); Rational operator* ( const Rational&, const Rational&); istream& operator>> ( istream&, Rational& ); ostream& operator<< ( ostream&, Rational& );

Summary of Attribute-based ADT Design Range of legal values Default initial value? Construct object from client-provided values? explicit constructors or allow type conversion? Throw exception if constructor or mutator is given an illegal value. Attributes (virtual data members) Accessors, mutators; consistent naming scheme Nonmember functions Overloaded functions; default arguments Overloaded operators Final Should the client programmer be able to create derived classes?