Introduction to C++ IT 1033: Fundamentals of Programming

Similar documents
6.096 Introduction to C++ January (IAP) 2009

Understanding main() function Input/Output Streams

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Your First C++ Program. September 1, 2010

4. Structure of a C++ program

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.1

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

Fundamentals of Programming. By Budditha Hettige

Chapter 1 Introduction to Computers and C++ Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

2 nd Week Lecture Notes

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Lecture 2 Tao Wang 1

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

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

BITG 1233: Introduction to C++

My First Command-Line Program

UNIT- 3 Introduction to C++

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

UEE1302 (1102) F10: Introduction to Computers and Programming

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Computer Programming : C++

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Programming in C++ 4. The lexical basis of C++

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

CSc Introduction to Computing

Dr. Md. Humayun Kabir CSE Department, BUET

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

CSI33 Data Structures

Chapter 2: Introduction to C++

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

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 2: Overview of C++

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

Chapter 2 Basic Elements of C++

Maciej Sobieraj. Lecture 1

Introduction to C++ Chapter

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Computer Programming

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

C++ Lab 03 - C++ Functions

Basic Elements of C. Staff Incharge: S.Sasirekha

Probably the best way to start learning a programming language is with a program. So here is our first program:

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

IS 0020 Program Design and Software Tools

CHAPTER-6 GETTING STARTED WITH C++

Introduction of C++ OOP forces us to think in terms of object and the interaction between objects.

Chapter 2, Part I Introduction to C Programming

Full file at C How to Program, 6/e Multiple Choice Test Bank

1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?

Introduction to Programming

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

C++ Code Structure. Cooperating with the Compiler

1

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

Introduction to Programming using C++

Data Structures and Programming with C++

Chapter 2: Basic Elements of C++

LECTURE 02 INTRODUCTION TO C++

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

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

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Introduction. Arizona State University 1

LESSON 4. The DATA TYPE char

Chapter Two MULTIPLE CHOICE

Introduction to C++ CS 1: Problem Solving & Program Design Using C++

Programming. C++ Basics

Chapter 2: Programming Concepts

Compiling C++ Programs Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

A First Program - Greeting.cpp

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

C++ Programming Basics

IT 1033: Fundamentals of Programming Data types & variables

1.1 Introduction to C Language. Department of CSE

Programming with C++ as a Second Language

Basic Types, Variables, Literals, Constants

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to C++ (using Microsoft Visual C++)

Lecture Set 2: Starting Java

C: How to Program. Week /Mar/05

C++ INDEX. Introduction: Instructions for use. Basics of C++: Structure of a program Variables. Data Types. Constants Operators Basic Input/Output

Chapter 1 INTRODUCTION

Manual. Subject Code: CS593. Computer Science and Engineering

Lecture Set 2: Starting Java

UNIT-2 Introduction to C++

Data Types and Variables in C language

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Exceptions, Case Study-Exception handling in C++.

Chapter 2 - Introduction to C Programming

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Object-Oriented Programming

Scientific Computing

Chapter 1 Introduction to Computers and Programming

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

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Objectives. In this chapter, you will:

4. Inputting data or messages to a function is called passing data to the function.

Transcription:

2 Introduction to C++ IT 1033: Fundamentals of Programming Budditha Hettige Department of Computer Science

C++ C++ is a middle-level programming language Developed by Bjarne Stroustrup Starting in 1979 at Bell Labs C++ runs on a variety of platforms Windows, Mac OS, various versions of UNIX Designed with a bias toward system programming and embedded 7/11/2015 Budditha Hettige (budditha@yahoo.com) 2

C++ A Programming Language Artificial Language General-purpose programming language Features Object-oriented & generic programming features Low-level memory manipulation Consists of Key words Syntax Semantics budditha@yahoo.com 3

C++ Vs Natural Languages C++ Artificial Language Consist of Keywords Syntax Semantics Translate through the Compilers Natural Language Natural Consist of Words Syntax Semantics Translate trough the Machine Translation systems or Human budditha@yahoo.com 4

Key words/ words C++ Natural Language budditha@yahoo.com 5

Syntax C++ Rules for construction of valid statements, including, Order of words, Punctuation Natural Language Grammar rules, subject, object, verbs etc. budditha@yahoo.com 6

Semantics C++ The set of rules that determines the meaning of instructions (what the computer will do) written in a programming language. Natural Language Is the study of meaning budditha@yahoo.com 7

Minimum C++ Program Do nothing budditha@yahoo.com 8

C++ Block A block is a set of logically connected statements that are surrounded by opening and closing braces. budditha@yahoo.com 9

Blocks C A B budditha@yahoo.com 10

