Introduction & Review

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

III. Classes (Chap. 3)

Lab 2: ADT Design & Implementation

CPSC 341 OS & Networks. Introduction. Dr. Yingwu Zhu

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

EECE.3220: Data Structures Spring 2017

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Implementing an ADT with a Class

PIC 10A Objects/Classes

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

Chapter 10 Introduction to Classes

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

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

A Deeper Look at Classes

Object Oriented Design

Introduction to Programming session 24

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

ADTs & Classes. An introduction

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

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

Lecture 18 Tao Wang 1

Object Oriented Design

CSCE 110 PROGRAMMING FUNDAMENTALS

CS 247: Software Engineering Principles. ADT Design

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

SFU CMPT Topic: Class Templates

Comp151. Generic Programming: Container Classes

C++ For Science and Engineering Lecture 15

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

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

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Programming II Lecture 2 Structures and Classes

Do not write in this area A.1 A.2 A.3 A.4 A.5 A.6 B.1 B.2 B.3 TOTAL. Maximum possible points:

2. It is possible for a structure variable to be a member of another structure variable.

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

Operator overloading

1 // Fig. 6.13: time2.cpp 2 // Member-function definitions for class Time. 3 #include <iostream> Outline. 4 5 using std::cout; 6 7 #include <iomanip>

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Polymorphism Part 1 1

การทดลองท 8_2 Editor Buffer Array Implementation

Advanced Systems Programming

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

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

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

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

pointers & references

Exercise 1.1 Hello world

Introduction to Classes

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

Due Date: See Blackboard

Computer Programming with C++ (21)

Due Date: See Blackboard

CSCI 123 Introduction to Programming Concepts in C++

C++ For Science and Engineering Lecture 12

Week 3: Pointers (Part 2)

W3101: Programming Languages C++ Ramana Isukapalli

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Function Overloading

CE221 Programming in C++ Part 1 Introduction

Classes: A Deeper Look

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

EL2310 Scientific Programming

Object-Oriented Design (OOD) and C++

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CSE 333 Midterm Exam Nov. 3, 2017 Sample Solution

CPSC 427: Object-Oriented Programming

EL2310 Scientific Programming

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

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Introduction to C++ Systems Programming

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

C++11 Move Constructors and Move Assignment. For Introduction to C++ Programming By Y. Daniel Liang

Chapter 18 - C++ Operator Overloading

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

Midterm Review. PIC 10B Spring 2018

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

ADT: Design & Implementation

CS3157: Advanced Programming. Outline

Classes and Data Abstraction

Operator Overloading

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Midterm Exam #2 Spring (1:00-3:00pm, Friday, March 15)

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS24 Week 3 Lecture 1

Part I: Short Answer (12 questions, 65 points total)

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Classes in C++98 and C++11

Arizona s First University. More ways to show off--controlling your Creation: IP and OO ECE 373

Transcription:

CPSC 250 Data Structures Introduction & Review Dr. Yingwu Zhu

What to learn? ADT design & implementation using C++ class Algorithm efficiency analysis Big-O ADTs: Binary search trees, AVL trees, Heaps, Priority queues Sorting algorithms Basic sorting algorithms Quicksort, Heapsort, Mergesort

Communication Channels Course website http://fac-staff.seattleu.edu/zhuy Syllabus, tentative schedule, HW, lecture slides, supplemental materials, grading policy, SU emails Check regularly (daily)

Programming Assignments Linux server: cs1.seattleu.edu Use SSHClient or putty to access the server SU email account and password Problem? Contact Renny Philipose (philipr@seattleu.edu) Work from home, NO VPN needed! Submission Electronic submission (announced later!) Ensure your program is compilable!

Study Strategies Class participation Lecture reviews Programming practice Questions/Thinking: What? Why? How?

Other Info See Syllabus for details

C++ classes vs. structs Similarity? Difference?

