Unformatted text preview:

Lecture 7OutlineAbstract Data Types 1Abstract Data Types 2Throttle ADT 1Throttle ADT 2Throttle ADT 3Throttle ADT 4C++ Classes 1C++ Classes 2C++ Classes 3Slide 12Throttle Class 1Using a Class 1Using a Class 2Throttle Class 2Throttle Class 3Throttle Class 4Separate Compilation 1Separate Compilation 2Separate Compilation 3Separate Compilation 4Separate Compilation 5Separate Compilation 6Separate Compilation 7In-class Exercise 1In-class Exercise 2In-class Exercise 3Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 1Lecture 7Log into LinuxNew documents posted to course webpageCoding style guideline; part of project grade is following thisHomework 4, due on Monday; this is a written assignmentProject 1, due next WednesdayReminder: Homework 3 due today by 4:30pmQuestions?Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 2OutlineAbstract data types (ADTs)C++ classesClass definitionClass usageClass implementationSeparate compilation (Section 2.3)Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 3Abstract Data Types (ADTs)A data structure is a systematic way of organizing and accessing data.We use an abstract model that specifies the type of data stored and legal operations on the data.It is called abstract because this view is implementation independent. We can use an ADT just knowing what it does and not how it does it.Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 4Abstract Data Types (ADTs)With this abstract view is important work can be split between those that write the data structure and those that write the application using the data structure, allowing both to go on simultaneously.The design an ADT has two partsDescriptions of operations that act on the data (name, return type, parameters, preconditions, postconditions)Description of the data being stored (name, type, variable/constant)Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 5Example: Throttle ADTThrottle ADT (from the textbook) models the behavior of real-world throttles. For example, an engine might have a throttle with positions 0 (off) to 6 (full open) that controls fuel flow.Start design with question: what do we want to be able to do with a throttle? The answers form the list of operations.Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 6Throttle ADTOperations can be classified by whether they change the status of their object.Operations that change the throttle status (mutators):Set the throttle to the shutoff positionShift the throttle's position by a given amountWednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 7Throttle ADTOperations that examine the throttle status (accessors):Find out the current fuel flow expressed as a proportion of the maximum flowFind out whether the throttle currently is onAlso, special operations that create a throttle object (constructors):Create a throttle object initialized to position 0Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 8Throttle ADTOnce we have a good idea of what we want a throttle object to do, we ask: what data is needed to support the operations? This data is called the attributes.position: an integer with values 0-6 representing the throttle positions; 0 is off, 6 is full openWednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 9C++ ClassesThe C++ class construct is used to implement an ADT. It allows the attributes and the operations of an ADT to be encapsulated in one entity called an object.Conventionally (and for supporting reuse), a class is implemented in two parts:Header file containing the class definitionSource file containing the function definitions for the operations.Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 10C++ ClassesA C++ class definition has similar syntax to a C struct definition.class <ClassName>{ public: // Operation prototypes go here // Also called member functions private: // Attribute declarations go here // Also called data members}; // the ; is requiredWednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 11C++ ClassesIn C++, both struct and class names become type names (without using typedef). The keywords public: and private: are used to control the access to the items that follow.Public means access is granted to all other code. Typically is it used for the operations of an ADT.Private means access is restricted to the operations of the ADT. Typically is it used for the attributes of an ADT and any internal helper functions.This use allows for information hiding.Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 12C++ ClassesA constructor is an operation that creates an object of the class.A constructor's name is the same as the class name. It does not return an object. There may be more than one constructor as long as they have different parameter lists.A constructor with no parameters is called the default constructor and is called when a class variable is declared without arguments.A constructor with parameters that are used to initialize the object state is called an explicit-value constructor and is called when a class variable is declared with arguments.Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 13Example: Throttle Classclass throttle // Textbook example{ public: // Operation prototypes // Constructor-same name as class; no return type throttle (); // Default constructor // Mutators void shut_off (); void shift (int amount); // Accessors-note the const keyword double flow () const; bool is_on () const; private: // Attribute declarations int position;}; // end throttle classWednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 14Using a ClassOnce the class definition is written, an application can use the class without the operation implementations.The class name is a type, so declare as usualthrottle example; // new throttle at position 0The operations are called using dot ('.') notation (like accessing struct fields).example.shift (3); // go from position 0 to 3cout << "The flow is " << example.flow() << endl;Wednesday, January 26 CS 215 Fundamentals of Programming II - Lecture 7 15Using a ClassHere is one way to think


View Full Document

UE CS 215 - Lecture 7

Documents in this Course
Lecture 4

Lecture 4

14 pages

Lecture 5

Lecture 5

18 pages

Lecture 6

Lecture 6

17 pages

Lecture 7

Lecture 7

28 pages

Lecture 1

Lecture 1

16 pages

Lecture 5

Lecture 5

15 pages

Load more
Download Lecture 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?