In Introduction to Object Oriented Programming in C
In Introduction to Object Oriented Programming in C++ James Skon
Objectives To understand the importance of reducing perceived complexity. Understand the concept of abstraction and modeling To learn the basics of class construction and use in C++
Making a phone call. . . What specific steps do you take when you need to make a call with your cell phone?
Making a phone call What really happens when you make a cell phone call?
Making a cell phone call The cell phone must find a tower and negotiate a connection. Cell phone must authenticate with tower. If tower is not owned by your carrier the credentials must be exchanged between the towers carrier and you carrier. A specific channel must be allocated (both a frequency and a slice within that frequency for the phone to communicate over The phone must digitize the analog audio signal into a digital signal, then convert that back into an analog signal for transmission. …. (much more)
What's the point? A phone “hides” the details of it's operation from the user. Why?
Complexity The idea is that the phone user only needs to think about “what” they want to do, not “how” it is done. A cell phone, along with the system that supports it, is truly one of the most complex systems ever build by mankind, yet a call can be made by a child!
Complexity It has been said that practical computer science is fundamentally about managing complexity The art of programming is the art of organizing complexity, of mastering multitude and avoiding its bastard chaos as effectively as possible. Edsger W. Dijkstra
Complexity The competent programmer is fully aware of the strictly limited size of his own skull; therefore he approaches the programming task in full humility, and among other things he avoids clever tricks like the plague. Edsger W. Dijkstra
Complexity Thus, our goal as programmers is to find a way to manage complexity. It's not about reducing the actual complexity in the system (that can't be avoided), rather it's about reducing the perceived complexity by the designer and implementer. Humans can't build and operate systems they can't understand! But how do we manage complexity?
Models and Abstraction The cell phone provides a “model” of the act of communication, while hiding the complexity. This model is called an “abstraction”. We see abstractions everywhere: A steering wheel is an abstraction that allows one to guide a vehicle without thinking about how it works. Using a credit card is an “abstraction” of transferring money between two financial organizations. Can you think for more?
Abstraction in programming Programming is all about creating a simplified “model” of something we need to represent. The goal is to find a model the represents exactly, and only, those facets of the problem we care about. This model is called an abstraction.
Abstraction Example – exam schedule Suppose we wish to create a schedule of final exams so that students don't have time conflicts. We could look at every students schedule, and through trial and error try to find a solution. This could get complicated. The problem is there are just to many details to manage. Or we could create a simplified model of the problem with a graph, connecting classes (nodes) with a line that is present if a student is in both of those classes.
Course conflict map Drama Lit CS Math Phil This is a model of the situation, an abstraction that only includes what we care about, conflicts. . .
Course conflict map use We can now solve the exam-scheduling problem by repeatedly finding and removing the largest set of classes that collectively have no common students. We then iteratively remove the largest set of classes with no common students (no interconnecting lines)
Finding an exam schedule Set one Drama Lit Set two CS Math Phil Math Set three CS Drama
Finding an exam schedule Set one Phil Time Slot 1 Phil, Lit, Drama Time Slot 2 Math Time Slot 3 CS Lit Set two Math Set three CS Drama
Models and Abstractions By creating a model with only the right the information, we were able to solve a problem without thinking about irrelevant details What the actual times were Who was in each class Where the classes met Who teaches the classes Etc.
Models and Abstractions Computer Science is all about creating models, and we use the notion of abstraction to create a model where we only have to think about those things we care about at the moment. This has the effect of reducing perceived complexity.
Hiding complexity in C++ How can a programming language like C++ allow us to hide complexity? How can a programming language like C++ allow us to build models which are appropriate abstractions of the parts of a problem we care about? Objects and Classes
Classes and Objects In programming a class is a model of something we wish to represent. It is an the design of a model. An object is an instance of a class. It is the realization of the design. Consider something simple – we want to represent 2016 presidential candidates What what do we need to know about candidates? What do we need to do with candidates?
A class – the design A class has two parts: The interface – this is the part that a user sees The implementation – this is the internal part that only the creators needs to understand. Implementation The Interface Seen by public Unseen (private)
An Object - Instances i. Phone (Class) Bill's i. Phone James's i. Phone Ann's i. Phone
2020 presidential candidates Information (know) Actions (do) Name Create candidates Party Set Polls polls Display Candidate information
Class Diagram Name Variables Operations
Candidate Class Name Variables Operations
Class and Objects Class design Objects Instances
Class and Objects
Objects in use
Candidate Interface class Candidate { private: string name; string party; private This is the internal representation int polls; public: Candidate(string a. Name, string a. Party, int a. Poll); void set. Polls(int poll); void print(); }; public This is the external interface
Candidate Implementation Candidate : : Candidate(string a. Name, string a. Party, int a. Poll) { name = a. Name; party = a. Party; polls = a. Poll; } Constructor Initializes a new instance of the candidate class
Candidate Implementation // If the object variables and parameters have the same name. Candidate : : Candidate(string name, string party, int polls) { this -> name = name; this -> party = party; this -> polls = polls; } Constructor Initializes a new instance of the candidate class
Class use int main() { Candidate h(”Bernie Sanders", "Democrat", 46); Objects Each of these is an object that is an Candidate t(”Donald Trump", "Republican", 42); instance of the class Candidate h. print(); t. print(); h. set. Polls(37); t. set. Polls(43); h. print(); j. print(); } Name : Hillary Clinton Party : Democrat Polls : 46% Name : Donald Trump Party : Republican Polls : 42% Name : Hillary Clinton Partymethods : Democrat A method is applied Polls : 37% to an: object by Trump Name Donald object. method(); Party : Republican Polls : 43%
Classes in use
Classes in use: interface Interface class Candidate { private: string name; string party; int polls; public: // Constructor Candidate(string a. Name, string a. Party, int a. Poll); // Mutator void set. Polls(int poll. Num); // Accessor string get. Name(); string get. Party(); int get. Polls(); void print(); };
Classes in use: implementation Implementation #include Candidate. h Candidate : : Candidate(string name, string party, int polls) { this -> name = name; this -> party = party; this -> polls = polls; } void Candidate : : set. Polls(int polls) { this -> polls = polls; } void Candidate : : print(){ cout << "Name : " << name << endl; cout << "Party : " << party << endl; cout << "Polls : " << polls << "%" << endl; }
Classes in use Main #include Candidate. h int main() { Candidate h(”Bernie Sanders", "Democrat", 46); Candidate j(”Donald Trump", "Republican", 42); h. print(); j. print(); h. set. Polls(37); j. set. Polls(43); h. print(); j. print(); }
Now you try Go to: https: //www. onlinegdb. com/edit/Hy. BCRc. Huf Change the polls to new values. 1) Add 2 new candidates of your choice 2) Modify the class to also store the candidates age. 3) Create a method “birthday()” that increases the candidates age. 4) Challenge: create an array of candidates, and a loop that prints the values of each.
Codepad. org With codepad you can copy my code and “create a new paste” Paste the code in, edit it, and try it. It seems you cannot edit code in place, you must always “create a new paste” and try it for every edit.
Summary The software developer's first and foremost task is to manage complexity. Abstraction provides a way to separate function from implementation, thus hiding complexity, while exposing needed capabilities. The task becomes one of finding the appropriate abstraction, and designing it's interface and it's implementation.
Summary A C++ Class has two parts, and interface and an implementation. A program includes the interface in order to create and use instances of the class. The user of the class does not need to see or understand the implementation of the class, only it's interface. The interface provides a abstraction of the thing we wish to model.
Now you try Go to: https: //www. onlinegdb. com/edit/Hy. BCRc. Huf Change the polls to new values. 1) Add 2 new candidates of your choice 2) Modify the class to also store the candidates age. 3) Create a method “birthday()” that increases the candidates age. 4) Challenge: create an array of candidates, and a loop that prints the values of each.
- Slides: 42