Abstract Data Types (ADT) and C++ Classes

Similar documents
pointers & references

The Class Construct Part 2

Programming, numerics and optimization

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

CS 247: Software Engineering Principles. ADT Design

1.124 Quiz 1 Thursday October 8, 1998 Time: 1 hour 15 minutes Answer all questions. All questions carry equal marks.

1.124 Quiz 1 Thursday October 8, 1998 Time: 1 hour 15 minutes Answer all questions. All questions carry equal marks.

Ch 2 ADTs and C++ Classes

Chapter 4 Defining Classes I

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

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

Object-Oriented Programming

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Introduction to Classes

CS11 Intro C++ Spring 2018 Lecture 1

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

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

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

Classes in C++98 and C++11

Lecture 3 ADT and C++ Classes (II)

C++ Programming Classes. Michael Griffiths Corporate Information and Computing Services The University of Sheffield

ADTs & Classes. An introduction

Miri Ben-Nissan (Kopel) (2017)

CSI33 Data Structures

Object-Oriented Programming Concepts

An introduction to Java II

CS304 Object Oriented Programming Final Term

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

Inheritance and Polymorphism

Chapter 19 C++ Inheritance

SFU CMPT Topic: Classes

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

CS250 Final Review Questions

C++ Constructor Insanity

CS 11 C++ track: lecture 1

2 ADT Programming User-defined abstract data types

CS11 Intro C++ Spring 2018 Lecture 3

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Intermediate Programming & Design (C++) Classes in C++

Lab 2: ADT Design & Implementation

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

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

2 2

PIC 10A. Lecture 15: User Defined Classes

Chapter 19 - C++ Inheritance

Programming Languages and Techniques (CIS120)

CS 247: Software Engineering Principles. Modules

Object Oriented Programming in C#

Programming Languages and Techniques (CIS120)

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.

Intermediate Programming, Spring 2017*

Fast Introduction to Object Oriented Programming and C++

Namespaces and Class Hierarchies

Do not write in this area TOTAL. Maximum possible points: 75

CS11 Introduction to C++ Fall Lecture 1

Chapter 6 Introduction to Defining Classes

A <Basic> C++ Course

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

EL2310 Scientific Programming

A <Basic> C++ Course

SFU CMPT Topic: Has-a Relationship

Software Design Abstract Data Types

Anatomy of a Class Encapsulation Anatomy of a Method

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

EECE.3220: Data Structures Spring 2017

Implementing Abstract Data Types (ADT) using Classes

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

Abstraction in Software Development

IS0020 Program Design and Software Tools Midterm, Fall, 2004

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

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

CSCE 110 PROGRAMMING FUNDAMENTALS

Where do we stand on inheritance?

CSI33 Data Structures

CE221 Programming in C++ Part 1 Introduction

Short Notes of CS201

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Interview Questions of C++

An Introduction to C++

INHERITANCE - Part 1. CSC 330 OO Software Design 1

CS201 - Introduction to Programming Glossary By

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

CS11 Introduction to C++ Fall Lecture 3

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

Derived and abstract data types. TDT4205 Lecture 15

Financial computing with C++

C++ (Non for C Programmer) (BT307) 40 Hours

C:\Temp\Templates. Download This PDF From The Web Site

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

Operator overloading: extra examples

CS 162 Intro to CS II. Structs vs. Classes


CS3157: Advanced Programming. Outline

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

III. Classes (Chap. 3)

C++ (classes) Hwansoo Han

And Even More and More C++ Fundamentals of Computer Science

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

Transcription:

Abstract Data Types (ADT) and C++ Classes 1-15-2013

Abstract Data Types (ADT) & UML C++ Class definition & implementation constructors, accessors & modifiers overloading operators friend functions HW#1 posted due: Tuesday, 1/22 Quiz Thursday, 1/24

