Object oriented programming C++

Similar documents
Thus, to declare billy as shown above it would be something as simple as the following sentence:

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Object oriented programming C++

Object oriented programming with C++

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

BITG 1113: Array (Part 2) LECTURE 9

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

C++ Lecture 5 Arrays. CSci 588: Data Structures, Algorithms and Software Design.

Computer Programming: C++

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Short Notes of CS201

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CS201 - Introduction to Programming Glossary By

Intermediate Programming, Spring 2017*

2 nd Week Lecture Notes

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

M.CS201 Programming language

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Biyani's Think Tank. Concept based notes. C Language M.C.A. Neha Jain M.C.A Lecturer Deptt. of Information Technology Biyani Girls College, Jaipur

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Language Proposal: tail

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Array. Prepared By - Rifat Shahriyar


BİL200 TUTORIAL-EXERCISES Objective:

Unit 2: Java in the small

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to C Final Review Chapters 1-6 & 13

INTRODUCTION TO PROGRAMMING

In this chapter, you will learn about: An Array Type for Strings. The Standard string Class. Vectors. Introduction Computer Science 1 CS 23021

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

AN OVERVIEW OF C++ 1

Lecture 04 FUNCTIONS AND ARRAYS

CS 220: Introduction to Parallel Computing. Input/Output. Lecture 7

OBJECTIVE QUESTIONS: Choose the correct alternative:

Key Differences Between Python and Java

Pace University. Fundamental Concepts of CS121 1

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Unit 7. Functions. Need of User Defined Functions

ARRAYS(II Unit Part II)

6.096 Introduction to C++

Problem Solving with C++

Functions. Lecture 6 COP 3014 Spring February 11, 2018

Computers Programming Course 10. Iulian Năstac

Chapter 10 Characters, Strings, and the string class

Computer Systems Lecture 9

Pointers. Reference operator (&) ted = &andy;

Procedural Programming & Fundamentals of Programming

JVM (java) compiler. A Java program is either a library of static methods (functions) or a data type definition

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Java Bytecode (binary file)

Advanced Computer Programming

UEE1302 (1102) F10: Introduction to Computers and Programming

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Functions in C C Programming and Software Tools

CSC 307 DATA STRUCTURES AND ALGORITHM ANALYSIS IN C++ SPRING 2011

Chapter 15 - C++ As A "Better C"

C Arrays. Group of consecutive memory locations Same name and type. Array name + position number. Array elements are like normal variables

Introduction to C Language

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

( &% class MyClass { }

Introduction to Bioinformatics

Copyright 2003 Pearson Education, Inc. Slide 1

Absolute C++ Walter Savitch

Lecture 18 Tao Wang 1

Programming Language Basics

M.EC201 Programming language

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Kurt Schmidt. October 30, 2018

Procedural programming with C

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to string

Object Oriented Design

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

AP CS Unit 3: Control Structures Notes

Technical Questions. Q 1) What are the key features in C programming language?

Getting started with C++ (Part 2)


COMP1917: 09 Arrays and Strings

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Java+- Language Reference Manual

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CS201 Some Important Definitions

Typescript on LLVM Language Reference Manual

Variables. Data Types.

This is CS50. Harvard University Fall Quiz 0 Answer Key

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Transcription:

http://uranchimeg.com Object oriented programming C++ T.Uranchimeg Prof. Dr. Email uranchimeg@must.edu.mn Power Engineering School M.EC203* -- OOP (C++) -- Lecture 06

Subjects Functions Functions with no types Arguments passed by value and by reference inline functions Overloaded functions M.EC203* -- OOP (C++) -- Lecture 06 2

inline functions The inline directive can be included before a function declaration to specify that the function must be compiled as code at the same point where it is called. This is equivalent to declaring a macro. Its advantage is only appreciated in very short functions, in which the resulting code from compiling the program may be faster if the overhead of calling a function (stacking of arguments) is avoided. M.EC203* -- OOP (C++) -- Lecture 06 3

Format inline type name ( arguments... ) { instructions... } M.EC203* -- OOP (C++) -- Lecture 06 4

Recursive Recursivity is the property that functions have to be called by themselves. It is useful for tasks such as some sorting methods or to calculate the factorial of a number. M.EC203* -- OOP (C++) -- Lecture 06 5

The factorial M.EC203* -- OOP (C++) -- Lecture 06 6

Example M.EC203* -- OOP (C++) -- Lecture 06 7

