C++ Programming Languages

Slides:



Advertisements
Similar presentations
Chapter 19 Vectors, templates, and exceptions Bjarne Stroustrup
Advertisements

Chapter 18 Vectors and Arrays
1.00 Lecture 37 A Brief Look at C++: A Guide to Reading C++ Programs.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Chapter 18 Vectors and Arrays John Keyser’s Modification of Slides by Bjarne Stroustrup
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.
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:
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
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.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles C/C++ Emery Berger and Mark Corner University of Massachusetts.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 C ++ -overview Department of Computer Engineering 1.
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
OOP Languages: Java vs C++
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object Oriented Data Structures
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
C++ Programming. Table of Contents History What is C++? Development of C++ Standardized C++ What are the features of C++? What is Object Orientation?
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
C++ Overview Eileen Kraemer. C++ Overview C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80’s  The original cfront translated C++
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
Principles of programming languages 10: Object oriented languages Isao Sasano Department of Information Science and Engineering.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
©Copyrights 2016 Eom, Hyeonsang All Rights Reserved Computer Programming Object Oriented Programming & C++ 1 st Lecture 엄현상 (Eom, Hyeonsang) Department.
FASTFAST All rights reserved © MEP Make programming fun again.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Principles of programming languages 10: Object oriented languages
How to be generic Lecture 10
Abstract Data Types and Encapsulation Concepts
Object-Oriented Programming (OOP) Lecture No. 45
A First C++ Class – a Circle
Object-Orientated Programming
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
CS212: Object Oriented Analysis and Design
Some examples.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Abstract Data Types and Encapsulation Concepts
Abstract Data Types Stacks CSCI 240
Lecture 9 Concepts of Programming Languages
Presentation transcript:

C++ Programming Languages Sharif University of Technology C++ Programming Languages Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 C++-overview Department of Computer Engineering

Outline C++ Overview C++ Design Goals C++ Enhancements Language Features Not Part of C++ Data Hiding Implementation in C Data Abstraction Implementation in C Data Abstraction Implementation in C++ Exception Handling Implementation in C++

C++ Overview C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80’s nearly 30 years ago! C++ is compatible extension of C that provides: Stronger type-checking Support for data abstraction Support for object-oriented programming Support for generic programming

