Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Run time vs. Compile time
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Lecture 9 Concepts of Programming Languages
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
C++ fundamentals.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
OOP Languages: Java vs C++
Chapter 11 Abstract Data Types and Encapsulation Concepts.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Programming Languages and Paradigms Object-Oriented Programming.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright 2008 Oxford Consulting, Ltd 1 October C++ Classes Enhanced Structures C  struct only permitted to have data members  Functions to.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
ISBN Object-Oriented Programming Chapter Chapter
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
CHAPTER 7 Object Oriented Programming. Object-oriented program “Data and Operations go together was independently” is the main idea The promise of making.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Principles of programming languages 10: Object oriented languages
Abstract Data Types and Encapsulation Concepts
7.1 What Is An Object Object-oriented program - Description or simulation of application Object-oriented programming is done by adopting or extending an.
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
Chapter 5 Classes.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
Constructors and Destructors
Classes and Objects.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations

Constructs for Program Structuring n The programming with Procedures Modules Classes

Procedures n Raising the level of a Computation n Program design with Procedures Behavior Implementation

–Behavior In general the behavior of a construct is what is observable from outside the c onstruct Define procedure corresponding to what an algorithm does –Implementation The part that is accessible only from within that construct Hide the details of an algorithm in the procedure body

Example n The evaluation of expression ( ) * 2; It can be partitioned into Scanning l Scanning or Lexical analysis groups individual characters into tokens Parsing l Parsing or Syntax analysis uses the syntactic structure of the expression to determine its value.

CHARACTER STREAM ( ) * 2; TOKEN STREAM EXPRESSION VALUE Scanner Parser 50 lparen number 512 minus number 487 rparen times number 2 semicolon

Modules n A module is a collection of declarations, including both variables and procedures n A module serves as a black box with which the rest of the program interacts through an interface n Program design with Module Role Interface Implementation

– Role Modules are usually organized around data. If an operation affects some data, then the operation and the data may belong together in a module – Interface A subset of the declaration in the module Contain types, variables, and procedures The procedures in the interface determine the behavior of the module

– Implementation Consist of everything else about the module, including the code for procedures and for initialization Hide design decision in the private part of a module

Figure 6.2 Public and private view of two modules

Classes : User-defined data type n Design types corresponding to what the program manipulates n A term class is an abbreviation of “class of objects” n An object is a run-time entity with data on which operations can be performed

Comparison of Procedures, Modules and Classes n Serve distinct needs n can be used in combination with each other n procedures - needed to implement operations in a module or class n module - used to statically partition the source text of program with classes n differences in not in activity (the procedures)but in the organization

Procedures n Define all the details n all the representation is known throughout the program n entries are represented as a pointer to a record containing the data

Modules n Hide the representation n provide public operations n advantage over procedures is that the representation is hidden and access is checked n hiding and checking contribute to feel of entries as object rather than pointers

Defined Types n As with modules, the representation is hidden, access is checked n in addition, objects are initialized upon creation

Program Organization n Procedures at the same level –a sequence of useful procedures –procedures can call each other Figure 6.6

n Nested procedures –An attempt to manage programs with many procedures –The idea is to declare a procedure inside another –To declare functions close to where they are used Figure 6.7

n Modules –a collection of data and related actions –a grouping of declarations, which can include types, variables, and procedures Figure 6.8

CHAPTER 6: PART II 6.5 CLASS DECLARATIONS IN C DYANAMIC ALLOCATION IN C TEMPLATES: PARAMETERIZED TYPES 6.8 IMPLEMENTATION OF OBJECTS IN C++

6.5 CLASS DECLARATIONS IN C++ n C++: User-defined types receive the same support as built-in types. n Classes = generalization of records called structures (grouping of data). n Both data and functions can be structure members. n Variables can be declared and objects created in C++ classes.

STRUCTURE OR CLASS n Structure vs. Class = Public vs. Private n All members of a structure are public. n All members of a class are private. n Take note: These are by default!!!! n A structure is a special case of a class of the same name.

DECLARATION n Keyword: struct n Declarations enclosed in braces {}. n Declarations in braces are members.

SAMPLE CODE struct Stack{ int top; char elements[101]; char pop( ); void push (char); Stack ( ); }; n 2 variables –top- data member that holds an integer –elements- data member that holds an array of characters n 3 functions –pop- takes no parameter and returns a character –push- takes character and returns nothing –Stack- constructor

