C++ Programming Basics

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Chapter 2: Basic Elements of C++
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.
CHAPTER 2 BASIC ELEMENTS OF C++. In this chapter, you will:  Become familiar with the basic components of a C++ program, including functions, special.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Input & Output: Console
Chapter 2: Using Data.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
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.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 2: Basic Elements of C++
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 A Simple “Hello World” Example #include // input-output library using namespace std; int main() // function main { cout
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Bill Tucker Austin Community College COSC 1315
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter 1.2 Introduction to C++ Programming
TK1913 C++ Programming Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++ Chapter 1.
Computer Programming BCT 1113
Chapter 1.2 Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Programming Fundamentals
Computing Fundamentals
Basic Elements of C++.
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Chapter 3: Input/Output
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Introduction to C++ Programming
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
C++ for Engineers and Scientists Second Edition
Presentation transcript:

C++ Programming Basics

Overview Basic Program Construction Output using cout Directives Comments Integer Variables Character Variables Input using cin Floating Point Types Type bool Endl and setw Manipulators Type Conversion Arithmatic Operators Libaray Functions

Basic Program Construction #include <iostream> int main() { cout<<” Every age has a language of its own\n ”; return 0; } Fucntion Name Braces and Function Body Program state ments White spaces: ignored by compiler such as spaces, carriage returns, linefeeds, tabs

Always start with main() Program entry point Control always goes to main() before excuting other functions main() function may also contain calls to other stand alone functions and member functions in various objects

Output using Cout<< Cout is an object predefined in C++ corresposnds to the standard output stream Standard output stream flows to the screen display Cout<<”Every age has a language of its own \n”; << Operator is called insertion or put to operator String Constant is array of characters and retains its value throughout the program existance

Directives Preprocessor Directives It is instruction to the compiler A part of compiler called preprocessor deals with directives before it begin real compilation process. E.g. #include <iostream.h> #include tells compiler to insert another file into source file Same as pasting a block of text into a document with your word processor

Directives Using Directive C++ program is divided into namespaces A namespace is part of the program in which certain names are recognized; outside namespace they are unknown E.g. Using namespace std; All program statements within std namespace Used when working in multifiles environment If not using directive then we need to add Std::cout<<”Hello World”;

Comments Helps in understanding code: Optional Syntax // single line comment /*this is multiline comment*/ Can be inserted anywhere in program

Variables Variable has symbolic name and can have varity of values Variables are located in particular location in computer memory Declaration introduce variable’s name and specify its type such as int Int a; Long num; Definition introduce intialization of variable Int a = 10; Identifer vs keyword Assignment statements var1 = 20; var2 = var1 + 10;

Integer Variables Integer variables represent integer numbers like 1,30,000 and - 26 The amount of memory occupied by integers is system dependent 32 bit system such as windows allows int to occupy 4 bytes Ranges occupied by various types are listed in header file LIMITS

Character Variables Type char stores integer having range from -128 to 127 Occupy only 1 byte of memory Commonly use to store ASCII characters such as ‘a’, ‘B’, ‘$’, ‘3’ etc Character Constants vs String Constant Character constant is translated into equivalent in ASCII code e.g. Constant ‘a’ will be translated into 97 Escape Squences \ causes an escape from normal way character are interpreted \ t, \ n, \ \

Input with cin>> Keyword cin in an object defined in C++ to correspond to standard input stream. >> is the extraction or get from operator #include<iostream> Using namespace std; Int main() { int ftemp; cout <<”Enter temperature in fahrenheit: ”; cin>>ftemp; int ctemp = (ftemp – 32) * 5/9; cout<<”Equivalent in Celsius is: ”;<<ctemp<<‘\n’; return 0; }

Floating Point Types Floating point variables represent numbers with decimal place e.g. 3.1415927, 0.0000635 and -10.2 Float Stores number in range 3.4 x 10 -38 to 3.4 x 10 38 Seven digits precision Double 8 bytes of storage and handles number in range from 1.7 x 10 -308 to 1.7 x 10 308 15 digits precision

Constants Const Qualifier const float PI = 3.14159F; // type const float Value of variable will not change throughout the program #define Directive Can also be specified using #define directive #define PI 3.14159 PI will be replaced by its value throughout the program

Variable Type Summary Name Description Size* Range* char Character or small integer. 1byte signed: -128 to 127 unsigned: 0 to 255 short int (short) Short Integer. 2bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 4bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int (long) Long integer. bool Boolean value. It can take one of two values: true or false. true or false float Floating point number. +/- 3.4e +/- 38 (~7 digits) double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits) long double Long double precision floating point number. wchar_t Wide character. 2 or 4 bytes 1 wide character

Setw and endl Manipulator Manipulators are used with insertion operator (<<) to modify or manipulate the way data is displayed Endl manipulator for new line setw() manipulator causes the number(string) that follows it in the stream to be printed within a field n characters wide n is argument to setw(n) Right justified IOMANIP header file used for these manipulators

Type Conversion Automatic Conversion Casts ? When two operands of different data types are encountered in the same expression ,the lower type variable is converted to the type of higher type variable. Casts ?

Arithmetic Operators + addition - subtraction * multiplication / division % Modulo/Remainder

Arithmetic Assignment Operator Combines an arithmetic operator and an assignment operator and eliminate repeated operand total = total + items can be writen as total+=items Increment Operator Add 1 to the value of an existing variable E.g. count = count +1 // adds 1 to count Count +=1; // adds 1 to count ++count; // adds 1 to count

Cont’d Increment operator can be used in two ways Prefix Postfix Operator preceeds the variable TotalWeight = avgWeight * ++count; Postfix Operator follows variable totalWeight = avgWeight * count++; Decrement Operator (--) Same as increment but subtracts 1 from its operand Can be used in both prefix and post fix forms

Library Functions Many activities in C++ are carried out by library functions Performs file access, mathematical computation and data conversions E.g. Sqrt(number) is libaray function in cmath header file Header File File that contain declaration of any library functions used E.g. #include <cmath> header file of SQRT() function

Library files vs Header files Library files contain actual machine-executable code for functions Have .lib file extension Sqrt() function is part of library files Automatically extracted from by the linker Header files contain information for particular group of functions Two Ways to use #include #include ”myheader.h” // search in current directory #include<cmath> Search for particular header file in INCLUDE directory