Exapmle M.EC203* -- OOP (C++) -- Lecture 06 8

Prototyping functions Until now, we have defined the all of the functions before the first appearance of calls to them, that generally was in main, leaving the function main for the end. If you try to repeat some of the examples of functions described so far, but placing the function main before any other function that is called from within it, you will most likely obtain an error. M.EC203* -- OOP (C++) -- Lecture 06 9

Prototyping functions The reason is that to be able to call a function it must have been declared previously (it must be known), like we have done in all our examples. But there is an alternative way to avoid writing all the code of all functions before they can be used in main or in another function. M.EC203* -- OOP (C++) -- Lecture 06 10

Prototyping functions It is by prototyping functions. This consists in making a previous shorter, but quite significant, declaration of the complete definition so that the compiler can know the arguments and the return type needed. M.EC203* -- OOP (C++) -- Lecture 06 11

Format type name ( argument_type1, argument_type2,...); M.EC203* -- OOP (C++) -- Lecture 06 12

Property 01 It does not include a statement for the function. That means that it does not include the body with all the instructions that are usually enclose within curly brackets { }. M.EC203* -- OOP (C++) -- Lecture 06 13

Property 02 and 03 It ends with a semicolon sign (;). In the argument enumeration it is enough to put the type of each argument. The inclusion of a name for each argument as in the definition of a standard function is optional, although recommended. M.EC203* -- OOP (C++) -- Lecture 06 14

Example M.EC203* -- OOP (C++) -- Lecture 06 15

Example M.EC203* -- OOP (C++) -- Lecture 06 16

Example The first things that we see are the prototypes of functions odd and even: void odd (int a); void even (int a); that allows these functions to be used before they are completely defined, for example, in main, which now is located in a more logical place: the beginning of the program's code. M.EC203* -- OOP (C++) -- Lecture 06 17

Arrays Arrays are a series of elements (variables) of the same type placed consecutively in memory that can be individually referenced by adding an index to a unique name. That means that, for example, we can store 5 values of type int without having to declare 5 different variables each with a different identifier. Instead, using an array we can store 5 different values of the same type, int for example, with a unique identifier. M.EC203* -- OOP (C++) -- Lecture 06 18

Array an array to contain 5 integer values of type int called billy could be represented this way: where each blank panel represents an element of the array, that in this case are integer values of type int. These are numbered from 0 to 4 since in arrays the first index is always 0, independently of its length. M.EC203* -- OOP (C++) -- Lecture 06 19

Format Like any other variable, an array must be declared before it is used. A typical declaration for an array in C++ is: type name [elements]; where type is a valid object type (int, float...), name is a valid variable identifier and the elements field, that is enclosed within brackets [], specifies how many of these elements the array contains. M.EC203* -- OOP (C++) -- Lecture 06 20

Initializing arrays When declaring an array of local scope (within a function), if we do not specify otherwise, it will not be initialized, so its content is undetermined until we store some values in it. If we declare a global array (outside any function) its content will be initialized with all its elements filled with zeros. M.EC203* -- OOP (C++) -- Lecture 06 21

Example Thus, if in the global scope we declare: int billy [5]; every element of billy will be set initialy to 0: M.EC203* -- OOP (C++) -- Lecture 06 22

Initializing arrays But additionally, when we declare an Array, we have the possibility to assign initial values to each one of its elements using curly brackets { }. For example: int billy [5] = { 16, 2, 77, 40, 12071 }; this declaration would have created an array like the following one: M.EC203* -- OOP (C++) -- Lecture 06 23

Example The number of elements in the array that we initialized within curly brackets { } must match the length in elements that we declared for the array enclosed within square brackets [ ]. For example, in the example of the billy array we have declared that it had 5 elements and in the list of initial values within curly brackets { } we have set 5 different values, one for each element. M.EC203* -- OOP (C++) -- Lecture 06 24

Example Because this can be considered useless repetition, C++ includes the possibility of leaving the brackets empty [ ] and the size of the Array will be defined by the number of values included between curly brackets { }: int billy [] = { 16, 2, 77, 40, 12071 }; M.EC203* -- OOP (C++) -- Lecture 06 25

Access to the values of an Array In any point of the program in which the array is visible we can access individually anyone of its values for reading or modifying as if it was a normal variable. The format is the following: name[index] M.EC203* -- OOP (C++) -- Lecture 06 26

Example M.EC203* -- OOP (C++) -- Lecture 06 27

valid operations M.EC203* -- OOP (C++) -- Lecture 06 28

