Download presentation
Presentation is loading. Please wait.
1
C++ Programming Basics
2
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
3
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
4
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
5
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
6
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
7
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”;
8
Comments Helps in understanding code: Optional Syntax
// single line comment /*this is multiline comment*/ Can be inserted anywhere in program
9
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;
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
11
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, \ \
12
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; }
13
Floating Point Types Floating point variables represent numbers with decimal place e.g , and -10.2 Float Stores number in range 3.4 x to 3.4 x 10 38 Seven digits precision Double 8 bytes of storage and handles number in range from 1.7 x to 1.7 x 15 digits precision
14
Constants Const Qualifier
const float PI = F; // type const float Value of variable will not change throughout the program #define Directive Can also be specified using #define directive #define PI PI will be replaced by its value throughout the program
15
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: to unsigned: 0 to 65535 int Integer. 4bytes signed: to unsigned: 0 to 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
16
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
17
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 ?
18
Arithmetic Operators + addition - subtraction * multiplication /
division % Modulo/Remainder
19
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
20
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
21
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
22
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.