A SHORT COURSE ON C++

Similar documents
Scientific Computing

C++ Support Classes (Data and Variables)

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

Summary of basic C++-commands

Introduction to C ++

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 2. Outline. Simple C++ Programs

Your first C++ program

Introduction to Programming using C++

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

CS2255 HOMEWORK #1 Fall 2012

Programming Language. Functions. Eng. Anis Nazer First Semester

Engineering Problem Solving with C++, Etter/Ingber

Computer Programming : C++

A brief introduction to C++ and Interfacing with Excel

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:

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

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

2 nd Week Lecture Notes

Computing and Statistical Data Analysis Lecture 3

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

I/O Streams and Standard I/O Devices (cont d.)

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Lab Instructor : Jean Lai

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

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:

Chapter 1 Introduction to Computers and C++ Programming

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

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

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Understanding main() function Input/Output Streams

6.096 Introduction to C++ January (IAP) 2009

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

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++

CSCE 110 PROGRAMMING FUNDAMENTALS

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

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

CSCS 261 Programming Concepts Exam 1 Fall EXAM 1 VERSION 1 Fall Points. Absolutely no electronic devices may be used during this exam.

Chapter 2: Introduction to C++

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

Objectives. In this chapter, you will:

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

CHAPTER 3 BASIC INSTRUCTION OF C++

1. a) What #include statement do you put at the top of a program that does uses cin, cout or endl?

120++: a C++ Subset Corresponding to A Project-Based Introduction to C++

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

A Freshman C++ Programming Course

A First Program - Greeting.cpp

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

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

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

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

Introduction to C ++

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

C++ Quick Guide. Advertisements

Chapter 1 INTRODUCTION

Chapter 3 - Functions

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Local and Global Variables

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

BITG 1233: Introduction to C++

Name SECTION: 12:45 2:20. True or False (12 Points)

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

Your First C++ Program. September 1, 2010

Computer Science II Lecture 1 Introduction and Background

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

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

CSCE Practice Midterm. Data Types

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

CSCE Practice Midterm. Data Types

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

Dr. Md. Humayun Kabir CSE Department, BUET

Fundamentals of Programming CS-110. Lecture 2

CSCI 1061U Programming Workshop 2. C++ Basics

Name Section: M/W T/TH Number Definition Matching (6 Points)

(created by professor Marina Tanasyuk) FUNCTIONS

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

Chapter 2: Overview of C++

The C++ Language. Arizona State University 1

C++ for Python Programmers

LECTURE 02 INTRODUCTION TO C++

Introduction to Programming

LOGO BASIC ELEMENTS OF A COMPUTER PROGRAM

Programming. C++ Basics

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Name Section: M/W T/TH Number Definition Matching (8 Points)

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

WARM UP LESSONS BARE BASICS

G52CPP C++ Programming Lecture 17

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

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

Transcription:

Introduction to A SHORT COURSE ON School of Mathematics Semester 1 2008

Introduction to OUTLINE 1 INTRODUCTION TO 2 FLOW CONTROL AND FUNCTIONS If Else Looping Functions Cmath Library Prototyping

Introduction to A structured language can hide information from the rest of the program. Structuring code and data allows easy upgrades many programmers to work on a large project Object-oriented programming imposes a high level of structure Problems are broken down into subproblems, and then into self-contained units called objects Common traits of object-oriented languages are: encapsulation polymorphism inheritance

Introduction to USING OBJECTS 1 Encapsulation: functions and data inside an object have restricted access. 2 Polymorphism: represents the concept of one interface, multiple method. The same interface can be used to do different things for different objects: i.e. define + to add numbers, but perform string concatenation on characters and strings, a + b = ab. 3 Inheritance: allows one object to acquire the properties of another. An example would be to define a generic object car that has a steering wheel, four wheels and an engine. The new object sports car inherits all these properties and adds a sun roof, go-faster stripes and a huge stereo. university-logo

WRITING Introduction to The key elements of C/ syntax are: Semicolon used to mark end of statements Case is important Totally free form, lines and names can be as long as you like! Comments take the form /* C style comment */ or // style comment Code blocks are surrounded by braces {}

Introduction to A VERY SIMPLE CODE main() { } The following is a program. There are no commands to execute. If we save it in the file simple prog.cc, we can compile and run it with the commands: > c++ simple prog.cc >./a.out

Introduction to INTRINSIC VS. INCLUDE Unlike Fortran, there are almost no intrinsic functions in This includes the ability to print to screen. We can include standard libraries for: Advanced Storage Strings Mathematical functions The syntax for including libraries is: #include <library name > Include statements must appear before any other statements.