Example M.EC203* -- OOP (C++) -- Lecture 06 29

Multidimensional Arrays Multidimensional arrays can be described as arrays of arrays. For example, a bidimensional array can be imagined as a bidimensional table of a uniform concrete data type. M.EC203* -- OOP (C++) -- Lecture 06 30

Example M.EC203* -- OOP (C++) -- Lecture 06 31

Example M.EC203* -- OOP (C++) -- Lecture 06 32

Example Multidimensional arrays are not limited to two indices. They can contain as many indices as needed, although it is rare to have to represent more than 3 dimensions. Just consider the amount of memory that an array with many indices may need. M.EC203* -- OOP (C++) -- Lecture 06 33

Example For example: char century [100][365][24][60][60]; assigns a char for each second contained in a century, that is more than 3 billion chars! This would consume about 3000 megabytes of RAM memory if we could declare it. M.EC203* -- OOP (C++) -- Lecture 06 34

Example M.EC203* -- OOP (C++) -- Lecture 06 35

Arrays as parameters At some moment we may need to pass an array to a function as a parameter. In C++ is not possible to pass by value a complete block of memory as a parameter to a function, even if it is ordered as an array, but it is allowed to pass its address. This has almost the same practical effect and it is a much faster and more efficient operation. M.EC203* -- OOP (C++) -- Lecture 06 36

Example In order to admit arrays as parameters the only thing that we must do when declaring the function is to specify in the argument the base type for the array, an identifier and a pair of void brackets []. M.EC203* -- OOP (C++) -- Lecture 06 37

Example For example, the following function: void procedure (int arg[]) admits a parameter of type "Array of int" called arg. In order to pass to this function an array declared as: int myarray [40]; it would be enough to write a call like this: procedure (myarray); M.EC203* -- OOP (C++) -- Lecture 06 38

Example M.EC203* -- OOP (C++) -- Lecture 06 39

Strings of Characters In C++ there is no specific elemental variable type to store strings of characters. In order to fulfill this feature we can use arrays of type char, which are successions of char elements. Remember that this data type (char) is the one used to store a single character, for that reason arrays of them are generally used to make strings of single characters. M.EC203* -- OOP (C++) -- Lecture 06 40

Example M.EC203* -- OOP (C++) -- Lecture 06 41

Example Notice how after the valid content a null character ('\0') it is included in order to indicate the end of the string. The panels in gray color represent indeterminate values. M.EC203* -- OOP (C++) -- Lecture 06 42

Initialization of strings In this case we would have declared a string of characters (array) of 6 elements of type char initialized with the characters that compose Hello plus a null character '\0'. M.EC203* -- OOP (C++) -- Lecture 06 43

Example Therefore we could initialize the string mystring with values by either of these two ways: M.EC203* -- OOP (C++) -- Lecture 06 44

Example Before going further, notice that the assignation of multiple constants like double-quoted constants (") to arrays are only valid when initializing the array, that is, at the moment when declared. Expressions within the code like: M.EC203* -- OOP (C++) -- Lecture 06 45

Important We can "assign" a multiple constant to an Array only at the moment of initializing it. M.EC203* -- OOP (C++) -- Lecture 06 46

Assigning values to strings strcpy (string copy) is defined in the cstring (string.h) library and can be called the following way: strcpy (string1, string2); M.EC203* -- OOP (C++) -- Lecture 06 47

Example cin.getline ( char buffer[], int length, char delimiter = ' \n'); where buffer is the address of where to store the input (like an array, for example), length is the maximum length of the buffer (the size of the array) and delimiter is the character used to determine the end of the user input, which by default - if we do not include that parameter - will be the newline character ('\n'). M.EC203* -- OOP (C++) -- Lecture 06 48

Example M.EC203* -- OOP (C++) -- Lecture 06 49

Converting strings to other types The cstdlib (stdlib.h) library provides three useful functions for this purpose: atoi: converts string to int type. atol: converts string to long type. atof: converts string to float type. M.EC203* -- OOP (C++) -- Lecture 06 50

Converting strings to other types M.EC203* -- OOP (C++) -- Lecture 06 51

Have you questions? M.EC203* -- OOP (C++) -- Lecture 06 52

Summary Functions Functions with no types Arguments passed by value and by reference inline functions Overloaded functions M.EC203* -- OOP (C++) -- Lecture 06 53

End of Thank you for ATTENTION M.EC203* -- OOP (C++) -- Lecture 06 54