Introduction to Programming Block Tutorial C/C++

From this document you will learn the answers to the following questions:

What are instances of a class?

What do objects have in the execution of a program?

What is specified for an entire class?

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

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Object Oriented Software Design II

Glossary of Object Oriented Terms

Advanced Data Structures

C++FA 5.1 PRACTICE MID-TERM EXAM

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

CEC225 COURSE COMPACT

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

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

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

CIS 190: C/C++ Programming. Polymorphism

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance


C++ INTERVIEW QUESTIONS

a. Inheritance b. Abstraction 1. Explain the following OOPS concepts with an example

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

History OOP languages Year Language 1967 Simula Smalltalk

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

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

Java (12 Weeks) Introduction to Java Programming Language

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

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

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

Curriculum Map. Discipline: Computer Science Course: C++

Ch 7-1. Object-Oriented Programming and Classes

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

El Dorado Union High School District Educational Services

Object-Oriented Programming

13 Classes & Objects with Constructors/Destructors

Introduction to Object-Oriented Programming

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

The C Programming Language course syllabus associate level

Basics of C++ and object orientation in OpenFOAM

BCS2B02: OOP Concepts and Data Structures Using C++

11 November

Class 16: Function Parameters and Polymorphism

Chapter 1 Fundamentals of Java Programming

C++ Overloading, Constructors, Assignment operator

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

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

Object Oriented Design

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Object Oriented Software Design II

1. Polymorphism in C++...2

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

Java Application Developer Certificate Program Competencies

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

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Object Oriented Software Design II

C++ Programming Language

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

N3458: Simple Database Integration in C++11

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

C++ Support for Abstract Data Types

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

How To Design Software

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

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Subject Name: Object Oriented Programming in C++ Subject Code:

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

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

AP Computer Science Java Subset

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Building Java Programs

Course notes Standard C++ programming

Cpt S 223. School of EECS, WSU

Fundamentals of Java Programming

Description of Class Mutation Mutation Operators for Java

C# programming. Introduction. By Per Laursen

Practical Programming Methodology. Michael Buro. Class Inheritance (CMPUT-201)

More C++ Concepts. Operator overloading Friend Function This Operator Inline Function

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Programming in C# with Microsoft Visual Studio 2010

Roulette: Inheritance Case Study

An Internet Course in Software Development with C++ for Engineering Students

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

I PUC - Computer Science. Practical s Syllabus. Contents

Java Interview Questions and Answers

C++FA 3.1 OPTIMIZING C++

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Software Engineering Concepts: Testing. Pointers & Dynamic Allocation. CS 311 Data Structures and Algorithms Lecture Slides Monday, September 14, 2009

Java CPD (I) Frans Coenen Department of Computer Science

PHP Object Oriented Classes and objects

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Implementation Aspects of OO-Languages

Constructor, Destructor, Accessibility and Virtual Functions

Course Title: Software Development

Algorithms, Flowcharts & Program Design. ComPro

CSI33 Data Structures

Transcription:

Michael Bader Master s Program Computational Science and Engineering

C/C++ Tutorial Overview From Maple to C Variables, Operators, Statements Functions: declaration, definition, parameters Arrays and Pointers From Pointers to Dynamical Data Structures Object Oriented Programming: From C to C++

Friday Lesson Overview Object Oriented Programming Objects and Classes in C++ Information Hiding Inheritance Polymorphism

What is an Object? Definition: An object is a software bundle of variables and related methods. An object is characterized by its identity, its state, and its behaviour: the identity of an object distinguishes it from all other objects; the state of an object is given by its variables; behaviour of an object is specified by its methods (functions, procedures) Objects can reflect real world components that are part of the problem.

What is a Class? Definition: A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. Objects vs. Classes: objects are instances of a class classes are implemented by the programmer objects are created during the execution of a program all objects have the same behaviour (i.e. methods); methods are specified for an entire class all objects of a class have the same variables ( members ), but with different values ( state )

Paradigms in Object Oriented Programming Abstraction Encapsulation Modularity Inheritance Polymorphism

What is Abstraction? Abstraction means focusing on the essential properties of an object. ignore details ask what can an object do? instead of how is it going to do it? top-down approach use objects and classes for program design (instead of memory cells) specify interfaces before starting the implementation Abstraction is part of the object oriented design process.

What is Encapsulation? Encapsulation is also known as Information Hiding: Implementational issues of a class should be hidden from third parties as far as possible. access to private members or functions of another class is forbidden modifying the state of an object is only possible using public methods (and members); the public members (and methods) define the interface of a class Goal: All information and processes associated with an object should be contained within its definition. Consequence: implementation of a class can be changed without affecting its behaviour.

What is Modularity? Modularity means partitioning a program (project) into smaller units, each performing a well designed task. Goals: group related functions/procedures/objects well-defined interfaces between modules division of responsibilities make application code more readable Consequence: different programmers or teams can work on different modules of a project.

