Hank childs university of oregon
Download
1 / 63

Hank Childs, University of Oregon - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Hank Childs, University of Oregon' - borka


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Hank childs university of oregon

CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/

Lecture 11:

Transitioning to Classes

Hank Childs, University of Oregon

May 2nd, 2014


Outline
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


Outline1
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


Announcements late passes
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


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


Outline2
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


Ascii character set
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


Signed vs unsigned chars
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


Character strings
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


Useful c library string functions
Useful C library string functions

  • strcpy: string copy

  • strncpy: string copy, but just first N characters

  • strlen: length of a string



References
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


References vs pointers vs call by value
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.


Different misc c topic initialization during declaration using parentheses
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.


Learning classes via structs
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.


Methods tally counter
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




Constructors
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


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


Argument can be passed to constructor.

(This is the flavor called “parameterized constructor”)


Good questions from last lecture
Good questions from last lecture…

  • Can we use “using namespace <class>”?



Outline3
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)



Accessing 2d arrays
Accessing 2D Arrays

2D Element: (i, j) ==> 1D Element: i*width + j

1D Element: E <==> 2D Element: (E/width, E%width)


Outline4
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


Project 3b
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!


Outline5
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


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)


3 big changes to structs in c
3 Big changes to structs in C++

  • You can associate “methods” (functions) with structs

    (we resume the discussion here, talking about constructors)


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


Constructor types
Constructor Types

Default constructor

Parameterized

constructor

Copyconstructor


Example of 3 constructors
Example of 3 Constructors

????????????????



3 big changes to structs in c1
3 big changes to structs in C++

  • You can associate “methods” (functions) with structs

  • You can control access to data members and methods


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


Public private
public / private

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



The friend keyword can override access controls
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”.


Class vs struct
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 …


3 big changes to structs in c2
3 big changes to structs in C++

  • You can associate “methods” (functions) with structs

  • You can control access to data members and methods

  • Inheritance


Simple inheritance example
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!!)



Inheritance tallycounter
Inheritance + TallyCounter

FancyTallyCounter inherits every bit of TallyCounter, and adds a new method: DecrementCount


Virtual functions
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



Virtual functions example1
Virtual functions: example

You get the method furthest down in the inheritance hierarchy


Virtual functions example2
Virtual functions: example

You can specify the method you want to call by specifying it explicitly


Access controls and inheritance
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


One more access control word protected
One more access control word: protected

  • Protected means:

    • It cannot be accessed outside the object

      • Modulo “friend”

    • But it can be accessed by derived types

      • (assuming public inheritance)


Public private protected
Public, private, protected

* = with public inheritance


More on virtual functions upcoming
More on virtual functions upcoming

  • “Is A”

  • Multiple inheritance

  • Virtual function table

  • Examples

    • (Shape)


Outline6
Outline

  • Announcements

  • Review

  • Project 3A

  • Project 3B

  • Structs in C++

  • Additional topics (if time)


Memory errors
Memory Errors

  • Array bounds read

  • Array bounds write


Memory errors1
Memory Errors

  • Free memory read / free memory write

When does this happen in real-world scenarios?


Memory errors2
Memory Errors

  • Freeing unallocated memory

When does this happen in real-world scenarios?

Vocabulary: “dangling pointer”: pointer that points to memory that has already been freed.


Memory errors3
Memory Errors

  • Freeing non-heap memory

When does this happen in real-world scenarios?


Memory errors4
Memory Errors

  • NULL pointer read / write

  • NULL is never a valid location to read from or write to, and accessing them results in a “segmentation fault”

    • …. remember those memory segments?

When does this happen in real-world scenarios?


Memory errors5
Memory Errors

  • Unitialized memory read

When does this happen in real-world scenarios?


Outline7
Outline

  • Announcements/Review

  • Grade project 2D

  • Building Large Projects

  • Mangling with C/C++

  • Project 3A

    • Background on images

  • Bonus topics

    • Memory Errors

    • Webpages


Web pages
Web pages

  • ssh –l <user name> ix.cs.uoregon.edu

  • cd public_html

  • put something in index.html

  •  it will show up as

    http://ix.cs.uoregon.edu/~<username>


Web pages1
Web pages

  • You can also exchange files this way

    • scpfile.pdf <username>@ix.cs.uoregon.edu:~/public_html

    • point people to http://ix.cs.uoregon.edu/~<username>/file.pdf

Note that ~/public_html/dir1shows up as http://ix.cs.uoregon.edu/~<username>/dir1

(“~/dir1” is not accessible via web)


ad