C++ Design Goals As with C, run-time efficiency is important Unlike other languages (e.g., Ada, Java, C#, etc.) complicated run-time libraries and virtual machines have not traditionally been required for C++ Note, that there is no language-specific support for concurrency, persistence, or distribution in C++ Compatibility with C libraries & traditional development tools As close to C as possible, but no closer

C++ Enhancements C++ supports data abstraction & encapsulation e.g., the class mechanism & name spaces C++ supports object-oriented programming features e.g., abstract classes, inheritance, & virtual methods C++ supports generic programming e.g., parameterized types C++ supports sophisticated error handling e.g., exception handling C++ supports identifying an object’s type at runtime e.g., Run-Time Type Identification (RTTI)

Language Features Not Part of C++ Concurrency Persistence Garbage Collection Distribution

Stack Example typedef int T; #define MAX_STACK 100 /* const int MAX_STACK = 100; */ T stack[MAX_STACK]; int top = 0; T item = 10; stack[top++] = item; // push ... item = stack[--top]; // pop Obviously not very abstract...

Data Hiding Implementation in C /* File stack.h*/ /* Type of Stack element. */ typedef int T; /* Stack interface. */ int create (int len); int destroy (void); void push (T new_item); void pop (T *old_top); void top (T *cur_top); int is_empty (void); int is_full (void);

Data Hiding Implementation in C /* File stack.c */ #include "stack.h" static int top, size; /* Hidden within this file. */ static T *stack; /* Hidden within this file. */ int create (int len) { top = 0; size = len; stack = malloc (size * sizeof (T)); return stack == 0 ? -1 : 0; } void destroy (void) { free ((void *) stack); } void push (T item) { stack[top++] = item;} void pop (T *item) { *item = stack[--top]; } void top (T *item) { *item = stack[top - 1]; } int is_empty (void) { return top == 0; } int is_full (void) { return top == size; }

Data Hiding Implementation in C Use case Main problems: The programmer must call create() first & destroy() last! There is only one stack & only one type of stack /* File main.c */ #include "stack.h" void main(void) { T i; push (10); /* Oops, forgot to call create! */ push (20); pop (&i); destroy (); }

Data Abstraction Implementation in C /* File stack.h*/ typedef int T; typedef struct { size_t top, size; T *stack; } Stack; int Stack_create (Stack *s, size_t len); void Stack_destroy (Stack *s); void Stack_push (Stack *s, T item); void Stack_pop (Stack *, T *item); /* Must call before pop’ing */ int Stack_is_empty (Stack *); /* Must call before push’ing */ int Stack_is_full (Stack *); /* ... */

Data Abstraction Implementation in C /* File stack.c */ #include "stack.h" int Stack_create (Stack *s, size_t len) { s->top = 0; s->size = len; s->stack = malloc (size * sizeof (T)); return s->stack == 0 ? -1 : 0; } void Stack_destroy (Stack *s) free ((void *) s->stack); s->top = 0; s->size = 0; s->stack = 0; void Stack_push (Stack *s, T item) { s->stack[s->top++] = item; } void Stack_pop (Stack *s, T *item) { *item = s->stack[--s->top]; } int Stack_is_empty (Stack *s) { return s->top == 0; }

Data Abstraction Implementation in C Use case /* File main.c */ #include "stack.h" void main(void) { Stack s1, s2, s3; /* Multiple stacks! */ T item; Stack_pop (&s2, &item); /* Pop from empty stack */ /* Forgot to call Stack_create! */ Stack_push (&s3, 10); s2 = s3; /* Disaster due to aliasing!!! */ /* Destroy uninitialized stacks! */ Stack_destroy (&s1); Stack_destroy (&s2); } Main problems: No guaranteed initialization, termination, or assignment Still only one type of stack supported No generalized error handling... The C compiler does not enforce information hiding e.g., s1.top = s2.stack[0]; /* Violate abstraction */ s2.size = s3.top; /* Violate abstraction */

Data Abstraction Implementation in C++ We can get encapsulation and more than one stack: //2D point class. file: “Point.h” class Point { double x_coord; double y_coord; // Data-members public: Point(); // Constructors Point(double x, double y); ~Point(); // Destructor double getX(); // Accessors double getY(); void setX(double x); // Mutators void setY(double y); }; /* File stack.h*/ typedef int T; class Stack { public: Stack (size_t size); Stack (const Stack &s); void operator= (const Stack &); ~Stack (void); void push (const T &item); void pop (T &item); bool is_empty (void); bool is_full (void); private: size_t top, size; T *stack; };

Data Abstraction Implementation in C++ /* File stack.cpp */ #include "stack.h" Stack::Stack (size_t s): top(0), size(s), stack(new T[s]) {} Stack::Stack (const Stack &s) : top (s.top), size (s.size), stack (new T[s.size]) { for (size_t i = 0; i < s.size; ++i) stack[i] = s.stack[i]; } void Stack::operator= (const Stack &s) { if (this == &s) return; T *temp_stack = new T[s.size]; delete [] stack; stack = 0; for (size_t i = 0; i < s.size; ++i) temp_stack[i] = s.stack[i]; stack = temp_stack; top = s.top; size = s.size; Stack::~Stack (void) { delete [] stack; } bool Stack::is_empty (void) { return top == 0; } bool Stack::is_full (void) { return top == size; } void Stack::push (const T &item) { stack[top++] = item; } void Stack::pop (T &item) { item = stack[--top]; }

Data Abstraction Implementation in C++ Use case /* File main.c */ #include "stack.h" void main(void) { Stack s1 (1), s2 (100); T item; if (!s1.is_full ()) s1.push (473); if (!s2.is_full ()) s2.push (2112); if (!s2.is_empty ()) s2.pop (item); // Access violation caught at compile-time! s2.top = 10; // Termination is handled automatically. }

Benefits of C++ DAT Data hiding & data abstraction, e.g., Stack s1 (200); s1.top = 10 // Error flagged by compiler! The ability to declare multiple stack objects Stack s1 (10), s2 (20), s3 (30); Automatic initialization & termination { Stack s1 (1000); // constructor called automatically. // ... // Destructor called automatically }

Drawbacks of C++ DAT Error handling is obtrusive use exception handling to solve this (but be careful)! The example is limited to a single type of stack element (int in this case) use parameterized types to remove this limitation Function call overhead use inline functions to remove this overhead

Exception Handling Implementation in C++ /* File stack.h*/ typedef int T; class Stack { public: class Underflow { /* ... */ }; // WARNING: be cautious when using class Overflow { /* ... */ }; // exception specifiers... Stack (size_t size); Stack (const Stack &s); void operator= (const Stack &); ~Stack (void); void push (const T &item) throw (Overflow); void pop (T &item) throw (Underflow); bool is_empty (void); bool is_full (void); private: size_t top, size; T *stack; };

Exception Handling Implementation in C++ /* File stack.cpp */ #include "stack.h" Stack::Stack (size_t s): top(0), size(s) { /*stack = allocate…*/} … void Stack::push (const T &item) throw (Stack::Overflow) { if (is_full ()) throw Stack::Overflow (); // guard condition stack[top++] = item; } void Stack::pop (T &item) throw (Stack::Underflow) if (is_empty ()) throw Stack::Underflow (); item = stack[--top];

Exception Handling Implementation in C++ Use case /* File main.c */ #include "stack.h" void main(void) { Stack s1 (1), s2 (100); try { T item; s1.push (473); s1.push (42); // Exception, full stack! s2.pop (item); // Exception, empty stack! s2.top = 10; // Access violation caught! } catch (Stack::Underflow) { /* Handle underflow... */ } catch (Stack::Overflow) { /* Handle overflow... */ } catch (...) { /* Catch anything else... */ throw; }

Template Implementation in C++ /* File stack.h*/ template <typename T> class Stack { public: Stack (size_t size); Stack (const Stack<T> &rhs); void operator= (const Stack<T> &rhs); ~Stack (void) void push (const T &item); void pop (T &item); bool is_empty (void); bool is_full (void); private: size_t top, size; T *stack; };

Template Implementation in C++ /* File stack.c*/ template <typename T> Stack<T>::Stack (size_t size) : top (0), size (size){/* allocate */ } Stack<T>::~Stack (void) { /* free*/ } void Stack<T>::push (const T &item) { stack[top++] = item; } void Stack<T>::pop (T &item) { item = stack[--top]; } // ...

Template Implementation in C++ Use case /* File main.c */ #include "stack.h" void main(void) { Stack<int> s1 (1000); Stack<float> s2; s1.push (-291); s2.top = 3.1416; // Access violation caught! }