FAQ 5.14 What is an abstraction and why is it important? An abstraction is a simplified vew of an object in the user s own vocabulary. In OO and C++, an abstraction is the simplest interface to an object that provides all the features and services the intended users expect. FAQ 5.15 Should abstractions be user-centric or developer-centric? User-centric. Focus on the user s point of view.

Computes what each employee should be paid for a day of work. Reads a file containing start and stop times for each employee. Then calculates and saves the pay amounts to another file. int runpaycalculator (const char csinputfilename[], const char csoutputfilename[]) Sample input: 510 + 24601 990-24601 Sample output 24601 96

int runpaycalculator (const char csinputfilename[], const char csoutputfilename[]) Algorithm: call computehours and then computeandwritepay Data structure: The number of hours worked by each employee is stored in an array indexed by the possible employee numbers. This array is of size MAX_EMPLOYEE_NUMBER + 1, where MAX_EMPLOYEE_NUMBER is a global constant

In a well-designed modular program, software components should satisfy the following two properties: 1. Each component performs one well-defined task. (i.e. cohesion ) 2. Each component is as independent as possible from the others. (i.e. loosely coupled )

1. Easier to understand; little redundant code. 2. Facilitates software reuse. 3. Easier to implement. 4. Easier to test. 5. Easier to modify.

Independence of modules is typically achieved by information hiding (which can be achieved by encapsulation ). Procedural Abstraction Use of a function depends on its purpose (what it does) but not on its implementation (how it does it). FAQ 5.18 What s the value of separating interface from implementation? It s a key to eliminating the ripple effect when a change is made.

Class designer/implementer - designs & implements a class vs. Client programmer - uses a class for an application vs. End-user - uses the application

Procedural Abstraction (Algorithm) Data Abstraction (Data)

An Abstract Data Type (ADT) is a specification of a set of data and a set of operations that can be performed on the data. examples: String List Dictionary Student Time Circle Dice Song Telephone Directory Complex number

In C++, a class represents an ADT. An instance of a class is a specific object which is created, and the data members are filled in with values (possibly default values). Objects are created with a specialized member function called a constructor. An instance of a class is destroyed (recycled) with a specialized member function called a destructor

Type name Public interface Circle constructor(s): Circle(int,int), Circle(float,int,int); float computearea(); float getradius(); void setradius(float); // etc.

An ADT is a contract between The interface designer and... The coder of a class that implements the interface Precondition: any assumption/constraint on the method data before the method begins execution Postcondition: describes result of executing the method

A C++ program is a collection of functions and classes. A class represents a set of objects that have common properties. A class is a template for creating objects. A class represents a type. Type determines the set of values an object may have. Type determines the operations that can be performed on those values. In C++ there are two kinds of types: Primitive or build-in types User Defined or class types

A class consists of members Data members also called data fields or attributes Member functions also called operators, functions or methods Data members are also sometimes called instance variables because each object (instance of a class) contains them. Data members may be either primitive or class types.

private instance variables: private float radius; private Point center; public methods: constructor(s) accessor methods (get) mutator methods (set) float computearea() etc.

Represent a 2D point Data: (x, y) coordinates, integer values Methods: create a point with coordinates (0,0) create a point with coordinates (x,y), get the x coordinate of a point, get the y coordinate of a point, draw a point erase a point move a point etc.

Unified Modeling Language (UML) is a standard diagram notation for describing a class Field signatures: type and name Method signatures: name, argument types, result type Class name Field values Instance of Person

Type name Public interface Point constructor(s): Point(int,int), Point(); // default (0,0) int getx(); int gety(); // etc.

private instance variables: private int xcoordinate private int ycoordinate public methods: constructor(s) accessor methods (get) mutator methods (set) etc.

Class members that are declared in the public section of a class definition are accessible to all functions (inside or outside) the class. Class members that are declared in the private section of a class definition are accessible only to functions that are members of the class. Generally we want the operators (member functions) visible to the users of the class. Thus they are declared public. Generally we want to keep the implementation details (data members) hidden from the users of the class Thus they are declared private.

