C++ Introduction to class and data abstraction

Similar documents
DATA STRUCTURE - STACK

CmpSci 187: Programming with Data Structures Spring 2015

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Object Oriented Software Design II

Analysis of a Search Algorithm

C++ Overloading, Constructors, Assignment operator

Comp151. Definitions & Declarations

C++ INTERVIEW QUESTIONS

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Programming with Data Structures

Stacks. Linear data structures

1 Abstract Data Types Information Hiding

Stack Allocation. Run-Time Data Structures. Static Structures

DATA STRUCTURE - QUEUE

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Chapter 13 Storage classes

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

An Incomplete C++ Primer. University of Wyoming MA 5310

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Abstract Data Types. Chapter 2

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Introduction to Data Structures

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

An Overview of Java. overview-1

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Lecture J - Exceptions

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Binary storage of graphs and related data

Habanero Extreme Scale Software Research Project

C++FA 5.1 PRACTICE MID-TERM EXAM

El Dorado Union High School District Educational Services

Classes and Pointers: Some Peculiarities (cont d.)

7.1 Our Current Model

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

C++ Support for Abstract Data Types

CS 111 Classes I 1. Software Organization View to this point:

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Lecture 12: Abstract data types

St S a t ck a ck nd Qu Q eue 1

Sources: On the Web: Slides will be available on:

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

Java from a C perspective. Plan

The C Programming Language course syllabus associate level

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Data Structures and Algorithms Lists

Tutorial on C Language Programming

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

DATA STRUCTURES USING C

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Ch 7-1. Object-Oriented Programming and Classes

Introduction to Programming

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Object-Oriented Programming

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Introduction to Java

Linked Lists Linked Lists, Queues, and Stacks

Glossary of Object Oriented Terms

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Data Structures Using C++ 2E. Chapter 5 Linked Lists

A deeper look at Inline functions

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

C Compiler Targeting the Java Virtual Machine

Object Oriented Software Design II

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

CSC 551: Web Programming. Fall 2001

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Inline Variables. Document Number: N4424 Date: Hal Finkel and Richard Smith

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Common Data Structures

Passing 1D arrays to functions.

10CS35: Data Structures Using C

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

C++ Crash Kurs. C++ Object-Oriented Programming

Syllabus OBJECT ORIENTED PROGRAMMING C++

java Features Version April 19, 2013 by Thorsten Kracht

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

C++FA 3.1 OPTIMIZING C++

Introduction to Objective-C. Kevin Cathey

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Part I. Multiple Choice Questions (2 points each):

Note: Syntactically, a ; is needed at the end of a struct definition.

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Variable Base Interface

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

LAB4 Making Classes and Objects

PES Institute of Technology-BSC QUESTION BANK

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

ECS 165B: Database System Implementa6on Lecture 2

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Code Refactoring and Defects

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

1. Polymorphism in C++...2

Transcription:

C++ Introduction to class and data abstraction 1

Data abstraction A data abstraction is a simplified view of an object by specifying what can be done with the object while hiding unnecessary details In computer science, the term Abstract Data Type (ADT) is used The term interface is also sometimes used In OOP, an ADT is implemented as a class Example of ADT: a stack It does not matter how the stack is implemented: array, single linked list, double linked list What matters is the operations on the stack: push, pop 2

Encapsulation Encapsulation means preventing access to some piece of information or functionality An abstract specification tells us what an object does but not how it does it (information / functionality hiding) With encapsulation we can design a program in a way that changing its internal implementation will have no effect on the rest of the program or its users Example: a stack can be implemented at first using an array, we can later change this to a single linked list. It should not affect the users of the class Stack. 3

Example: stack implemented as an class Stack { private: int size; int max_size; int top; int* data; public: Stack (int N); ~Stack(); void push(int el); int pop(); bool is_empty(); bool is_full(); int num_elements(); array Data members and implementation details (the stack is implemented by an array) The public interface: what the users of the class and the rest of the program sees 4

Example: stack implemented as a class Stack { private: struct Node { int data; Node* next; int size; int max_size; Node* top; public: Stack (int N); ~Stack(); void push(int el); int pop(); bool is_empty(); bool is_full(); int num_elements(); linked list Data members and implementation details (the stack is implemented by linked list) The public interface: what the users of the class and the rest of the program sees. It remains unchanged. 5

Data member of a class Data members of a class can be: Any basic type (int, float, ) or pointer to a basic type Any user defined type or pointer to a user defined type (that has already been defined in the program) A class name can be used in its own definition but only as a pointer: class Node { public: int data; Node* next; or struct Node { int data; Node* next; } 6

Forward declaration Forward declaration consists in declaring a class not yet defined such that a pointer (or reference) to that type can be used Forward declaration: class Node; // forward declaration class Stack { public: Node* top; // ok Node a_node; // compile error Note: as illustrated in the example above, it works for pointers (and references) only 7

Initialization of data members Data members need to be initialized in constructors or member functions For example, the following code is illegal in C++ (but works in Java): class A { public: int a = 1; // not ok 8

Initialization of data members The following code is correct: class A { public: int a; A() { a = 1; } 9

Initialization of data members Constants in C++ on the other hand can be initialized at definition: class A { public: static const int a = 1; // legal Notes: It works for integral types only; you can not initialize static const double or static const float this way It can fail with some old C++ compiler 10

Member functions Member functions (or methods) are used to manipulate objects Example: if a class MyString defines a method length() to query the string s length it can be used as: MyString str = some string ; int len = str.length(); Or: MyString* str = getptrtostring(); int len = str->length(); 11

Member functions Methods can be defined inside a class (we call them inline): class A { public: int a_function(int x) { return x + 1;} 12

Member functions Or methods can be declared within a class and defined somewhere else (either in the header file or in an implementation file) Example: // within the file A.h class A { public: int a_function(int x); // within the file A.cpp int A::a_function(int x) { return x + 1;} Note that when the method is defined outside the class, its name is prefixed by the class name (A:: in the example above) 13

Access control Access control apply to both member data and member functions It allows to control who is able to access a given data or function Public: means accessible to everybody Protected: means accessible to member functions and friends of the class and to member functions and friends of the derived classes Private: means accessible only to member functions and friends of the class Trying to access a non-accessible member results in a compile time error 14

Example class MyString { private: char* repr; void allocate(); public: int length(); 15

Example repr and allocate() are accessible by length() repr and allocate() would not be accessible by an external function, e.g. main() length() can be accessed by any other functions (including methods of other classes( 16

Object implementation Each object has its own copy of the class data members The exception is the static data members that are shared among instances (we will look at static member later) Member functions are shared among the object instances 17