CS 330 Lecture 36 – Object-oriented Programming
Agenda
- what ?s
- the pillars of OOP
- hello, C++
- member initialization lists
- RAII
- overloading builtin operators
- << vs. printf
TODO
- Investigate object-oriented criticism by searching the internet. On a 1/4 sheet, identify at least three objections that people raise against it. Do you agree or disagree? (Remember, this is the internet, where thinking before typing is not expected. Don’t automatically believe each point is reasoned.) Cite your source URLs.
Note
We’ve trekked through the shell as a tool to program out interaction with the computer, regular expressions as a pattern language for recognizing structured text, the inner workings of an interpreter, assembly as the foundational language for talking with our machines, C as a pretty spare layer of abstraction over assembly, and the ability of functional programming to express a vast number of algorithms but leave little holes in them that we may customize their work according to our needs. Now it’s time to hit up object-oriented programming.
Our treatment of OOP begins with C++. Many modern languages support C++, but C++ was probably the first language with OOP to achieve widespread adoption.
Object-oriented programming is said to be founded on three pillars:
- encapsulation
- inheritance
- polymorphism
We will define this, but focus on encapsulation. Inheritance is great for library writers who must craft a hierarchy of types. Polymorphism is great for library users who want the library to process their types just as well as its own. On the ground, though, we spend far more time just organizing our own code into classes.
In our discussion, we’ll touch upon a variety of issues that are relevant to our study of programming languages:
- member initialization lists in C++ constructors
- references
- overloading builtin operators
- open classes
- Stroustrup’s rejection of printf
- resource acquisition is initialization (RAII)
Code
world.rb
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
from ./coderay:24:in `'
twoface.cpp
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information
from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem'
from ./coderay:24:in `'