Introduction to HELLO WORLD A simple example of the standard input/output library: #include <iostream> using namespace std; main(){ cout << Hello World! << endl; } The output at a terminal will look like: > c++ hello world.cc >./a.out Hello World! university-logo

Introduction to STANDARD DATA TYPES There are six basic data types in : char CHARACTER int INTEGER float REAL double REAL(dp) bool LOGICAL void Corresponding fortran variables are shown in red. We use void for functions that do not return a value (SUBROUTINE).

Introduction to DECLARING VARIABLES We may declare variables anywhere in the code. Variables will be localised to the block in which they are declared What is the output from the following? #include <iostream> using namespace std main() { int i=0; cout << " i= " << i << endl; { int i=10; cout << " i= " << i << endl; } cout << " i= " << i << endl; } university-logo

Introduction to ARRAYS We declare and reference arrays using square brackets []. int array[100]; // 100 integer array array[0] = 0; array[1] = 1 + array[0] Arrays are indexed from 0, and this cannot easily be changed. Multidimensional arrays are declared in the obvious way int array 2D[5][5]; // 2D array array 2D[0][0] = 0;

Introduction to OPERATORS We have the same simple operators + - * / like Fortran There is no equivalent to the power ** There are three extra operators: % is the modulus operator, giving the remainder of integer division ++ adds 1 to its operand -- takes 1 away from its operand We can write the code x=x+1 as x++.

Introduction to SIMPLE INPUT AND OUTPUT We use stream variables to access the screen, keyboard and files. They are like UNITS in Fortran. We need to include stream libraries at the top of the program #include<iostream> using namespace std main(){ int i cout << " Enter a number. " << endl; cin >> i; //read in a number cout << " Your number is " << i << endl; } university-logo

Introduction to SIMPLE INPUT AND OUTPUT cout is the standard screen variable, and cin the standard keyboard variable To pass data to and from the stream we use the << and >> operators. << data is passed right to left, in the example the string is passed to cout >> data is passed left to right, in the example the integer is passed from cin to i Multiple bits of data can be passed to the stream by stringing them together in the same command. Use endl to finish a line. university-logo

Introduction to FILE INPUT AND OUTPUT To read and write to files we must include the fstream library. Input streams have type ifstream, and output streams ofstream ifstream file input; // an input file stream ofstream file output; // an output file stream ifstream and ofstream have intrinsic functions to open and close files. We can also check if the file is open with the is open() function. file input.open("input.in"); // open file input.in if(file input.is open()) // check file is open university-logo

Introduction to IF, ELSE IF AND ELSE If Else Looping Functions Cmath Library Prototyping We can use if, else if, and else to control flow through the program. int i; cout << " Enter a number " << endl; cin >> i; if(i<0)cout << " i is negative" << endl; else if(i==0)cout << " i is zero" << endl; else cout << " i is positive" << endl;

Introduction to IF, ELSE IF AND ELSE If Else Looping Functions Cmath Library Prototyping To execute more than one command on an if condition use blocks if(condition){ // lots of commands in here } else { // and in here too. }

FOR LOOPS Introduction to If Else Looping Functions Cmath Library Prototyping The general form for a loop is for(initialisation; condition; increment) statement; We can loop over multiple commands using a block for(int i=0;i<10;i++){ temp = i*10; cout << " value " << temp << endl; }

EXITING A LOOP Introduction to If Else Looping Functions Cmath Library Prototyping The command break can be used like the command EXIT in Fortran. for(int loop=0;loop<iter max;loop++){ solve for U(u,y,U); if(residual(x,y,u)<tolerance)break; }

FUNCTIONS Introduction to If Else Looping Functions Cmath Library Prototyping The general syntax for a function is: data type function name(arguments) { function statements } Functions must be declared before the main program. All functions must return a value of the data type specified in the declaration. Even if this is void!

Introduction to EXAMPLE FUNCTION If Else Looping Functions Cmath Library Prototyping #include<iostream> using namespace std // square an integer int square(int i) { return i*i; } // Main Program main(){ int number=5 cout << square(number) << endl; }

Introduction to If Else Looping Functions Cmath Library Prototyping ACCESSING THE MATH LIBRARY Simply include the library at the top of your code: #include<cmath> All of the trigonometric, hyperbolic and exponential functions are present. There is also a pow(x,y) to raise x to the power y. and a sqrt() function.

Introduction to If Else Looping Functions Cmath Library Prototyping A function must be defined before it can be called. Use prototypes to declare functions before they are used. data type function name(arguments) The main body of the function can be placed somewhere else in the code (or even a separate file) This is like the EXTERNAL declaration in Fortran.