Object-Oriented Programming

Similar documents
CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Structure of this course. C and C++ Past Exam Questions. Text books

Programming in C and C++

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Programming in C and C++

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C-LANGUAGE CURRICULAM

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

6.096 Introduction to C++ January (IAP) 2009

A Fast Review of C Essentials Part I

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Computers Programming Course 5. Iulian Năstac

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Introduction to C++ with content from

UNIT- 3 Introduction to C++

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Motivation was to facilitate development of systems software, especially OS development.

Chapter 1 & 2 Introduction to C Language

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

IECD Institute for Entrepreneurship and Career Development Bharathidasan University, Tiruchirappalli 23.

BLM2031 Structured Programming. Zeyneb KURT

Tokens, Expressions and Control Structures

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Procedures, Parameters, Values and Variables. Steven R. Bagley

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

Motivation was to facilitate development of systems software, especially OS development.

CSI33 Data Structures

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

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

Lectures 5-6: Introduction to C

Fundamental of Programming (C)

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces


Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Review of the C Programming Language for Principles of Operating Systems

C: How to Program. Week /Mar/05

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Introduction to Programming Using Java (98-388)

Model Viva Questions for Programming in C lab

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Chap 0: Overview. Overview of basic C++ syntax Refresh programming basics C++ Vs. Java differences Coding conventions used. EECS 268 Programming II 1

TDDB68 Concurrent Programming and Operating Systems. Lecture 2: Introduction to C programming

Contents. Preface. Introduction. Introduction to C Programming

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Lectures 5-6: Introduction to C

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

Procedural Programming & Fundamentals of Programming

Computers Programming Course 11. Iulian Năstac

C Language, Token, Keywords, Constant, variable

COMPUTER SCIENCE HIGHER SECONDARY FIRST YEAR. VOLUME II - CHAPTER 10 PROBLEM SOLVING TECHNIQUES AND C PROGRAMMING 1,2,3 & 5 MARKS

Short Notes of CS201

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Getting started with Java

Programming, numerics and optimization

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

Pace University. Fundamental Concepts of CS121 1

Preview from Notesale.co.uk Page 6 of 52

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

CS201 - Introduction to Programming Glossary By

Introduce C# as Object Oriented programming language. Explain, tokens,

Language Reference Manual simplicity

Fundamental of C programming. - Ompal Singh

Programming in C and C++

Programming. Data Structure

C++ Programming: From Problem Analysis to Program Design, Third Edition

Unit 7. Functions. Need of User Defined Functions

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

The C Language Reference Manual

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to Computing Lecture 01: Introduction to C

CE221 Programming in C++ Part 1 Introduction

Compiling and Running a C Program in Unix

Chapter 2 - Introduction to C Programming

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

C Basics. Chapter 2: C Basics. C Programming Language. v3.3 February 6, 2017

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

BASIC ELEMENTS OF A COMPUTER PROGRAM

VARIABLES AND CONSTANTS

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

CSCI 171 Chapter Outlines

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

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

A brief introduction to C programming for Java programmers

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

Transcription:

iuliana@cs.ubbcluj.ro Babes-Bolyai University 2018

Overview 1 2 3 4 5 6 7

I No beard, no belly, no guru... Ken Thompson (B), Dennis Ritchie (C) - UNIX Bjarne Stroustrup (C++) James Gosling (Java) Figure: Figure sources: https://herbsutter.com/2011/10/13/ 2000-interview-dennis-ritchie-bjarne-stroustrup-and-james-gosling/,http://www.catb.org/~esr/jargon/ html/u/unix.html

