CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 2. 150107. CARRANO CH1, C++ INTERLUDE 1.

Slides:



Advertisements
Similar presentations
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Advertisements

Abstract Data Type Fraction Example
1 Demo Reading Assignments Important terms & concepts Fundamental Data Types Identifier Naming Arithmetic Operations Sample Programs CSE Lecture.
Operator Overloading Back to Fractions.... Implementing an Object We’ve talked at length about object- orientation. – We’ve looked heavily at encapsulation.
Class and Objects.
Abstract Data Types Development and Implementation JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Abstract Data Types Development and Implementation.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Rossella Lau Lecture 10, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 10: Operator overload  Operator overload.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Abstract Data Type. COMP104 Slide 2 Summary l A class can be used not only to combine data but also to combine data and functions into a single (compound)
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
1 Gentle Introduction to Programming Session 5: Memory Model, Object Oriented Programming.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
Object Oriented Programming C++. ADT vs. Class ADT: a model of data AND its related functions C++ Class: a syntactical & programmatic element for describing.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
1 CSC241: Object Oriented Programming Lecture No 07.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
Object Oriented Programming in C++ Chapter5 Operator Overloading.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 14. User Defined Classes.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R1. ADTs as Classes.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Case Study - Fractions Timothy Budd Oregon State University.
Introduction to Classes in C++
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSS 332 PROGRAMMING ISSUES WITH OBJECT-ORIENTED LANGUAGES LECTURE
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE C++ INTERLUDE 1.3. C++ BOOK.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH 2, APPENDIX E.
1 Today’s Objectives  Announcements Homework #3 is due on Monday, 10-Jul, however you can earn 10 bonus points for this HW if you turn it in on Wednesday,
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO , APP D, C++ BOOK.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Using Member Functions and Data Members.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
1 Introduction to Object Oriented Programming Chapter 10.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Yan Shi CS/SE 2630 Lecture Notes
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value A copy of the argument’s value.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CSCE 210 Data Structures and Algorithms
Classes (Part 1) Lecture 3
CSS342: Objects and Classes
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
The dirty secrets of objects
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Fraction Abstract Data Type
CS212: Object Oriented Analysis and Design
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
COMS 261 Computer Science I
COP 3330 Object-oriented Programming in C++
Development and Implementation
Class rational part2.
Presentation transcript:

CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CH1, C++ INTERLUDE 1

Announcements Syllabus/Website Review Correction: VS 2013 Update 4 HW1 Questions Can I get away with a single constructor? Where should I place const definitions? What do I need to turn in? 1/12, Monday: 15min in class peer design review.

Why C++ Object Oriented Programming (OOP)? Abstraction Encapsulation Hierarchy Polymorphism

Encapsulation

Encapsulation and Information Hiding Wall:Not only encapsulate the entire implementation but also make it invisible/inaccessible. Slit:Interface of the implementation such as arguments and a return value. Implementation of method S Program that uses method S Function call with arguments Return a value

Class Classes:a new data type formed of a collection of data and a set of operations on the data Data Structures:a construct within a programming language that stores a collection of data add remove query Behave as a new data type Program that uses a class Examples: - student lists - rational numbers - complex numbers - currency (cents/dollars) - length measurement (inches/feet) - weight measurement (oz/lbs)

Encapsulation at ground level Program consists of Driver file (main) Classes (ex, MyClass) MyClass.h -- interface MyClass.cpp -- implementation Interface (.h file) public: functions called by others private: data, helper functions Implementation (.cpp file) MyClass::function()

Examples of private – flush out deck example const int CARDS_IN_DECK = 52; class Deck { public: Deck(); Card DealSingleCard(); Hand DealHand(int number); ….interface as defined last time…. ~Deck(); private: Card deck[52]; }; enum Suit { Diamond, Spade, Heart, Club }; class Card { public: Card(); Card(int value, Suit suit); ~Card(); private: int value; Suit suit; };

Friends Declared on either functions or classes; identified with the friend keyword Non-member functions can access private data of an object if it has been declared a friend class foo { friend class TheClassToGrantAccess; public: private: }

Constructor Uses same name as class is constructs Nothing returned; not even void Default constructor created Function executed on creation of object Arrays: constructors called in increasing order Arr[0], Arr[1], Arr[2],… Signature chooses constructor Casting of call parameters done using normal casting rules What’s in a constructor? Normally used initialize private data as built-in types in C++ are undefined (int a; a is unknown state) Generally side-effecting is not done

