CSI33 Data Structures

Similar documents
A <Basic> C++ Course

A <Basic> C++ Course

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

Structuur van Computerprogramma s 2

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

EL2310 Scientific Programming

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

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

CSI33 Data Structures

PIC10B/1 Winter 2014 Exam I Study Guide

Due Date: See Blackboard

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

C++_ MARKS 40 MIN

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

Abstraction in Software Development

CSCE 110 PROGRAMMING FUNDAMENTALS

CSI33 Data Structures

Object Oriented Programming

CSI33 Data Structures

EL2310 Scientific Programming

Financial computing with C++

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

III. Classes (Chap. 3)

Overloaded Operators, Functions, and Students

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

Programming Abstractions

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:

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

Introduction to Classes

Programmation avancée pour le calcul scientifique Lecture title: Classes

CS 247: Software Engineering Principles. ADT Design

Scientific Computing

C++ Class Details, Heap

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

Function Overloading

PIC 10A Objects/Classes

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

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

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Absolute C++ Walter Savitch

CSI33 Data Structures

Due Date: See Blackboard

ADTs & Classes. An introduction

16. Structs and Classes I

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

PIC 10A. Lecture 17: Classes III, overloading

AN OVERVIEW OF C++ 1

Chapter 18 - C++ Operator Overloading

CS 247: Software Engineering Principles. Modules

Implementing Abstract Data Types (ADT) using Classes

Operator Overloading

CSCE 206: Structured Programming in C++

Fast Introduction to Object Oriented Programming and C++

Come and join us at WebLyceum

Chapter 2. Procedural Programming

CPE Summer 2015 Exam I (150 pts) June 18, 2015

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

Class Example. student.h file: Declaration of the student template. #ifndef STUDENT_H_INCLUDED #define STUDENT_H_INCLUDED

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

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

Problem Solving with C++

OBJECT ORIENTED PROGRAMMING USING C++

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Chapter 5: Loops and Files

Ch 2 ADTs and C++ Classes

Come and join us at WebLyceum

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

EL2310 Scientific 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.

Object-Oriented Programming

C++ Quick Guide. Advertisements

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

Inheritance and Polymorphism

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

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

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

CS11 Intro C++ Spring 2018 Lecture 3

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

this Pointer, Constant Functions, Static Data Members, and Static Member Functions this Pointer (11.1) Example of this pointer

Question: How can we compare two objects of a given class to see if they are the same? Is it legal to do: Rectangle r(0,0,3,3);,, Rectangle s(0,0,4,4)

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...

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

COMP322 - Introduction to C++

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

Abstract Data Types (ADT) and C++ Classes

CS 215 Fundamentals of Programming II Spring 2011 Project 2

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

CSCI 123 Introduction to Programming Concepts in C++

EECE.3220: Data Structures Spring 2017

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

Module Operator Overloading and Type Conversion. Table of Contents

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

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

PHY4321 Summary Notes

CSI33 Data Structures

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

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

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

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

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

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College November 9, 2016

Outline Outline 1 Chapter 9: C++ Classes

Outline Chapter 9: C++ Classes 1 Chapter 9: C++ Classes

Class Syntax Chapter 9: C++ Classes Basics C++ is Object-Based: classes can be implemented. Classes have the same components as in Python Data Members = Attributes = (Instance or Class) Variables Member Functions = (Instance or Class) Methods C++ is compiled, so classes (with their components) must be declared before they can be used by any code. A class declaration is usually written in a header file (<classname>.h), so it can be included by any program file using or implementing the class. It declares data members and member functions. The definitions of member functions of a class are usually in an implementation file (<classname>.cpp). Once it is compiled, it can be linked with any application file.

Class Syntax Chapter 9: C++ Classes Form of a Class Declaration (Definition) Begins with class <classname> { Data Member declarations are similar to those for local variables: type, name. Member Functions declarations are similar to those for normal functions: return type; function name; parameter list; default values. Does not (usually!) give the implementation of member functions. Ends with };

Class Syntax Chapter 9: C++ Classes Member Functions No self parameter! Like function declarations: return type; function name; parameter list; default values. const member functions do not change any data member (attribute) of the object. inline member function declarations have implementation code. Constructors have the same name as the class. Different constructors for one class must have different signatures (formal parameter lists). (later: destructors)

