Classes and Abstraction
- This is chapter 12, you should start reading it.
- We are about to make a major shift in our approach.
- Up to now, we have been working on Procedural Programming.
- In this method of programming, we try to create a function to "solve" the program.
- We may break this into smaller functions.
- But we focus on the algorithm.
- This type of programming is tightly coupled with functional decomposition.
- This type of programming works well for small and medium scale projects.
- But it falls apart as the size of a project increases.
- The thing we are switching to is object oriented.
- This focuses less on algorithms and more on "objects"
- This uses a bottom up design
- You think of the little pieces that you will build your program out of.
- Then how these will combine to build larger pieces...
- A major item in Object oriented is abstraction.
- By abstraction, we mean separating the essential qualities of an idea or object from the details of how it works.
- A basic definition you must know is the abstract data type.
- An Abstract Data Type(ADT) is a data type whose properties ((domain and operations) are specified independently from any particular implementation.
- Think about integers.
- What is the domain? (ie the things that are integers)
- What are the operations? (IE What can you do with integers)
- How are these implemented?
- Do the same with floats.
- Do the same with strings.
- We want to implement things as an ADT
- By providing the domain and operations programmers know what they can do with our data.
- But they don't need to be worried about how to do it.
- Think about a book.
- What are the properties of a book?
- What are the operations you can perform on a book?
- It depends on what we are doing with our book object.
- What if we are dealing with a library and we only care about books as things in our library?
- What if we are dealing with reading books we might worry about pages, words, chapters, ...
- With this, we start thinking about things in a whole new way.
- It is more natural to think about a book then the algorithms you might perform on a book.
- If we have a book object, what else might we want to build?
- A Book List or Book Inventory?
- Some OOP terms
- The basic unit in object oriented programming is an object.
- Objects instances of a class.
- A class is the way c++ implements a ADT
- Like a struct, a class is a blueprint for a data holding object.
- Classes have
- Member data (the domain)
- Member functions (the operations)
- When a variable of the class is created an object is instantiated or an instance of the class is created.
- The terms object and instance are synonymous.
- How are classes different from structs?
- Structs hold only data.
- Classes hold both data and functions.
- Structs have a lose association for the data.
- Classes the data and functions that are all related, tightly, to the class.
- The information is structures is available and the variables can be accessed directly
- In a class, the information is "hidden" and the user needs to use the functions to access and change the data.
- When designing objects you try to think about "what defines this object and what does this object do?" rather than "what can I do to this data?"
- A few more terms
- Encapsulation: Designing a class so that its attributes are isolated from the actions of external code except through a formal interface.
- Information hiding: The encapsulation and hiding of implementation details to keep the client of an abstraction from depending or incorrectly manipulating these details.
- Abstraction The separation of logical properties (interface and specification) of an object from its implementation (internal data representation and algorithms)