Structs and Classes: Similarities Essentially the same syntax Both are used to model objects with multiple attributes (characteristics) represented as data members also called fields Thus, both are used to process nonhomogeneous data sets. 9

Differences Structs vs. Classes Members public by default Class members are private by default Can be specified private Can be specified public 10

Advantages in C++ Classes C++ classes model objects which have: Attributes represented as data members Operations represented as functions (or methods) Leads to object oriented programming Objects are self contained "I can do it myself" mentality 11

Class Declaration Syntax class ClassName { public: Declarations of public members private: Declarations of private members }; 1. Data members normally placed in private: section of a class (information hiding) 2. Function members usually in public: section (exported for external use) 12

Class Libraries Class declarations placed in header file Given.h extension Contains data items and prototypes Implementation file Same prefix name as header file Given.cpp extension Programs which use this class library called client/driver programs 13

Translating a Library 14

Example of User-Defined Time Class Now we create a Time class (page 150) Actions done to Time object, done by the object itself Note interface for Time class object, Fig. 4.2 Data members private inaccessible to users of the class Information hiding class Time { private: unsigned myhours, myminutes; char myamorpm; public: }; 15

Constructor Understanding Constructors, p158-159 Initialize data members Optional: allocate memory Note constructor definition in Time.cpp example (p161) ClassName::ClassName (parameter_list) : member_initializer_list { // body of constructor definition }

Constructors Time::Time() { myhours = 0; myminutes = 0; myamorpm = A ; } Data member initialization Time::Time(unsigned h, unsigned m, char c) : myhours(h), myminutes(m), myamorpm(c) { } 17

Overloading Functions Same function name, different signatures! Note existence of multiple functions with the same name Time(); Time(unsigned inithours, unsigned initminutes, char initampm); Known as overloading Compiler compares numbers and types of arguments of overloaded functions Checks the "signature" of the functions 18

Default Arguments Possible to specify default values for constructor arguments Time(unsigned inithours = 12, unsigned initminutes = 0, char initampm = 'A'); Consider Time t1, t2(5), t3(5,30), t4(5,30,'p'); 19

Copy Operations(p166) During initialization Time t = bedtime During Assignment t = midnight; 20

Overloading Operators Same symbol can be used more than one way Operator, function: operator () Two cases If is a function member: a b a.operator (b) Otherwise, a b operator (a,b) Operators: +,-,*,/ Operators: <<, >> 21

Redundant Declarations Note use of #include "Time.h" in Time.cpp Client program Causes "redeclaration" errors at compile time Solution is to use conditional compilation Use #ifndef and #define and #endif compiler directives 22

Pointers to Class Objects Possible to declare pointers to class objects Time * timeptr = &t; Access with timeptr->getmiltime() or (*timeptr).getmiltime() 23

The this Pointer Every class has a keyword, this a pointer whose value is the address of the object Value of *this would be the object itself 24

Exercise Use what we have reviewed to Implement a polynomial class in the form of ax+b, coefficients a and b are integers. The class implementation should have: Constructor Overload operator+ Overload operator<< 25

Example for class ax+b //poly.h: header file #include <iostream> using namespace std; #ifndef _POLY_H // avoid redundant declarations #define _POLY_H class Poly { private: int m_ca; //coefficient a int m_cb; public: Poly(int a, int b) : m_ca(a), m_cb(b) { }; //constructor void display(ostream& out) const; //for operator << overloading Poly operator+(const Poly& po); //overloading + }; //do not forget to put ;!!!! ostream& operator<<(ostream& out, const Poly& po); //overloading << #endif 27

Example: ax+b //poly.cpp: implementation file #include poly.h using namespace std; void Poly::display(ostream& out) const { out << m_ca << x + << m_cb << endl; } Poly Poly::operator+(const Poly& po) { Poly c; c.m_ca = this->m_ca + po.m_ca; //what is this? Do we need it here? c.m_cb = m_cb + po.m_cb; return c; } ostream& operator<<(ostream& out, const Poly& po) { po.display(out); return out; } 28