Class Syntax Chapter 9: C++ Classes Access Specifiers Set access levels for member functions and data members: public: available outside the scope of the class. private: available only within the scope of the class. protected: available within the scope of the class or within subclasses derived from the class.

Class Syntax Chapter 9: C++ Classes Class Header File Example: Rational.h #ifndef RATIONAL H #define RATIONAL H class Rational { public: Rational(int n = 0, int d = 1) { set(n, d); } bool set(int n, int d); int num() const { return num ; } int den() const { return den ; } double decimal() const {return num /double(den );} private: int num, den ; }; #endif

Class Syntax Chapter 9: C++ Classes Class Implementation Files (.cpp) Include the header file for that class Class Implementation Member Function Implementations Scope resolution operator (::): (classes and namespaces)

Class Syntax Chapter 9: C++ Classes Class Implementation File Example: Rational.cpp #include "Rational.h" bool Rational::set(int n, int d) { if (d!= 0) { num = n; den = d; return true; } else return false; }

The C++ string Class Usage #include <string> to use this class. <<, >> are overloaded to work with cin and cout. getline(cin, name): all text typed before the end-of-line delimiter goes into name. <, <=, >, >=, ==,!=, +, += are overloaded.

ifstream, ofstream Classes Usage #include <fstream> to use these classes. Use infile.open(filename.c str()), outfile.open(filename.c str()) to access. <<, >> are overloaded to work with ifstream and ofstream. getline(cin, name) gives name all text typed before the end-of-line delimiter. Use infile.close(), outfile.close() to end access.

Overloading Operator Symbols Overloading + as Standalone Function class Rational { public: Rational(int n = 0, int d = 1) { set(n, d); }... // access functions int num() const { return num ; } int den() const { return den ; }... private: int num, den ; }; Rational operator+(const Rational &r1, const Rational &r2);

Overloading Operator Symbols Overloading + as Standalone Function... Rational operator+(const Rational &r1, const Rational &r2) { int num, den; num = r1.num() * r2.den() + r1.den() * r2.num(); den = r1.den() * r2.den(); return Rational(num, den); }

Overloading Operator Symbols Overloading + as Standalone Function // mainv1.cpp # include "Rationalv1.h" int main() { Rational r1(2, 3), r2(3, 4), r3; r3 = r1 + r2; // common method of calling r3 = operator+(r1, r2); // direct method of calling }

Overloading Operator Symbols Overloading + as Member Function class Rational { public: Rational(int n = 0, int d = 1) { set(n, d); }... // access functions int num() const { return num ; } int den() const { return den ; }... Rational operator+(const Rational &r2) const; private: int num, den ; };

Overloading Operator Symbols Overloading + as Member Function... Rational Rational::operator+(const Rational &r2) const { Rational r; r.num = num * r2.den + den * r2.num ; r.den = den * r2.den ; return r; }...

Overloading Operator Symbols Overloading + as Member Function // mainv2.cpp #include "Rationalv2.h" int main() { Rational r1(2, 3), r2(3, 4), r3; r3 = r1 + r2; // common method of calling r3 = r1.operator+(r2); // direct method of calling }

Overloading Operator Symbols friend Functions and Classes Declared within the definition of a class using the keyword friend Allowed to have access to the private data and functions of the class. Needed for efficient performance with other classes.

Overloading Operator Symbols friend Example: Rational.h friend std::istream& operator>>(std::istream& is, Rational &r); friend std::ostream& operator<<(std::ostream& os, const Rational &r);... std::istream& operator>>(std::istream &is, Rational &r); std::ostream& operator<<(std::ostream &os, const Rational &r);

Overloading Operator Symbols friend Example: Rational.cpp std::istream& operator>>(std::istream &is, Rational &r) { char c; is >> r.num >> c >> r.den ; return is; } std::ostream& operator<<(std::ostream &os, const Rational &r) { os << r.num() << "/" << r.den(); return os; }

Class Variables Syntax Declared using the static keyword. All instances (objects) in the class share the same value for a class variable. There is only one value for the entire class. Just as in the Python Card class.

Class Variables Example: Card.h class Card {... private:... static const std::string suits [4]; static const std::string faces [13]; };

Class Methods Chapter 9: C++ Classes Syntax Declared using the static keyword. Can only access class variables. (A function call to a class method is not related to any particular instance.) Can be used to count how many instances are alive for a class: increment count in the constructor, decrement the count in the destructor. Must call using the class name and scope qualifier: Card::count().