C++ semicolon The semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. budditha@yahoo.com 11

Example Create a C++ program to print message on console window budditha@yahoo.com 12

Example This is a comment line. All lines beginning with two slash signs (//) or (/*) are considered comments and do not have any effect on the behavior of the program. // Single line comment /* */ block comment Block comments cannot be nested budditha@yahoo.com 13

Example Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines with expressions but indications for the compiler's preprocessor. In this case the directive #include <iostream> tells the preprocessor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input-output library in C++, and it is included because its functionality is going to be used later in the program. budditha@yahoo.com 14

What is preprocessor. Is a program that processes its input data to produce output that is used as input to another program. The preprocessor provides the ability for the inclusion of header files. Is a separate program invoked by the compiler as the first part of translation Example: budditha@yahoo.com 15

Preprocessor in a Program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 16

Example All the elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std Use Only for GCC compilers budditha@yahoo.com 17

Example Beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. All C++ programs have a main function budditha@yahoo.com 18

Example Is a C++ statement. This statement performs the only action that generates a visible effect in our first program. Command budditha@yahoo.com 19

Example The return statement causes the main function to finish. budditha@yahoo.com 20

Cording, Editing, compiling and run a program Create new Code:blocks project Add following code Compile and run 7/11/2015 Budditha Hettige (budditha@yahoo.com) 21

Code::Blocks Code::Blocks IDE Project Source Code Source file Output budditha@yahoo.com 22

Example 2 Create a C++ program to display your name and address budditha@yahoo.com 23

Cording Styles 7/11/2015 Budditha Hettige (budditha@yahoo.com) 24

New Line budditha@yahoo.com 25

Example 2 What is output of the following program Output budditha@yahoo.com 26

Escape sequences Escape sequences are used to represent certain special characters within string literals ( ) Escape sequence Description \' single quote \" double quote \? question mark \\ backslash \a audible bell \b backspace \f form feed - new page \n line feed - new line \r carriage return \t horizontal tab budditha@yahoo.com 27

ASCII art with C++ ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from cout<<" @ @ @ \n ; cout<<" @ @ @ @ \n ; cout<<" @ @ @ \n ; cout<<" \\ / \n ; cout<<" \n ; cout<<" \n ; cout<<" \n ; cout<<" \n ; cout<<" ( ) \n ; cout<<" ^^^^^ \n ; budditha@yahoo.com 28

Example Write a C++ program to display your name using Ascii Art 7/11/2015 Budditha Hettige (budditha@yahoo.com) 29

Exercise 1. Write a C++ Program to Display the Following output ------------------------------- C1033 Fundamentals of Programming ------------------------------- budditha@yahoo.com 30

Different ways to create a C++ program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 31

Different ways to create a C++program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 32

Different ways to create a C++program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 33

Different ways to create a C++program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 34

Template for a C++ Program 7/11/2015 Budditha Hettige (budditha@yahoo.com) 35

C++ Programming C++ Source File (.cpp) C++ Compiler program ) budditha@yahoo.com Operating system can directly execute 36

Preparing a C++ program for running budditha@yahoo.com 37

Compiling budditha@yahoo.com 38

Compile errors

Compilation Errors Compiler fails to compile a piece of computer program source code. Error message is given budditha@yahoo.com 40

Common C++ compilation errors Undeclared identifier Common function undeclared = expected Internal compiler error Unexpected closing brace budditha@yahoo.com 41

Example Write the following C++ program and identify Compilation errors budditha@yahoo.com 42

Example Correct errors and rewrite the program budditha@yahoo.com 43

Clear the console screen Header Command 7/11/2015 Budditha Hettige (budditha@yahoo.com) 44

Change console Text and background color Sets the default console foreground and background colours. Syntax COLOR [background][foreground] system("color FA"); system("color F0"); 7/11/2015 Budditha Hettige (budditha@yahoo.com) 45

Color Code 0 = Black 1 = Blue 2 = Green 3 = Aqua 4 = Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = Light Blue A = Light Green B = Light Aqua C = Light Red D = Light Purple E = Light Yellow F = Bright White 7/11/2015 Budditha Hettige (budditha@yahoo.com) 46

Example Write a C++ program to display following screen USER INFORMATION ------------------------------- NAME : B. HETTIGE ADDRESS: No23, Panadura AGE : 19 SALARY : 23500 GENDER : M ------------------------------- 7/11/2015 Budditha Hettige (budditha@yahoo.com) 47

Summary C++ Programming Language? C++ Vs Natural Languages C++ Syntax Create a C++ program using code:blocks Cording styles ASCII Art Handle compile errors Customize Console screen budditha@yahoo.com 48