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

Object-Oriented Design and Abstract Data Types, Papers of Computer Science

An introduction to object-oriented design (ood) and abstract data types (adts) in the context of object-oored programming (oop) in c++. Ood focuses on objects and their interactions, while adts define a group of data and operations associated with it. The concept of classes, which define objects and their operations, and the use of classes as a method of implementing an adt. It also explains how to perform operations using objects and the difference between classes and structs.

Typology: Papers

Pre 2010

Uploaded on 08/18/2009

koofers-user-7iu-1
koofers-user-7iu-1 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object-oriented design (OOD)
used in Object-oriented Programming (OOP)
focus is on objects (rather than functions)
object = an instance of an entity; same as a variable
solve the problem by identifying object in the solution & how they interact
Closer look at objects
oobjects are defined by “classes”
oclass = a structured data type including operations
oclasses define data types and operations
owe’ve already used classes & objects (without knowing it) to do I/O & use
string data type
othe iostream library file defines the classes istream and ostream
oThen within the library file, an object cin is defined to be of type istream
oan object cout is defined to be of type ostream
istream cin;
ostream cout;
oin the same way the fstream library defines classes ifstream and
ofstream
othen you can declare your own object of these classes in your program
othe string library defines a class called string
oyou can then create objects of the string class in your program
string firstName;
How do we perform operations using objects in C++?
oobjects have operations ( methods ) associated with them
othe syntax to call a function that is associated with an object is
<objectName>.<functionName>(<parameters>);
cin.get(someChar);
//void function
cout.setf(ios::fixed, ios::floatfield); //void function
length = firstName.length( ); //non-void function
oSome functions are designed in a special way to allow a
special operator to signal a function call
EX: cout << “hello!”; // the characters << are actually a
function call
// the string “hello!” is a parameter
pf3
pf4
pf5

Partial preview of the text

Download Object-Oriented Design and Abstract Data Types and more Papers Computer Science in PDF only on Docsity!

Object-oriented design (OOD)  used in Object-oriented Programming (OOP)  focus is on objects (rather than functions)  object = an instance of an entity; same as a variable  solve the problem by identifying object in the solution & how they interact  Closer look at objects o objects are defined by “classes” o class = a structured data type including operations o classes define data types and operations o we’ve already used classes & objects (without knowing it) to do I/O & use string data type o the iostream library file defines the classes istream and ostream o Then within the library file, an object cin is defined to be of type istream o an object cout is defined to be of type ostreamistream cin;ostream cout; o in the same way the fstream library defines classes ifstream and ofstream o then you can declare your own object of these classes in your program o the string library defines a class called string o you can then create objects of the string class in your program  string firstName;  How do we perform operations using objects in C++? o objects have operations ( methods ) associated with them o the syntax to call a function that is associated with an object is .();cin.get(someChar); //void functioncout.setf(ios::fixed, ios::floatfield); //void functionlength = firstName.length( ); //non-void function o Some functions are designed in a special way to allow a special operator to signal a function call  EX: cout << “hello!”; // the characters << are actually a function call // the string “hello!” is a parameter

Abstract Data Types (ADTs): Conceptual specification of a group of data and operations associated with the data. From the text: A data type whose properties (domain and operations) are specified independently of any particular implementation. Example: List ADT Data: item Operations: Create a list add and item remove an item search for an item count the items sort the list display all the items Notice that it’s separate from implementation details so this model can be used over & over again. It could be used to model a grocery list, a list of items needed for a chemistry lab, a list of operations that need to occur before the space shuttle takes off, … Once an ADT has been written, it is the programmer’s job to make implementation decisions. For a List we might use an array, of a specific maximum size and another integer to keep track of how many items are in the list.. EX: Clock ADT Data: Time Operations: Create a time object Set the time Display the time Increment the time by 1 second Compare times for equality Compare times for less than Note that all these operations are of different types: Constructor : an operation that creates a new instance of the ADT Transformer : Builds a new value of the ADT. Another way of looking at this is to say a transformer modifies an existing ADT. Observer : Allows us to examine the state of an instance without modifying it. Iterator : An operation that allows us to process - one at a time - all the components of a given ADT

Implementation file: something.cpp //for each function associated with your class returnType YourNewType:: functionName ( parameter list) { function body } EX: Write a class specification file for the Time ADT (don’t worry about how we’ll use it yet!) #include class Time { public: // See pages 547-549 for details void set(int hours, int min, int sec); void increment(); void write() const; bool equal( Time nother) const; bool lessThan ( Time nother) const; // Note that these last three functions are const member functions // guarantee that these will not modify a private data member private: int hrs; int mins; int secs; }; #include “time.cpp” ///==== time.cpp========================================== // === the implementation file for the Time ADT ======= // See pages 549- void Time::set( int hr, int mn, int sc) { } void Time::increment( ) { } void Time::write( ) { } bool Time :: equal( Time nother) const{ } bool Time:: lessThan( Time nother) const{ }

Using a Class as a Type in a program Consider the Time class that was created previously. The point of creating a user- defined structured data type is to allow someone to use it in a program. Time can be treated just like any other type in that variables can be declared to be of type Time. A variable whose type is defined by a class is said to be an object or instance of the class. In order to use Time as a type in our program, the compiler must “know” what the identifier Time is defined to include. EX: int main( ) { Time aTime; Using the instance to accomplish goals The instance of the class contains data (specified by the private members or instance variables ) and can call upon the methods of the class ( to perform operations. ) EX: int main ( ) { #include “clock.h” Time aTime; int h, m, s; cout << “Enter the initial time for the clock (hrs mins secs)” << endl; cin >> h >> m >> s; aTime.set(h, m, s); aTime.increment( ); aTime.write( ); return 0; } Why don’t the increment( ) and display( ) functions require parameters?