Computer Scientist of the week Edsger Dijsktra Shortest Path Algorithm (Dijkstra’s Algorithm) Semaphores for coordinating multiple processors/programs Pioneer in Distributed Systems 1972 Turing Award winner (programming languages) Formal verification of programs Schlumberger Centennial Chair, University of Texas

C++ Fundamentals let’s code…

C++ Program Dev Lifecycle

IO: Console #include using namespace std; { int age; cout << "hello world. How old are you (in years)?\n"; cin >> age; cout << "that is " << (365 * age) << " days."; }

string Mutable; Copied not shared. string firstName = "jimmy"; string lastName("hoffa"); string fullName; fullName = firstName + " " + lastName; cout << fullName << endl; cout << "First and last letters are:" << fullName[0] << " " << fullName[fullName.length() - 1] << endl; if (fullName == "jimmy hoffa") { cout << "Found !!!! "; } else { cout << "oh where oh where have you gone"; }

Call by Value, Reference, and Constant Reference typedef struct { int x; int y; } Coordinates; main() { int result; Coordinates coord1 = { 3, 3 }; result = Area(coord1); cout << coord1.x << " * " << coord1.y << " = " << result; } int Area(Coordinates coord) int Area(Coordinates &coord) int Area(const Coordinates &coord) { int temp; temp = coord.x; coord.x = 35; //Modify to show pass by val, ref, const ref semantics return (temp * coord.y); }

Call by Value, Reference, and Constant Reference Which of swap functions is appropriate? void swap(string a, string b) { string tmp = a; a = b; b = tmp; } void swap(const string &a, const string &b) { string tmp = a; a = b; b = tmp; } void swap(string &a, string &b) { string tmp = a; a = b; b = tmp; }

Example program: Rational Class Create a class to represent a rational number

Example program: Rational Class Create a class to represent a rational number This should allow for multiplication, division, addition, subtraction. Comparison (eg, equals) Printing out

First w/o Operator Overloading. Partial Rational.h class Rational { public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational Multiply(Rational rat) const; …. Fill in Divide, Add, Subtract, equals… void PrintRational(std::ostream &outstream) const; ~Rational(); private: int numerator; int denominator; void Reduce(); };

First w/o Operator Overloading. Partial Rational.cpp void Rational::Reduce() { int gcd = 1; for (int i = 2; i <= min(numerator, denominator); i++) { if (((numerator % i) == 0) && ((denominator % i) == 0)) { gcd = i; } if (gcd > 1) { numerator /= gcd; denominator /= gcd; }

First w/o Operator Overloading. Partial Rational.cpp Rational::Rational() { numerator = 0; denominator = 1; } Rational::Rational(int n, int d) { numerator = n; denominator = d; Reduce(); } int Rational::getDenominator() const { return denominator; } Rational Rational::Multiply(Rational rat) const { Rational tempRat; tempRat.numerator = numerator * rat.numerator; tempRat.denominator = denominator * rat.denominator; tempRat.Reduce(); return tempRat; } void Rational::PrintRational(ostream &outstream) const { outstream << numerator << " / " << denominator << endl; }

BELL RANG

Operator Overload Allowing for operators to work on classes in intuitive ways. Today we will cover Assignment: = Arithmetic: +, -, *, / Comparison: ==, !=,, = Input: > General rules for overloading Whenever the meaning of an operator is not obviously clear and undisputed, it should not be overloaded Always stick to the operator’s well-known semantics Always provide all out of a set of related operations Operators retain their precedence order

Overloading +,-,*,/ as member functions class Rational { public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator+(const Rational &rat) const; Rational operator-(const Rational &rat) const; ………. }

Overloading input/output > class Rational { friend std::ostream& operator<<(std::ostream &outStream, const Rational &rat); friend std::istream& operator>>(std::istream &inStream, Rational &rat); public: Rational(); Rational(int a, int b); int getNumerator() const; int getDenominator() const; Rational operator*(const Rational &rat) const; Rational operator/(const Rational &rat) const; Rational operator+(const Rational &rat) const; Rational operator-(const Rational &rat) const; ………. }

More C++ Fundamentals

Pointers 1.Pointer variablesint *p, *q; 2.Static allocationint x; 3.Address-of operatorp = &x; 4.Memory cell to which P points*p = 6; 5.Pointer operationsq = p; ??? pqx ?? pqx ?6 pqx 6 pqx