PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Similar documents
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction to C++ Systems Programming

C++ basics Getting started with, and Data Types.

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

AN OVERVIEW OF C++ 1

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

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Programming in C++: Assignment Week 1

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Programmazione. Prof. Marco Bertini

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

CSE 333 Lecture 9 - intro to C++

COMP322 - Introduction to C++

Tutorial-2a: First steps with C++ programming

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

6.096 Introduction to C++ January (IAP) 2009

Short Notes of CS201

Fast Introduction to Object Oriented Programming and C++

CS201 - Introduction to Programming Glossary By

CS

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Interview Questions of C++

CS 376b Computer Vision

Lab Instructor : Jean Lai

Why VC++ instead of Dev C++?

CS3157: Advanced Programming. Outline

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2: Introduction to C++

Exercise 1.1 Hello world

Linked List using a Sentinel

UEE1302 (1102) F10: Introduction to Computers and Programming

WARM UP LESSONS BARE BASICS

CS2141 Software Development using C/C++ C++ Basics

CS 11 C++ track: lecture 1

CS349/SE382 A1 C Programming Tutorial

Standard I/O in C and C++

Introduction to C++ Introduction to C++ 1

CS242 COMPUTER PROGRAMMING

Evolution of Programming Languages

Introduction to Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

LECTURE 02 INTRODUCTION TO C++

Introduction to C++ (Extensions to C)

Where do we go from here?

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

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

Intermediate Programming, Spring 2017*

Programming. C++ Basics

a data type is Types

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

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

The C++ Language. C++ Features. Example: A stack in C. C++ Solution: Class. Example: A stack in C. Bjarne Stroupstrup, the language s creator

Pointers, Dynamic Data, and Reference Types

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

CPSC 427: Object-Oriented Programming

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Understanding main() function Input/Output Streams

File I/O Christian Schumacher, Info1 D-MAVT 2013

2 nd Week Lecture Notes

C++ Tutorial AM 225. Dan Fortunato

Types, Values, Variables & Assignment. EECS 211 Winter 2018

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

beginslide Chapter 6 The C++ Programming Language The Role of Classes

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

BITG 1233: Introduction to C++

Programming Language. Functions. Eng. Anis Nazer First Semester

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Computing and Statistical Data Analysis Lecture 3

EC 413 Computer Organization

C++ Programming Lecture 11 Functions Part I

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

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

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Program Organization and Comments

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Lab # 02. Basic Elements of C++ _ Part1

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

C C C C++ 2 ( ) C C++ 4 C C

EEE145 Computer Programming

BİL200 TUTORIAL-EXERCISES Objective:

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

Bruce Merry. IOI Training Dec 2013

Basic Types, Variables, Literals, Constants

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

PHY4321 Summary Notes

CS11 Intro C++ Spring 2018 Lecture 1

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

CSE 333 Autumn 2013 Midterm

Transcription:

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

Objectives Recap C Differences between C and C++ IO Variable Declaration Standard Library Introduction of C++ Feature : Class Programming in C++ 2

Recap C Built in Types Enumerated Type Derived data type struct union Array pointer Type Modifiers short long signed unsigned Programming in C++ 3

Recap C Variable Is a name given to a storage area Operator Literal Expression Must have a value Statement Command for a specific action Expression statement Control statement Programming in C++ 4

Quiz #1 struct st1 { int a; char c; int b[10]; ; What is the size of struct st1? Without using sizeof operator Programming in C++ 5

DIFFERENCES BETWEEN C AND C++ Programming in C++ 6

Hello World Program #include <stdio.h> int main() { printf("hello World"); printf( \n"); return 0; #include <iostream> int main() { std::cout << "Hello World"; std::cout << std::endl; return 0; Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 7

Hello World Program IO header is stdio.h printf is used to print to stdout stdout prints to console printf is declared in global scope printf is a variadic function \n is the escaped new line character IO header is iostream Operator << is used stream to std::cout Std::cout is ostream which prints to console cout declared in std namespace << is a binary operator Std::endl is newline functor Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 8

Program #2 #include <stdio.h> int main() { int a, b; int sum; printf( Input two numbers:\n ); scanf( %d%d, &a, &b); sum = a + b; printf( sum of %d and %d is: %d\n, a, b, sum); return 0; #include <iostream> Using namespase std; int main() { int a, b; cout << Input two numbers:\n ; cin >> a >> b; int sum = a + b; cout << sum of << a << and << b << is: << sum << endl; return 0; Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 9

