Download presentation
Presentation is loading. Please wait.
Published byIsabel Miles Modified over 9 years ago
1
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE 2. 150107. CARRANO CH1, C++ INTERLUDE 1
2
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.
3
Why C++ Object Oriented Programming (OOP)? Abstraction Encapsulation Hierarchy Polymorphism
4
Encapsulation
5
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
6
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)
7
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()
8
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; };
9
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: }
10
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
11
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
12
C++ Fundamentals let’s code…
13
C++ Program Dev Lifecycle http://www.technovisitors.com/2014/06/C-program-execution-life-cycle.html
14
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."; }
15
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"; }
16
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); }
17
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; }
18
Example program: Rational Class Create a class to represent a rational number
19
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
20
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(); };
21
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; }
22
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; }
23
BELL RANG
24
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
25
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; ………. }
26
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; ………. }
27
More C++ Fundamentals
28
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 1 3 4 5
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.