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

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

Embed Size (px)

Citation preview

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

CSS 342DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I

LECTURE 2. 150107.

CARRANO CH1, C++ INTERLUDE 1

Page 2: CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 2. 150107. 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.

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

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

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

Encapsulation

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

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.

Implementationof method S

Programthat uses method S

Function call with arguments

Return a value

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

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

Programthat uses a class

Examples:- student lists- rational numbers- complex numbers- currency (cents/dollars)- length measurement (inches/feet)- weight measurement (oz/lbs)

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

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()

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

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;};

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

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:}

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

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

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

Computer Scientist of the weekEdsger 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

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

C++ Fundamentalslet’s code…

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

C++ Program Dev Lifecycle

http://www.technovisitors.com/2014/06/C-program-execution-life-cycle.html

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

IO: Console#include <iostream>using namespace std;

{ int age;

cout << "hello world. How old are you (in years)?\n"; cin >> age; cout << "that is " << (365 * age) << " days.";}

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

string http://www.cprogramming.com/tutorial/string.html 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";}

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

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);}

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

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;}

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

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

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

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

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

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(); };

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

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; } }

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

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;}

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

BELL RANG

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

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

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

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; ………. }

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

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; ………. }

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

More C++ Fundamentals

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

Pointers1. Pointer variables int *p, *q;

2. Static allocation int x;

3. Address-of operator p = &x;

4. Memory cell to which P points *p = 6;

5. Pointer operations q = p;

? ? ?

p q x

? ?

p q x

? 6

p q x

6

p q x

1 3 4

5