C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
C++ Basics Part I Part II. Slide 2 Part I: basics.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
C++ Basics. COMP104 C++ Basics / Slide 2 Introduction to C++ * C++ is a programming language for manipulating numbers and user-defined objects. * C++
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
Programming Introduction to C++.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Introduction to C++ Programming COMP102 Prog. Fundamentals I:Introduction to C++ / Slide 2 Introduction to C++ l C is a programming language developed.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
C++ Lesson 1.
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Programming Introduction to C++.
Basics (Variables, Assignments, I/O)
Mr. Shaikh Amjad R. Asst. Prof in Dept. of Computer Sci. Mrs. K. S
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
Basics (Variables, Assignments, I/O)
2.1 Parts of a C++ Program.
Variables in C Topics Naming Variables Declaring Variables
C++ Basics.
Variables, Identifiers, Assignments, Input/Output
Variables in C Topics Naming Variables Declaring Variables
COMS 261 Computer Science I
Chapter 2: Introduction to C++.
Programming Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Programming Language C Language.
COMS 261 Computer Science I
Variables in C Topics Naming Variables Declaring Variables
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
The Fundamentals of C++
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

C++ Basics Programming

COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system l C programs are efficient and portable across different hardware platforms l C++ is an extension of the C language n C programs should run in C++ l C++ supports “object-oriented” programming

COMP104 Lecture 5 / Slide 3 General form of a C++ program //Program description is first #include directives go next using namespace std; int main(){ constant declarations go here variable declarations go here assignment statements go here return 0; }

COMP104 Lecture 5 / Slide 4 General form of a C++ program //simple program #include using namespace std; int main(){ // constant declaration const double Pi = ; // variable declarations double radius; double area; // assignment statements cout << "Enter circle radius: "; cin >> radius; area = Pi * radius * radius; cout << "Area : " << area << endl; return 0; }

COMP104 Lecture 5 / Slide 5 Syntax of the C++ Language l Reserved words (appear in blue in Visual C++) n Reserved words have a special meaning in C++. n The list of reserved words: asm, auto, bool, break, case, catch, char, class, const, continue, default, delete, do, double, else, enum, extern, float, for, friend, goto, if, include, inline, int, long, namespace, new, operator, private, protected, public, register, return, short, signed, sizeof, static, struct, switch, template, this, throw, try, typedef, union, unsigned, using, virtual, void, volatile, while

COMP104 Lecture 5 / Slide 6 Syntax of the C++ Language l Identifiers (appear in black in Visual C++) n An identifier is a name for variables, constants, functions, etc. n It consists of a letter followed by any sequence of letters, digits or underscores n Names are case-sensitive. The following are unique identifiers: Hello, hello, whoami, whoAMI, WhoAmI n Names cannot have special characters in them e.g., X=Y, J-20, #007, etc. are invalid identifiers. n C++ reserved words cannot be used as identifiers. n Choose identifiers that are meaningful and easy to remember.

COMP104 Lecture 5 / Slide 7 Syntax of the C++ Language l Comments (appear in green in Visual C++) n Comments are explanatory notes n not compiled (i.e. no executable code is generated for comments) n Comments are done in two ways: // A double slash starts a single line comment /* A slash followed by an asterisk marks the start of a multiple line comment. It ends with an asterisk followed by a slash */

COMP104 Lecture 5 / Slide 8 Syntax of the C++ Language Compiler Directive: #include n It refers to a header file of library functions or variables (e.g. input and output). n When you compile your C++ program, the compiler reads in the contents of the file before compiling the rest of your program. n The included file is compiled together with the program. There are two forms of #include : #include // for pre-defined files #include "my_lib.h" // for user-defined files

COMP104 Lecture 5 / Slide 9 Libraries #include loads the code from the standard libraries #include // new I/O library #include // old I/O library #include // standard functions #include // math functions #include // contains random funct #include // time function using namespace std; indicates that the new C++ libraries should be used. If this line is left out, then the old iostream library is loaded: #include

COMP104 Lecture 5 / Slide 10 Constant Declarations l Constants represent permanent values. l Their values can only be set in the declaration: const double pi = ; l They can make a program more readable and maintainable Constant declaration syntax: const = ; Examples: const double US2HK = 7.8; const double HK2Yuan = 1.07; const double US2Yuan = US2HK* HK2Yuan;

