1 / 63

Hank Childs, University of Oregon

borka
Download Presentation

Hank Childs, University of Oregon

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 11: Transitioning to Classes Hank Childs, University of Oregon May 2nd, 2014

  2. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  3. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  4. Announcements: Late Passes • All projects on Blackboard have full score, even if late • I propose you all keep track of your own late passes, and tell me how you want to apply them at the end of the quarter. • Alternate: you can tell me now if you want to apply. • Not considered: I figure out optimal solution for you. We decided you will keep track of your own

  5. Announcements • No class Friday May 9th • Our lecture-during-discussion last week was the “makeup” for this missed lecture • Will do something similar later in May. • (Trip to Germany week 10)

  6. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  7. ASCII Character Set There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention image source: granneman.com

  8. signed vs unsigned chars • signed char (“char”): • valid values: -128 to 127 • size: 1 byte • used to represent characters with ASCII • values -128 to -1 are not valid • unsigned char: • valid values: 0 to 255 • size: 1 byte • used to represent data

  9. character strings • A character “string” is: • an array of type “char” • that is terminated by the NULL character • Example: char str[12] = “hello world”; • str[11] = ‘\0’ (the compiler did this automatically) • The C library has multiple functions for handling strings

  10. Useful C library string functions • strcpy: string copy • strncpy: string copy, but just first N characters • strlen: length of a string

  11. More useful C library string functions source: cplusplus.com

  12. References • Simplified version of a pointer. • Key differences: • You cannot manipulate it • Meaning: you are given a reference to exactly one instance … you can’t do pointer arithmetic to skip forward in an array to find another object • A reference is always valid • No equivalent of a NULL pointer … must be a valid instance

  13. References vs Pointers vs Call-By-Value ref_doubler and ptr_doubler are both examples of call-by-reference. val_doubler is an example of call-by-value.

  14. Different Misc C++ Topic: initialization during declaration using parentheses This isn’t that useful for simple types, but it will be useful when we start dealing with objects.

  15. Learning classes via structs • structs and classes are closely related in C++ • I will lecture today on changes on how “structs in C++” are different than “structs in C” • … at the end of the lecture, I will describe how classes and structs in C++ differ.

  16. Methods & Tally Counter • Methods and Functions are both regions of code that are called by name (“routines”) • With functions: • the data it operates on (i.e., arguments) are explicitly passed • the data it generates (i.e., return value) is explicitly passed • stand-alone / no association with an object • With methods: • associated with an object & can work on object’s data • still opportunity for explicit arguments and return value

  17. C++-style implementation of TallyCounter

  18. C-style implementation of TallyCounter

  19. Constructors • Constructor: method for constructing object. • Called automatically • There are several flavors of constructors: • Parameterized constructors • Default constructors • Copy constructors • Conversion constructors I will discuss these flavors in upcoming slides

  20. Note the typedef went away … not needed with C++. Constructor is called automatically when object is instantiated (This is the flavor called “default constructor”) Method for constructor has same name as struct

  21. Argument can be passed to constructor. (This is the flavor called “parameterized constructor”)

  22. Good questions from last lecture… • Can we use “using namespace <class>”?

  23. Good question from last lecture…

  24. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  25. Project 3A

  26. Accessing 2D Arrays 2D Element: (i, j) ==> 1D Element: i*width + j 1D Element: E <==> 2D Element: (E/width, E%width)

  27. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  28. Project 3B • Add useful routines for manipulating an image • Double in size, halve in size • Concatenate • Crop • Blend • Assigned: tonight • Due: Wednesday • Project 3C will be a biggie!

  29. Outline • Announcements • Review • Project 3A • Project 3B • Structs in C++ • Additional topics (if time)

  30. C++ • Reminder: almost every C program is a valid C++ program. • We now know C pretty well • So we are going to focus on new things in C++ • Classes • Templates • Operator overloading • Standard library • (will also talk about other changes)

  31. 3 Big changes to structs in C++ • You can associate “methods” (functions) with structs (we resume the discussion here, talking about constructors)

  32. Copy Constructor • Copy constructor: a constructor that takes an instance as an argument • It is a way of making a new instance of an object that is identical to an existing one.

  33. Constructor Types Default constructor Parameterized constructor Copyconstructor

  34. Example of 3 Constructors ????????????????

  35. Conversion Constructor

  36. 3 big changes to structs in C++ • You can associate “methods” (functions) with structs • You can control access to data members and methods

  37. Access Control • New keywords: public and private • public: accessible outside the struct • private: accessible only inside the struct • Also “protected” … we will talk about that later Everything following is private. Only will change when new access control keyword is encountered. Everything following is now public. Only will change when new access control keyword is encountered.

  38. public / private You can issue public and private as many times as you wish…

  39. The compiler prevents violations of access controls.

  40. The friend keyword can override access controls. • Note that the struct declares who its friends are, not vice-versa • You can’t declare yourself a friend and start accessing data members. • friend is used most often to allow objects to access other objects. This will compile, since main now has access to the private data member “count”.

  41. class vsstruct • class is new keyword in C++ • classes are very similar to structs • the only differences are in access control • primary difference: struct has public access by default, class has private access by default • Almost all C++ developers use classes and not structs • C++ developers tend to use structs when they want to collect data types together (i.e., C-style usage) • C++ developers use classes for objects … which is most of the time You should use classes! Even though there isn’t much difference …

  42. 3 big changes to structs in C++ • You can associate “methods” (functions) with structs • You can control access to data members and methods • Inheritance

  43. Simple inheritance example • Terminology • B inherits from A • A is a base type for B • B is a derived type of A • Noteworthy • “:” (during struct definition)  inherits from • Everything from A is accessible in B • (b.x is valid!!)

  44. Object sizes

  45. Inheritance + TallyCounter FancyTallyCounter inherits every bit of TallyCounter, and adds a new method: DecrementCount

  46. Virtual functions • Virtual function: function defined in the base type, but can be re-defined in derived type. • When you call a virtual function, you get the version defined by the derived type

  47. Virtual functions: example

  48. Virtual functions: example You get the method furthest down in the inheritance hierarchy

  49. Virtual functions: example You can specify the method you want to call by specifying it explicitly

  50. Access controls and inheritance B and C are the same. public is the default inheritance for structs Private inheritance: derived types don’t get access. Public inheritance: derived types gets access to base type’s data members and methods

More Related