A constructor is a member function that initializes the data members of an object when the object is created. Note the use of initialization lists (more efficient than assignment statements) class Point { public: Point(int i, int j) : x(i), y(j) { } Point() : x(0), y(0) { } } private: int x; int y;

A modifier function provides the ability to modify the value of a private data member void setx(int newx) { x = newx; } An accessor function provides the ability to read the value of a private data member, without changing it (note use of const ) int getx() const { return x; }

class Point { public: Point(int i, int j) : x(i), y(j) { } } Point() : x(0), y(0) { } int getx() const { return x; } private: int x; int y; Client programmer can write: Point p1(10,30); Point p2; int i = p1.getx(); receiver (this) message call the method Point::getX() class of the receiver no args

A member function definition (implementation) may be included in the class definition. The compiler can insert the code for the function body where the function is called. This is known as an inline function. Use of inline member function is recommended only for the following: Functions whose body is very small (one or two lines) Constructors Accessors Modifiers

How would you compare two points, p1 and p2. Define a method to compare their x and y coordinates.p p1.lessthan(p2) Overload the operator < p1 < p2 bool Point::operator< (const Point& other) const { return (x < other.x) ((x == other.x) && (y < other.y)); }

class Point { public: Point(int i, int j) : x(i), y(j) { } Point() : x(0), y(0) { } int getx() const { return x; } Client programmer can write: Point p1; cin>>i; cin>>j; Point p2(i,j); if (p2 < p1) then cout << lol ; bool operator<(const Point& other) const; private: int x; int y; } bool Point::operator< (const Point& other) const { return // you fill in... }

class Point { public: // other methods as before Point p1(10,30); std::ostream& operator<<( std::ostream& os, const Point& p); private: // as before } receiver (this) cout << p1; message arg ostream& operator<<( ostream& os, const Point& p) { os << //... you fill in return os; } problem: The receiver is type ostream solution: Make this function a friend

class Point { public: // other methods as before Point p1 = new Point(10,30); friend std::ostream& operator<<( std::ostream& os, const Point& p); private: // as before } ostream& operator<<( ostream& os, const Point& p) { os << //... you fill in return os; } cout << p1; solution: Make this function a friend Gives permission for this function to have complete access to the data members, even though they are private to the class

A friend is an external function or class that is given the same access to the members of a class as if it were a member. We declare the ostream insertion operator to be a friend So it can access the data members and insert their string representation into the output stream. Because this operator s left-hand operand is an ostream object, thus it cannot be defined as a member of the Point class.

class Point { public: // other methods as before friend std::ostream& operator<<( std::ostream& os, const Point& p); private: // as before } ostream& operator<<( ostream& os, const Point& p) { os << //... you fill in return os; } /* how do these differ? */ Point p1 = new Point(10,30); Point p2(10,30); /* how do these differ? */ cout << p1.getx() << endl; cout << p1; /* additional examples were done in class */

class Point { public: // other methods as before bool operator==(const Point& other) const; private: // as before } Point p3 = new Point(10,30); if (p2 == p3) cout << equal points << endl; bool Point::operator== (const Point& other) const { return // you fill in... } Now that we have implemented operators == and <, what about <=? Reuse code!

1. Point.h header file (declarations & inline code) 2. Point.cpp implementation file (code) 3. main.cpp test driver g++ *.cpp

Could type this: % g++ -c *.cpp % g++ *.o o testpt or % make %./testpt % make clean Makefile testpt: main.o Point.o g++ main.o Point.o o testpt main.o: main.cpp Point.h Point.o g++ -c main.cpp o main.o Point.o: Point.h Point.cpp g++ -c Point.cpp o Point.o clean: rm f *.o testpoint

For Thursday, read Chapter 2, sections 2.6 2.9 of Maciel & Chapter 3, section 3.1 Be prepared to write a simple class definition for Circle Quiz #1 next week, Thursday, 1/24, in class