Constructors & Destructors n Stack ( ); n …………………….. n ~Stack ( ); n Constructors and destructors are parameterless functions. n Same name as class. n Called automatically: –constructors initialize –destructors clean up

MEMBER NAMES AND FUNCTION CODE struct Stack{ int top; char elements[101]; char pop( ); void push (char); Stack ( ) { top = 0; } }; char Stack::pop ( ); top = top - 1; return elements[top+1]; } void Stack::push (char c) { top = top + 1; elements[top] = c; } n Full member name: :: –full names needed to make explicit that belong to class if used outside of class declaration n If class name is known from context, use only the member name.

CLASS NAMES AS DEFINED TYPES #include main( ) { Stack s; s.push(‘!’); s.push(‘#”); printf(“%c %c %c\n”, s.pop( ), s.pop( ), s.pop( )); } n After declared, a class name can be used to declare variables. n Dot notation refers to members of an object. n C struct Complex x; n C++ Complex x;

INITIALIZATION WITH PARAMETERS struct Complex { float re; float im; Complex(float r, i) {re =r; im = i; } }; ……………………….. Complex x(1, 2) n Initialization of constructor may require passing of parameters. n Here declaration takes 2 parameters. n Declares and initializes complex # x to the parameters in ( ).

OVERLOADED FUNCTION NAMES struct Complex { float re; float im; Complex(float r) {re = r; im = 0; } Complex(float r, i) {re =r; im = i; } }; n Functions can be overloaded (including constructors). n Must differ by number and/or type of parameters in call.

ACCESSIBITLITY: PUBLIC, PRIVATE, PROTECTED n Privacy and access class based. n Access to members restricted through keywords (apply to outside class: in class have access to all objects in same class). n Public members are accessible to outside code. n Private members are accessible to member functions in the class declaration. Accessible to all objects of class. n Protected members like private members except for derived classes, visible through inheritance to derived classes.

PUBLIC vs. PRIVATE Class Stack { public: Stack ( ); char pop( ); void push(char); private: int top; char elements[101]; }; n Here the member variables are hidden (private). n By default: –struct = public –class = private

6.6 DYNAMIC ALLOCATION IN C++ n Three ways to create objects in C++: –variable declarations –dynamically through new –as static objects

NEW, DELETE n new T; n delete p; n Objects created by new exist until destroyed by delete. n New creates object of type T and returns pointer. n Delete destroys object pointed to.

POINTERS TO OBJECTS n Prefix * is a pointer-deferencing operator. n Cell * p; reads as “we get an object of type cell when we apply * to p.” n Summary of pointer notations: –p->info = member info of object pointed to by p. –0 = null pointer, pointing to no object. –this = used within member functions to point to this object itself.

DYNAMIC ALLOCATION USING CONSTRUCTORS AND DESTRUCTORS Class Stack { int top; char * elements; int size; public: void push(char); char pop( ); Stack (int); ~Stack( ); }; n Here the size of the array is a parameter. n Since dynamically allocated storage is taken off the heap it needs a destructor to explicitly deallocate (release) it.

CELLS AND LINKED LISTS class Cell { int info; Cell * next; Cell(int I) {info = I; next = this;} Cell(int I, Cell*n) {info = I; next = n;} friend class List; }; n Friends –friend declaration within a class gives nonmember functions access to private members of the class n Cell constructors –empty list is a cell that points to itself –new Cell c(0) or –next = this

LINKED LISTS class List { Cell * rear; public: void put(int); void push(int); int pop( ); int empty( ) {return rear == rear->next; } List( ) {rear = new Cell(0);} ~List( ) {while(!empty( )) pop( );} }; n Uses constructors from class Cell (because of friend declaration) n Initializes new list (circularly linked cell) n push adds to front n put adds to back n pop returns value of front and deletes cell

6.7 TEMPLATES: PARAMETERIZED TYPES n Data structures are containers that hold objects. n Operations on a container can be defined for any type. n Type parameters denote the type of element in the container. –Template

6.8 IMPLEMENTATION OF OBJECTS IN C++ n Objects laid out like records. (class = structure containing data members) n Pointers used to access indirectly. n Functions can be expanded in-line to reduce function call overhead. n Class declaration assists with checking restrictions and in-line expansion at compile time.

Take you C assignment and translate it directly into C++ using “BIN” objects. See page 246 in PL book for stack object example. Due 10/12/2000