Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Class Review: Procedural & Data Abstraction with Product and Time Classes, Study notes of Humanities

A class review on procedural and data abstraction concepts in c++. Through examples of product and time classes, it explains the importance of encapsulation, information hiding, and user-defined data types. Students will learn how to create abstract data types using classes, abstract an action using functions, and simplify programs by discovering classes.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-tzo-1
koofers-user-tzo-1 🇺🇸

10 documents

1 / 14

Toggle sidebar

Related documents


Partial preview of the text

Download C++ Class Review: Procedural & Data Abstraction with Product and Time Classes and more Study notes Humanities in PDF only on Docsity! Class review [Bono] 1 Review of classes • Review of procedural abstraction • Discovering classes • Example class definition • Information hiding (encapsulation) • Member function definitions * Product and Time examples and code are from Big C++, by Cay Horsmann and Timothy Budd; Wiley 2005 Class review [Bono] 2 Procedural abstraction • An example of a procedural abstraction: – interface: // reverseString: s’ has the same characters as // s, but with its characters in reverse order. void reverseString(string &s); – Possible implementations? Class review [Bono] 5 Data abstraction • Just like we can add operations with procedures, we can add user-defined data types, called abstract data types (or ADTs) • An ADT defines a set of possible values + a set of valid operations on the type. • Example: string – values are sequences of characters – operations are size, <, ==, =, +=, etc. • The language mechanism for ADTs in C++ is class – Function abstracts an action (operation) – Class abstracts a thing (object) Class review [Bono] 6 Discovering Classes • If program has a bunch of related variables that all refer to the same concept, think about the concept as a whole • Example from Big C++ – Computer has name, price, and score – Need to find model (out of a bunch of models) with highest score / price ratio (best value) – Code to do this with various data values: Requires 6 variables. (see bestval.cpp) – Better to abstract the concept: Product – Using Product class simplifies the program (see product1.cpp) Class review [Bono] 7 Discovering classes (cont.) • Now, suppose you need to sort all the computer data by value (i.e., score / price). • In old representation, instead of triplets of values now we would need three different vectors of values, and have to associate ones with same index: – E.g. name[i], score[i], price[i] • More natural to define one vector of Products. All the data about one product is in one element of the vector (i.e., one object). Class review [Bono] 10 Encapsulation • Information hiding – implementations of member functions not part of interface – also, data members are not part of interface. • Always put data in private section. • What is rationale? • Discuss with respect to an example class… Class review [Bono] 11 Time Class A class for representing a time of day (between 00:00:00 and 23:59:59). Code from Big C++ • Time t; Constructs time object "t" with the current time. • Time(hours, minutes, seconds) Constructs the time with hours, minutes, and secs. • t.get_seconds() Returns the seconds value of t. • t.get_minutes() Returns the minutes value of t. • t.get_hours() Returns the hours value of t. • t.add_seconds(n) Changes t to move by n seconds. n may be negative • t.seconds_from(t2) Computes the number of seconds between t and t2. Class review [Bono] 12 Why encapsulation? • Can guarantee that the object data cannot be put into an incorrect state. – E.g. can’t make a time with 25 hours in it by mistake: Time liftoff(19, 30, 0); . . . liftoff.hours = liftoff.hours + 6; // oops, not a legal time – In contrast: liftoff.add_seconds( 6*MIN_PER_HOUR*SEC_PER_MIN);
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved