An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout << “Hello world” << endl; }

Slides:



Advertisements
Similar presentations
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
1 CIS 205 Practice Test George Lamperti A word that has a predefined meaning in a C++ program and cannot be used as a variable name is known as.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Help session - C++ Arranged by : IEEE – NIIT Student Chapter.
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
Control Structures (B) Topics to cover here: Sequencing in C++ language.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
1 CSC 222: Computer Programming II Spring 2004  classes and objects  abstract data types, classes and objects  using existing classes, #include, member.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Fundamental Programming Fundamental Programming Introduction to Functions.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Array. Array is a group of data of the same type. Array elements have a common name –The array as a whole is referenced through the common name Individual.
INPUT & OUTPUT 10/20/2016Department of Computer Science, UOM | Introduction | Fakhre Alam.
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
Basic Elements of C++.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Introduction to C++ October 2, 2017.
Basic Elements of C++ Chapter 2.
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Programming Funamental slides
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Array Data Structure Chapter 6
Fundamental Programming
C++ Basics CSci 107. A C++ program //include headers; these are modules that include functions that you may use in your //program; we will almost always.
Presentation transcript:

An Introduction to C++ A First Look

void Functions #include #include void main( ) { cout << “Hello world” << endl; }

void Functions #include #include void main() { cout << “Hello world” << endl; } Hello world program execution

Input and Output The term stream refers to the abstract model of an input or output device in which the input device produces a stream of characters and an output device receives a stream of characters. #include #include cin and cout are defined cin and cout are defined #include #include files may be created/, modified & deleted files may be created/, modified & deleted

Streams C++ programmers say that the output operation ‘inserts’ characters into an output stream << insertion operator The input operation ‘extracts’ characters from an input stream. >> extraction operator

cin, cout cout << “Enter a word: “; cin >> aword; cout << “Thanks for entering “ << aword ; cout << endl; << insertion operator >> extraction operator

Functions that return things #include #include int main( ) { int number; cout << “Enter a number : “; cin >> number; cout << “Thanks for “ << number << endl; return 0; }

Functions that return things #include #include int main() { int number; cout << “Enter a number : “; cin >> number; cout << “Thanks for “ << number << endl; return 0; } Enter a number: 10 Thanks for 10 program execution

C++ built-in Datatypes n int (e.g. -6) n long (e.g ) n float (e.g. -8.4) n double (e.g. 3.14) n bool (e.g. true) n char (e.g. ‘a’)

C++ built-in Datatypes n int (e.g. -6) n long (e.g ) n float (e.g. -8.4) n double (e.g. 3.14) n bool (e.g. true) n char (e.g. ‘a’) Used on AP exams

Other Datatypes implemented using Classes n apstring name; (e.g. “Smith”) n apvector A (e.g. integer arrays) n apmatrix A (e.g. multi-dimensional arrays) n BigInt number (e.g ) n apstack A (e.g. stacks) n apqueue A (e.g. queues) AB Exam Only

Preprocessor n pre-processor commands begin with # n pre-processor commands are serviced first by the pre-processor which occurs before compilation

Preprocessor n #include is a pre-processor directive which inserts the contents of the file into the current file #include #include – angle brackets indicate to get the *.h file from a previous defined ‘header directory’, which is known to the environment #include “apstring.h” #include “apstring.h” – double quotes mean that the file to merge in, is found in current directory

Preprocessor... #ifndef _DICE_H #define _DICE_H...#endif The above technique is standard practice for most header files. IFNDEF ensures that the code sandwiched between IFNDEF and ENDIF, will not be seen by the compiler if the symbol _DICE_H has already been defined. This trick ensures that this code is only given once to the compiler.

Function Prototypes A function’s prototype is the first line of the function void GetPayment (double amt) {}

Function Prototypes The compiler needs the function prototype for each function made available to it, unless the function has been defined prior to its use.

Function Prototypes

Library functions have their prototypes defined within ‘header files’ located at the top of most C++ programs. #include #include void main() {}

/* math.h standard header */ #ifndef __MATH_H #define __MATH_H /* float declarations */ float cos(float x); float sin(float x) ; float tan(float x); #endif Inside.... #include Inside.... #include

Parameter Passing n pass by VALUE int SQUARED ( int N ); int SQUARED ( int N ); function SQUARED ( N : integer) : integer; function SQUARED ( N : integer) : integer; n pass by REFERENCE int SQUARED ( int & N); int SQUARED ( int & N); function SQUARED (var N : integer) : integer; function SQUARED (var N : integer) : integer; n pass by CONSTANT REFERENCE int SQUARED (const int & N); int SQUARED (const int & N);

Example Write a C++ main program which asks the user to enter an integer called, ‘N’. Output the absolute value of N by writing a function called, ABSOLUTE(N). ABSOLUTE(N). Sample Session: Enter N: -9 Enter N: -9 Absolute Value = 9

Solution #include #include int ABSOLUTE( int N); int main() { int N ; int N ; cout << “Enter N: “ ; cout << “Enter N: “ ; cin >> N; cin >> N; cout << “Absolute Value = “ cout << “Absolute Value = “ << ABSOLUTE(N) << endl; << ABSOLUTE(N) << endl; return 0; return 0;}

Solution int ABSOLUTE( int N); { int value=N; if (value < 0) value = -1 * value; value = -1 * value; return value; }

Constants #define MAXWORDS 1000 OR BETTER.... TO DO const int MAXWORDS = 1000;

Conditions If ( numA == numB) numC = numA; else numC = numB; == equals != not equal to < less than > greater than <= less than or equal to equal to >= greater than or equal to or equal to && AND || OR

Exercise Write a C++ program which constructs 3 double variables called, ‘X’, ‘Y’ & ‘Z’. Output the middle number of ‘X’, ‘Y’ & ‘Z’. Sample Session: Sample Session: Enter x: 6 Enter x: 6 Enter y: 5 Enter z: 7 The middle number is 6

Solution #include #include int main() {double x, y, z, sum, highest, lowest, middle; cout > x; cout > y; cout > z; sum = x + y + z; highest = x;//Determine highest if (y > highest) highest = y; if (z > highest) highest = z; lowest = x;//Determine lowest if (y < lowest) lowest = y; if (z < lowest) lowest = z; middle = sum - highest - lowest; cout << “The middle number is “ << middle; return 0; }

Classes n Classes are objects with behaviours DICE CLASS Object Red Red Behaviours Roll() Roll() NumSides() NumSides() NumRolls() NumRolls()

Classes n Classes are objects with behaviours DICE CLASS Object Red Red Behaviours Roll() Roll() NumSides() NumSides() NumRolls() NumRolls() Member Functions a Red die

The Dice Class #include #include #include “dice.h” int main( ) {int g, r, totalrolls; Dice red(6), green(6); g = green.Roll( );// Roll the Dice r = red.Roll( ); totalrolls = red.NumRolls( ) + green.NumRolls( ); cout << “Sum: “ << g + r << endl;; cout << “Total : “ << totalrolls << endl; return 0; }

Exercise Write a C++ program which constructs 5 red dice with names, r1, r2, r3, r4 & r5. r1, r2, r3, r4 & r5. Construct an integer variable called, ‘points’ and initialize it to 0. Roll the 5 dice once! If a Yahtzee occurs increment the points variable by 50.

Solution #include #include #include “dice.h” int main( ) {int points=0; Dice r1(6), r2(6), r3(6), r4(6), r5(6); int a = r1.Roll( ); int b = r2.Roll( ); int c = r3.Roll( );int d = r3.Roll( ); int e = r5.Roll( ); if ((a==b) && (a==c) && (a==d) && a==e)) a==e)) points = points + 50; cout << points; return 0; }