COMP104 Lecture 5 / Slide 11 Variable Declarations l A variable is best thought of as a container for a value with an address: Variable declaration syntax: ; Examples: int nickel; int penny; l A variable must be declared before it can be used. int main(){ x = 5;// illegal: x was not declared }

COMP104 Lecture 5 / Slide 12 Variable Declarations l A variable can be initialized in a declaration: int x = 3; l Several variables of the same type can be declared in the same declaration: double total_USD, area; A variable must have only one type. For example, a variable of the type int can only hold integer values.

COMP104 Lecture 5 / Slide 13 Types of Variable Declarations l Simple C++ variable types: int integer (32-bit integer on the PC) (example: 1) short 16-bit integer (allows ±32,767) long 32-bit integer (allows ±2,147,483,647) float floating point number (allows about 7 digits of precision: ) double double precision float (allows about 15 digits of precision: ) char a single character (example: ‘y’) BINARY DIGIT

COMP104 Lecture 5 / Slide 14 Simple Number System l Decimal l What is the maximum value a 3 decimal digit number can hold? n 999 l Binary l What is the maximum value a 16-binary digit number can hold? n one bit for sign –0: positive (or zero) –1: negative n Decimal base # digits Binary base

COMP104 Lecture 5 / Slide 15 Memory Depiction float y = 12.5; short Temperature = 32; char Letter = 'c'; short Number; Memory location

COMP104 Lecture 5 / Slide 16 Assignment Statements Assignment syntax: = ; Examples: int n, m, k;// declaration n = 5; m = 6 + (4 * 6); k = (n * 2) + m; k = k / 2;

COMP104 Lecture 5 / Slide 17 Assignment Statements l A variable must be assigned a value before it can be used. Variables are not automatically initialized in VC++. int x, y;// x declared, but not initialized // x and y have random values y = x;// the random value in x assigned to y l Once a value has been placed in a variable, it stays there until the program changes it.

COMP104 Lecture 5 / Slide 18 Assignment Statements int NewStudents = 6; int OldStudents = 21; int TotalStudents; TotalStudents = NewStudents + OldStudents ;

COMP104 Lecture 5 / Slide 19 Assignment Statements TotalStudents = NewStudents + OldStudents; OldStudents = TotalStudents;

Value1 = 10; Value2 = 20; int Hold = Value1; Value1 = Value2; Value2 = Hold;

COMP104 Lecture 5 / Slide 21 Arithmetic Operators l Common Addition + Subtraction - Multiplication * Division / Mod % l Note n No exponentiation operator

COMP104 Lecture 5 / Slide 22 Integer Division l Integer division produces an integer result n It rounds down the result l Examples 3 / 2 evaluates to 1 4 / 6 evaluates to 0 10 / 3 evaluates to 3

COMP104 Lecture 5 / Slide 23 Mod l Produces the remainder of the division l Examples 5 % 2 evaluates to 1 12 % 4 evaluates to 0 4 % 5 evaluates to 4

COMP104 Lecture 5 / Slide 24 Converting inches to feet and inches l 63 inch = 5 foot 3 inch l 5 = 63 / 12 l 3 = 63 % 12

COMP104 Lecture 5 / Slide 25 Operators and Precedence l Which of the following is it equivalent to mx + b ? n (m * x) + b n m * (x + b) l Operator precedence tells how to evaluate expressions l Standard precedence order ( ) Evaluated first, if nested innermost done first * / % Evaluated second. If there are several, then evaluate from left-to-right + - Evaluate third. If there are several, then evaluate from left-to-right

COMP104 Lecture 5 / Slide 26 Operator Precedence l Examples * 3 / = = -3 2 * 4 / * 5 % 4 =6/5 + 15%4 = 1+3 = * 3 / 4 =9.0 / 4 = 2.25 (1 + 3) * ((2 + 4 * 6) * 3) / =4 * (26*3)/2 + 2 = 158

COMP104 Lecture 5 / Slide 27 Standard Input/Output cin - the standard input stream Input operator “ >> ” n extracts data from input “stream” (the keyboard by default) n skips over white spaces n extracts only characters of the right form and performs automatic conversion to the type specified

COMP104 Lecture 5 / Slide 28 Standard Input/Output cout - the standard output stream Output operator “ << ” n inserts data into the output “stream” (the screen by default) n Example: int id; float score; cout << "Enter student ID and score: "; cin >> id >> score; cout << "Student ID: " << id << " score: " << score << endl;

// Convert inches to feet and inches // Input: inches // Output: feet and inches #include using namespace std; int main() { // inches to feet conversion factor const int in2feet = 12; int inches; // number of inches int feet;// number of feet cout<< "Enter number in inches: "; cin >> inches; // Convert inches to feet and inches feet = inches / in2feet; inches = inches % in2feet; cout << feet << " feet " << inches << " inches " << endl; return 0; }

COMP104 Lecture 5 / Slide 30 Assignment Conversions l A floating-point expression assigned to an integer object is always rounded down (truncated) l An integer expression assigned to a floating-point object is converted to a floating-point value l Example 1: float y = 2.7; int i = 15; int j = 10; i = y; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

COMP104 Lecture 5 / Slide 31 Assignment Conversions l Example 2: int m, n; double x, y; m = 3; n = 2.5; // 2.5 converted to 2 and assigned to n x = m/n; // 3/2=1 converted to 1.0 and assigned to x n = x+m/2; // m/2=1 : integer division // x+m/2 : double addition because x is double // convert result of m/2 to double (i.e. 1.0) // x+m/2=2.0 // convert result of x+m/2 to int (i.e. 2) //because n is int

COMP104 Lecture 5 / Slide 32 White Spaces Extra blanks or tabs (white spaces) are ignored x=3; x= 3 ; l Blank lines and comments are treated as white spaces l Code can be indented in any way l More than one statement can be on one line int x, y; x=3; y = 10;int z = x+y; l A single statement can be continued over several lines cout << feet << " feet and " << inches << " inches" << endl;

COMP104 Lecture 5 / Slide 33 Good Programming Style Place each statement on a line by itself (except long cout statements) x = m/n; l Use blank lines to separate sections of code Use the same indentation for all statements in the same block of code {…}. (“Format selection”, ALT-F8, will automatically fix indentation) int main(){ int x; x = 5; return 0; }

COMP104 Lecture 5 / Slide 34 Good Programming Style Use meaningful identifier names double area, sum, radius; l Document each variable when it is declared double area; // area of the circle double distance; // distance from top to bottom l Document each segment of code // Convert inches to feet and inches feet = inches / in2feet; inches = inches % in2feet; l Document the beginning of the program with a header that tells the purpose of the program // Convert inches to feet and inches