II Why?: widely used, both in industry and in education; is a high level ; C++ is a hybrid (multi-paradigm), implements all the concepts needed for object oriented ; many s are based on (Java, C#). Knowing C++ makes learning other s easier.

III Why : C++ is an evolving ; C++ is highly standardized; C++ gets compiled into processor instructions (no interpretation engine needed).

Integrated Development Environment for Microsoft Visual Studio 2017 Community (or Professional/Express/Premium) download from https://www.visualstudio.com/downloads or from Microsoft DreamSpark; offers both an IDE and a compiler. Eclipse CDT you need an external compiler: MinGW or Cygwin; you can find a list of compilers at: https://isocpp.org/ get-started to install on Windows, see tutorials mentioned in Lab1. Online IDEs you can find a list of online IDEs at: https://isocpp. org/get-started

Hello World Demo DEMO Hello World! (HelloWorldC.c, HelloWorld.cpp).

The compilation process I The compiler translates source code into machine code.

The compilation process II All these steps are performed before you start running a program. This is one of the reasons code runs far faster than code in many more recent s.?are source code files sufficient for someone to execute your program? In which conditions?

The compilation process III Figure: Figure source: https://xkcd.com/303/

Structure of a simple program Preprocessor directives - e.g. for using libraries; #i n c l u d e <s t d i o. h> #i n c l u d e <i o s t r e a m > main - special function that is called by the OS to run the program; int main ( ) { //... return 0 ; } Every statement must end with a semicolon.

Debugging Allows us to step through the code, as it is running; Execution can be paused at certain points; The effects of individual statements can be seen; Allows inspecting the current state of the program (values of variables, call stack); Breakpoints - stop the program when reaching the breakpoint.

Lexical elements I (un- is case sensitive. Identifier: Sequence of letters and digits, start with a letter or derline); Names of things that are not built into the ; E.g.: i, myfunction, res, nameofvariable. Keywords (reserved words): Identifier with a special purpose; Words with special meaning to the compiler; E.g.: int, for, typedef, struct.

Lexical elements II Literals: Basic constant values whose value is specified directly in the source code; E.g.: Hello, 72, 4.6, c. Operators: Mathematical: e.g. +, -, *; Logical: e.g.!, &&. Separators: Punctuation defining the structure of a program: e.g. ;, { }, ( ).

Lexical elements III Whitespace: Spaces of various sorts, ignored by the compiler: space, tab, new line. Comments: ignored by the compiler. // This is a single line comment. /* This is a multiline comment. */

A type is a domain of values and a set of operations defined on these values. are strongly typed s. Fundamental data types in C: char (1 byte) int (4 bytes) unsigned int (4 bytes) long int/long (4 bytes) float (4 bytes) double (8 bytes) long double (8 bytes) bool (1 byte) in C++: https://msdn.microsoft.com/en-us/library/ s3f49ktz.aspx.

Casting implicit casting; static cast. DEMO Type casting (Casting.cpp).

Arrays If T is an arbitrary basic type: T arr[n] - is an array of length n with elements of type T; indexes are from 0 to n-1; indexing operator: [ ]; compare 2 arrays by comparing the elements; multidimensional arrays: arr[n][m]. DEMO Arrays (Arrays.c).

C String Represented as char arrays, the last character is \0 (marks the end of the string); Handled as any ordinary array; Standard library for string manipulation in C (string.h); strlen - Returns the number of chars in a C string. strcpy - Copies the characters from the source string to the destination string. Obs. The assignment operator will not copy the string (or any array).

C String Standard library for string manipulation in C (string.h); strcmp - Compares two strings and returns: zero, if a = b; negative, if a < b; positive, if a > b. Obs. Using ==, <, >operators on C strings (or any array) compares memory addresses. strcat - Appends the characters from the source string to the end of destination string. Obs. None of these string routines allocate memory or check that the passed in memory is the right size. DEMO CStrings (CStrings.c).

Record - composite type is a collection of items of different types; group various data types into a structure. declared using struct. typedef - Introduce a shorthand name for a type. DEMO Records (StructExample.c).

Variables A variable is a named location in memory; Memory is allocated according to the type of the variable; The types tell the compiler how much memory to reserve for it and what kinds of operations may be performed on it; The value of the variable is undefined until the variable is initialized; It is recommended to initialise the variables (with meaningful values) at declaration; Use suggestive names for variables.

Constants Fixed values that the program may not alter during its execution; Can be defined using the #define preprocessor directive, or the const keyword; Can be: integer #d e f i n e LENGTH 10 const int LENGTH = 1 0 ; floating #d e f i n e PI 3. 1 4 string literal const char pc = " Hello" ; enumeration constant enum c o l o r s {RED, YELLOW, GREEN, BLUE} ;

I Every variable is a named memory location; A pointer is a variable whose value is a memory location (can be the address of another variable). Declaration: same as declaring a normal variable, except an asterisk (*) must be added in front of the variable s identifier. int x ; char s t r ; Operators address of operator & - take the address of a variable; dereferencing operator * - get the value at the memory address pointed to. DEMO (.c).

II

III Figure: Figure source: http://www.quickmeme.com/meme/3580gd

A statement is a unit of code that does something - a basic building block of a program. Except for the compound statement, in C and C++ every statement is ended by ;. in C and C++: Empty statement; Compound statement; Conditional statement: if, if-else, else if, switch-case; Loops: while, do-while, for. DEMO (.c).

Read/Write from/to console DEMO scanf - read from the command line http://www.cplusplus.com/reference/cstdio/scanf/ printf - print to the console (standard output) http://www.cplusplus.com/reference/cstdio/printf/ Read and Write (ReadWrite.c).

A function is a group of related instructions (statements) which together perform a particular task. The name of the function is how we refer to these statements. The main function is the starting point for every program. Declaration (Function prototype) <result type> name (<parameter list>); /* Computes the greatest common divisor of two positive integers. Input: a, b integers, a, b > 0 Output: returns the the greatest common divisor of a and b. */ int gcd ( int a, int b ) ;

Definition < result type> name (< parameter list >) { // statements - the body of the function } return <exp> - the result of the function will be the expression value and the function is unconditionally exited. A function that returns a result (not void) must include at least one return statement. The declaration needs to match the function definition.

Specification meaningful name for the function; short description of the function (the problem solved by the function); meaning of each input parameter; conditions imposed over the input parameters (precondition); meaning of each output parameter; conditions imposed over the output parameters (post condition).

Function invocation <name>(<parameter list>); All argument expressions are evaluated before the call is attempted. The list of actual parameters need to match the list of formal parameters (types). Function declaration needs to occur before invocation.

Variable scope and lifetime I Scope: the place where a variable was declared determines where it can be accessed from. Local variables have their own scopes: variables defined inside the function will be visible only in the function, and destroyed after the function call. Loops and if/else statements also have their own scopes. Cannot access variables that are out of scope (compiler will signal an error). A variable lifetime begins when it is declared and ends when it goes out of scope (destroyed).

Variable scope and lifetime II Global variables Variables defined outside of any function are global variables. Can be accessed from any function. The scope is the entire application. Do not use global variables unless you have a very good reason to do so (usually you can find better alternatives).

Function parameters I Pass by value E.g. void byvalue ( int a ) Default parameter passing mechanism in. On function call makes a copy of the actual parameter. The original variable is not affected by the change made inside the function.

Function parameters II Pass by reference In C: E.g. C there is no pass by reference; it is simulated with pointers; pointers are passed by value. void byrefc ( int a ) C++ void byref ( int& a )

Function parameters III DEMO The memory address of the parameter is passed to the function. Changes made to the parameter will be reflected in the invoker. Arrays are passed by reference. (.cpp).

Test functions Assert #i n c l u d e <a s s e r t. h> void a s s e r t ( int e x p r e s s i o n ) ; if expression is evaluated to 0, a message is written to the standard error device and the execution will stop. the message includes: the expression whose assertion failed, the name of the source file, and the line number where it happened.

Function design guidelines Single responsability principle. Use meaningful names (function name, parameters, variables). Use naming conventions (add rational, addrational, CON- STANT), be consistent. Specify and test functions. Use test driven development. Include comments in the source code. Avoid functions with side efects (if possible).