ADTs & Classes. An introduction

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

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

CS 247: Software Engineering Principles. ADT Design

Object-Oriented Design (OOD) and C++

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

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.

Ch. 12: Operator Overloading

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

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

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

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS304 Object Oriented Programming Final Term

Lecture 18 Tao Wang 1

III. Classes (Chap. 3)

Abstraction in Software Development

Implementing an ADT with a Class

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

Chapter 10 Introduction to Classes

Constants, References

Object Oriented Design

Short Notes of CS201

PIC 10A Objects/Classes

11.2. Overloading Operators

Problem Solving with C++

CS201 - Introduction to Programming Glossary By

Module Operator Overloading and Type Conversion. Table of Contents

7.1 Optional Parameters

Interview Questions of C++

Lecture 3 ADT and C++ Classes (II)

Understanding main() function Input/Output Streams

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

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Computer Science II CSci 1200 Lecture 18 Operators and Friends

IS0020 Program Design and Software Tools Midterm, Fall, 2004

UNIT- 3 Introduction to C++

Operator overloading

Object Oriented Design

CSCI 123 Introduction to Programming Concepts in C++

UNIVERSITY OF SWAZILAND

Lab 2: ADT Design & Implementation

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

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

A <Basic> C++ Course

A <Basic> C++ Course

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

Object Oriented Programming COP3330 / CGS5409

Chapter 18 - C++ Operator Overloading

CS3157: Advanced Programming. Outline

Ch 2 ADTs and C++ Classes

AN OVERVIEW OF C++ 1

Fast Introduction to Object Oriented Programming and C++

Programming Abstractions

Come and join us at WebLyceum

Introduction to Classes

Implementing Abstract Data Types (ADT) using Classes

Classes and Objects. Class scope: - private members are only accessible by the class methods.

CSI33 Data Structures

UEE1302 (1102) F10: Introduction to Computers and Programming

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Chapter 6 Structures and Classes. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Absolute C++ Walter Savitch

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

Function Overloading

Abstract Data Types (ADT) and C++ Classes

Program construction in C++ for Scientific Computing

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

2 ADT Programming User-defined abstract data types

Pointers, Dynamic Data, and Reference Types

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

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

MAHALAKSHMI ENGINEERING COLLEGE B TIRUCHIRAPALLI

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:

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

Collected By Anonymous

Protection Levels and Constructors The 'const' Keyword

CS 215 Fundamentals of Programming II Spring 2011 Project 2

C++_ MARKS 40 MIN

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

EECE.3220: Data Structures Spring 2017

CS201 Latest Solved MCQs

Operator Overloading in C++ Systems Programming

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

CSCE 110 PROGRAMMING FUNDAMENTALS

Friends and Overloaded Operators

OBJECT ORIENTED PROGRAMMING USING C++

Friends and Overloaded Operators

Overloading Operators in C++

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Fundamental Concepts and Definitions

Lesson 12 - Operator Overloading Customising Operators

Evolution of Programming Languages

Pointers & Dynamic Arrays

Introduction to Programming session 24

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

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

CSI33 Data Structures

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

Classes: A Deeper Look

Transcription:

ADTs & Classes An introduction

Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object Class definition includes: member functions member variables