Program #3 #include <stdio.h> #include <math.h> int main() { double a, b; printf( Input number:\n ); scanf( %lf, &a); b = sqrt(a); printf( sq root of %lf is: %lf\n, a, b); return 0; #include <iostream> #include <cmath> Using namespase std; int main() { double a; cout << Input number:\n ; cin >> a; double b = sqrt(a); cout << sq root of << a << is: << b << endl; return 0; Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 10

Standard Library Header Conventions C Heaeder C program Use.h #include <stdio.h> Names in Global namespace C++ program Prefix c no.h #include <cstdio> Names in std namespace C++ Header Not applicable No.h #include <iostream> #include <cmath> std::sqrt(9.0); #include <math.h> sqrt(9.0); Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 11

Namespace std : C++ standard library C Standard library All names are global stdout stdin printf scanf sort etc. C++ Standard library All names are within std namespace std::cout std::cin std::sort etc Source: Programming in C++ by Prof. Partha Pratim Das Programming in C++ 12

Namespaces Name Clash Occurs when building large systems from pieces Same globally-visible names Very difficult to fix Defines scope for enclosing global declarations namespace Mine { void print(int); float pi = 3.1415925635; void bar(float y) { float x = y + Mine::pi; Mine::print(5); Programming in C++ 13

Namespaces using directive brings namespaces or objects into scope namespace Mine { float pi = 3.1415926535; void print(int); using Mine::print; void foo() { print(5); // invoke Mine::print using namespace Mine; float twopi = 2*pi; // Mine::pi Programming in C++ 14

Namespaces Namespaces are open: declarations can be added namespace Mine { void f(int); namespace Mine { void g(int); // Add Mine::g() to Mine Programming in C++ 15

Namespaces Declarations and definitions can be separated namespace Mine { void f(int); void Mine::f(int a) { /* */ Programming in C++ 16

bool data type #include <stdio.h> #define TRUE 1 #define FALSE 0 int main() { int a = TRUE; printf( bool is %d\n, a); return 0; #include <iostream> Using namespase std; int main() { bool a = true; cout << bool is << a << endl; return 0; bool is a built-in data type true is a literal false is a literal Prints 1 for true and 0 for false Programming in C++ 17

new/delete operators In C++, the new and delete operators provide built-in language support for dynamic memory allocation and de-allocation. int *pi = new int; int *pi = new int(102); //new initializes!! int *parr = new int[4*num]; Arrays generally cannot be initialized. delete pi; delete [] parr; new does more than malloc! Programming in C++ 18

new/delete & malloc/free All C++ implementations also permit use of malloc and free routines. Do not free the space created by new. Do not delete the space created by malloc Results of the above two operations is memory corruption. Matching operators malloc-free new-delete new[] delete [] It is a good idea to use only new and delete in a C++ program. Programming in C++ 19

Summary C++ gives more flexibility in terms of variable declaration and IO C functions are simplified in C++ Ease of programming Defining own namespace Built-in type bool Programming in C++ 20

C++ FEATURE : CLASS Programming in C++ 21

Example: A stack in C typedef struct { char s[size]; int sp; Stack; stack *create() { Stack *s; s = (Stack *)malloc(sizeof(stack)); s->sp = 0; return s; Creator function ensures stack is created properly. Does not help for stack that is automatic variable. Programmer could inadvertently create uninitialized stack. Programming in C++ 22

Example: A stack in C char pop(stack *s) { if (sp = 0) error( Underflow ); return s->s[--sp]; void push(stack *s, char v) { if (sp == SIZE) error( Overflow ); s->s[sp++] = v; Not clear these are the only stackrelated functions. Another part of program can modify any stack any way it wants to, destroying invariants. Temptation to inline these computations, not use functions. Programming in C++ 23

C++ Solution: Class class Stack { char s[size]; int sp; Definition of both representation and operations public: ; Stack() { sp = 0; void push(char v) { if (sp == SIZE) error( overflow ); s[sp++] = v; char pop() { if (sp == 0) error( underflow ); return s[--sp]; Public: visible outside the class Constructor: initializes Member functions see object fields like local variables Programming in C++ 24

C++ Stack Class Natural to use Stack st; st.push( a ); st.push( b ); char d = st.pop(); Stack *stk = new Stack; stk->push( a ); stk->push( b ); char d = stk->pop(); Programming in C++ 25

C++ Stack Class Members (functions, data) can be public, protected, or private class Stack { char s[size]; public: char pop(); ; Stack st; st.s[0] = a ; // Error: sp is private st.pop(); // OK Programming in C++ 26