What is Inheritance? Inheritance means that a class can inherit state and behaviour from a superclass. Goals: re-use of existing (error free) code define generic behaviour of a superclass that can be inherited by several subclasses Example: Vehicles (= superclass) possible subclasses: car, airplane, horse cart, truck, bicycle subclasses of cars: limousine, convertible, etc.

Inheritance vs. Composition Inheritance: is-a -relationship Example: Vector is-a Array of Integer (?) access of elements identical mathematical operations may be missing Composition: has-a -/ consists-of -relationship Example: Vector consists-of Array of Integer (?) Assume: Array is resizable Vector resizable? different range of indices? Sometimes a Difficult Decision!

What is Polymorphism? different methods sharing the same name can be defined for different classes pick the method that corresponds to the type of the object corresponding method is chosen at runtime with inheritance, this even works if the syntax (at compile-time) suggests another method Define different implementations for common interfaces

Polymorphism and Interfaces Interface: abstract superclass that determines common behaviour (methods) of its subclasses Example: different matrix implementations specify set of interface methods: getelem, setelem, multiply implement methods in subclasses (SparseMatrix, TriMatrix,... ) solvers stick to interface methods required matrix methods are chosen at runtime depending on which type of matrix implementation is used

What is an Object Oriented Language? OO Programming/Design: technique/approach how to design and implement software OO Language: programming language that supports OO programming Object oriented programming languages should support classes/objects support inheritance/polymorphism support information hiding/encapsulation support means more than just it is possible to do it!

Classes in C++ Example: a class definition for vectors class Vector { public: Vector(int s); ~Vector(); // define methods: // constructor // destructor double getelem(int index); void setelem(int index, double value); } private: int size; double* elems; // define member variables: // size of the Vector // array of elements

Public and Private Members/Methods C++ allows to place certain access restrictions on its members and methods: public members or methods can be used by all functions or methods to modify an object s state private members or methods can only be accessed by methods of the same class. Other methods or functions cannot access or modify private members. protected members or methods can also be accessed by methods of subclasses (see inheritance). Information Hiding, Encapsulation

Creating and Deleting Objects Constructors and destructors are methods that create and delete objects of a class: initialize member variables allocate/delete memory for required dynamical data structures In C++, constructors and destructors do not have any return type have the same name as the class Constructors can have parameters, destructors never have any parameters.

Constructors and Destructors Example: Vector::Vector(int s) { size = s; elems = new double[s]; for(int i=0;i<size;i++) elems[i] = 0.0; // initialize elements } Vector::~Vector() { delete elems; } Method definitions are to be placed outside the class definition

new and delete The C++ operators new and delete create and delete objects dynamically. new (like malloc in C) returns a pointer to the created object. Example: Vector* v; v = new Vector(5); // define a new vector of 5 elements delete v; new can also be used to create arrays. Example: double* elems; elems = new double[10]; // define a new vector of 5 elements

Definition of Member Functions (Methods) Example: double Vector::getElem(int index) { if ( index>=0 && index<size ) return elems[index]; cerr << "Array Index out of Bounds!\n"; //error! } void Vector::setElem(int index, double value) { if ( index>=0 && index<size ) { elems[index] = value; return; } cerr << "Array Index out of Bounds!\n"; }

Calling Member Functions (Methods) Example: main() { int i; Vector v(10),w(10); for(i=0;i<10;i++) { v.setelem(i,i); w.setelem(i,10.0-i); } for(i=0;i<10;i++) cout << v.getelem(i) << << w.getelem(i) << endl; } calls refer to objects members of the called object are additional parameters

Inheritance in C++ Example: class definition for matrix lines class MatrixLine : public Vector { public: MatrixLine(int s); // Constructors are never inherited ~MatrixLine(); } double multiply(vector c); MatrixLines inherits from class Vector: methods getelem and setelem members size and elems

Polymorphism and Interfaces in C++ Example: matrix interface class Matrix { public: virtual double getelem(int, int) = 0; virtual void setelem(int, int, double) = 0; virtual Vector& multiply(const Vector&) = 0; } class SparseMatrix : public Matrix { public: virtual double getelem(int, int); virtual void setelem(int, int, double); virtual Vector& multiply(const Vector&);... }

Polymorphism and Interfaces in C++ (2) Example: main() { Matrix *m; SparseMatrix sm(10); TriMatrix tm(10); Vector v(10);... m = &sm; m->multiply(v); m = &tm; m->multiply(v); } The decision which implementation of multiply has to be used is taken at runtime.

Features of C++ that are not Object Oriented overloading of functions: Vector(int size); Vector(int size, double initvalue); overloading of operators: Matrix& operator*=(const Matrix& othermatrix); new and delete Reference types: Matrix &m, i.e. pointers without pointer arithmetics strict type checking, inline functions, templates, exceptions,...