Information Hiding Principle says we should only know what we need to know (to prevent getting bogged down in irrelevant detail) A class that uses information hiding in its design is an Abstract Data Type (ADT member variables hidden access to hidden members available only through member functions

Class declaration promotes information hiding class Name { public: //member functions //declared here private: //member variables //declared here }; Name is a valid identifier public section includes class parts accessible to outside world private section includes hidden parts Note semicolon!

Example object: the Nesper sign Member variables: message time & temperature Member functions: display message display time & temp change message change time & temp

class Nesper { public: void display_message ( ) const; void display_tnt ( ) const; void change_message (char[] text); void change_tnt (int temp, clock current); private: char message[25]; int temperature; clock time; };

Class definition: public section class Nesper { public: void display_message ( ) const; void display_tnt ( ) const;... Public section contains member function prototypes Keyword const indicates that the functions declared here do not change the object

Class definition: public section... void change_message (char[] text); void change_tnt (int temp, clock current); Change functions are examples of modifiers - - member functions that alter the calling object change_tnt includes a parameter (current) that is an example of an ADT variable

Class definition: private section private: char message[25]; int temperature; clock time; }; Member variables declared here Can be variables of built-in types, arrays or pointer, or instances of objects defined elsewhere

Where do they go? Class declaration: header file (xxxx.h) Member function definitions: definition file xxxx.cpp (same name as header -- different extension) should not include a main( ) function Class instances: user program

Implementing member functions #include <iostream.h> #include <cstring.h> #include <assert.h> void Nesper::change_message (char[] text) { assert (strlen(text)<25); message = text; } Preprocessor directives for all needed library routines Class name and scope operator (::) indicate this is a member function

Notes on member functions Every instance of a class (object) has its own copies of all class members, including member functions A member function is called by the object that owns it (can have several objects of same class in a program) Member functions may call other member functions

Using a class in a program #include nesper.h int main( ) { Nesper sign1, sign2;... Preprocessor directive indicates class definition should be included Declaring Nesper variable instantiates the class sign1 and sign2 are objects of type Nesper

Using a class in a program Member function is sign1.change_message activated by calling ( This is my sign ); object sign1.display_message( ); Syntax:... calling_object. function_name (argument(s));

Constructors Special member function Automatically called when an object is instantiated Unique characteristics: Constructor name is same as class name Constructor has no return value (not even void) Can have multiple constructors for a class -- example of function overloading classes2 15

Why define constructors? Can define a class without one; in this case, compiler uses automatic default constructor memory allocated for member variables constructors for individual members are called, if they exist With defined constructor, can do more -- including initializing member variables classes2 16

Constructor Prototypes class Nesper { public: Nesper (string msg); // initializes message Nesper (int temp); // initializes temperature Nesper (clock tm); // initializes time Nesper ( ); // default constructor classes2 17

Notes on Function Overloading Can have as many functions with the same name in a class as you wish Compiler differentiates between the functions by their parameter lists A constructor that requires no arguments is the default constructor -- will be called under most circumstances classes2 18

Calling a constructor Constructor is called when an object is declared: Nesper stopsign ( Stop! Stop, I say! ); // calls first ctor in class Nesper midnight (12:00); // calls ctor with clock parameter Nesper sign; // calls default constructor classes2 19

Constructor implementation Nesper::Nesper (string msg) { message = msg; } classes2 20

Another example: default ctor Nesper::Nesper( ) { message = Your message here ; temperature = 32; time = 12:00; } classes2 21

Default arguments Functions can be declared with default values listed for their parameters Provides flexibility for constructors: can be called with or without arguments can be called with some, but not all arguments specified if no arguments are specified, default values are used classes2 22

Nesper constructor with default arguments class Nesper { public: Nesper (string msg = Your message here, int temp = 32, clock tm = 12:00);... Default arguments are specified in the function prototype, not the implementation When function is called, can omit some or all arguments -- may be omitted starting from right classes2 23

Examples of function calls Nesper mysign ( Taurus, 65, 4:30); // all defaults replaced by actual arguments Nesper yoursign ( Pisces ); // msg replaced by argument; use defaults for // remaining values Nesper sign; // most common -- defaults used for all values classes2 24

Implementation of function with default arguments Nesper::Nesper (string msg, int temp, clock tm) { } message = msg; temperature = temp; time = tm; Implementation is identical to version that called for the same parameters but didn t use default arguments Default arguments appear only in prototype classes2 25

One more variation: inline functions Inline functions are defined (implemented) within the class definition Saves a little execution time (no function call, no return) Can be inefficient in terms of memory (can end up with many copies of same compiled code Best for extremely simple, one-liner functions classes2 26

Inline constructor example class Nesper { public: Nesper( ) { message = Your message ; temperature = 32; time = 12:00; }... Inline functions aren t usually used for constructors, unless the object is very small This is still just one function declaration within class definition classes2 27

Value semantics Operations that determine how values are copied from one object to another object of the same class type Assignment operator Copy constructor: constructor that instantiates an object which is an exact copy of its argument classes2 28

Automatic assignment example Nesper sign1( Hi, 32, 1:00), sign2; // sign2 has default values sign2 = sign1; // sign2 now has same // values as sign1 Can use automatic assignment when the object doesn t use dynamic memory Later we ll see how to define the assignment operation for classes that require it classes2 29

Automatic copy constructor Nesper sign1; Nesper sign2(sign1);... Nesper sign3 = sign1; First object uses default constructor Both second and third objects use copy constructor, even though third example looks like assignment Like automatic assignment, must be explicitly defined for some classes classes2 30

Assignment vs. copy constructor Assignment copies information from one existing object into another existing object Copy constructor declares and initializes a new object, which is a copy of an existing object classes2 31

#ifndef NESPER_H #define NESPER_H One more look at Nesper.h // macro guard -- use to ensure that class declaration // only appears once in a program -- safeguard // to prevent linking errors in large programs class Nesper { public: Nesper (string msg= Your message, int temp=32, clock tm=12:00); void display_message( ) const; void display_tnt( ) const; void change_message (string); void change_tnt (int temp, clock tm); private: string message; int temperature; clock time; }; #endif classes2 32

Overloading operators Have already seen function overloading in action -- multiple constructors Can also overload operators -- including logical, arithmetic & I/O If you want to be able to use these operations on a new class, you must overload them for the class operator overloading 33

Example class Fraction { }; public: Fraction (int num=1, int den=1); int get_num( ) const; int get_den( ) const; private: int numerator; int denominator; Consider the following declaration: Fraction f1, f2; What is the result of the following code? if (f1 = = f2) cout << They are equal ; Yes, it s a trick question! operator overloading 34

Overloading logical operators -- example bool operator = = (const Fraction& f1, const Fraction& f2) // precondition: f1 & f2 are Fractions reduced to LCD form // postcondition: returns true if f1 = = f2, false otherwise { return (f1.getnum( ) = = f2.getnum( ) && f1.getden( ) = = f2.getden( )); } operator overloading 35

Can use one overloaded operator to define another bool operator!= (const Fraction& f1, const Fraction& f2) { return!(f1 = = f2); } operator overloading 36

Notes on operator overloading Still leaves ordinary uses of operator available -- compiler uses context to determine which instance of = = or!= to use Binary operators are generally not declared as class member functions it s syntactically legal, but: no longer binary -- becomes operation performed on only one of the operands operator overloading 37

Overloading arithmetic operators As with binary comparison operators, binary arithmetic operators usually implemented as non-member functions Can overload more than one instance of an operator on same data type -- this is how, for example, it s possible to multiply a double by a double, but also a double by an int operator overloading 38

Overloading multiplication on Fractions Fraction operator * (const Fraction& f1, const Fraction& f2) { int n, d; n = f1.getnum( ) * f2.getnum( ); d = f1.getden( ) * f2.getden( ); Fraction result (n,d); return result; } operator overloading 39

General notes on binary operator overloading Syntax pattern: return type operator symbol (operand type& op1, Define as non-member functions operand type& op2); If access to private members is required, must provide for this can be done with get functions, as we ve seen can also be done by other means, shown shortly operator overloading 40

Overloading insertion & extraction Already overloaded for the basic types Can also be overloaded for new classes Once overloaded, will work on all stream types operator overloading 41

Example: overloading insertion ostream& operator << (ostream& o, const Fraction& f); Return type & first argument are output stream references -- this allows chaining Insertion must be defined as a non-member function Function use: Fraction half; cout << half; operator overloading 42

Function implementation ostream& operator << (ostream& o, const Fraction& f) { o << f.getnum( ) << / << f.getden( ); return o; } operator overloading 43

Overloading extraction istream& operator >> (istream& i, Fraction& f); Prototype similar to insertion -- same points apply Problem: accessor functions haven t been defined in Fraction class to put new values in an existing Fraction, and non-member functions can t access private members operator overloading 44

Possible solutions Define member functions to set values of numerator & denominator, then call these in operator >> function would be functional but defeats information hiding Better solution: make operator >> a friend function operator overloading 45

Friend function Non-member function that has access to private members of a class To declare a friend function, place its prototype in the class definition preceded by the keyword friend: friend istream& operator >> (istream& i, Fraction& f); operator overloading 46

Function implementation istream& operator >> (istream& i, Fraction& f) { char slash; // reads divider character i >> f.numerator >> slash >> f.denominator; return i; } operator overloading 47