Introduction To C++
Introduction To C++
Loader
Running Program
.cpp / .h
compiler
linker
getch();
}
unsigned
2- short 2 Byte
1- char x ;
0 to 65,535 or -32,768 to 32,767
2- short m; // preferred
short int y; // valid
3- int 2 Byte
Void
3- int z; 4- long 4 byte
0 to 4,294,967,295 or
4- long avg; // preferred -2,147,483,648 to 2,147,483,647
long int sum; // valid 5- Long long 8 byte
5- long long KL; // preferred
long long int c;// valid
bool
0 to 18,446,744,073,709,551,615 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
C++ data type
Integer
Floating variable hold fractional numbers
(real number), and very large number
(floating variable has three types.
Floating
1- float x ; Range: -3.4E+38 to 3.4E+38 (four bytes)
2- double m; Range: -1.7E+308 to 1.7E+308 (eight bytes)
Basic data type
bool
C++ data type
Integer
bool
C++ data type
Integer
Void
bool
string Type
Sequence of zero or more characters
Enclosed in double quotation marks
Null: a string with no characters
Each character has relative position in string
Position of first character is 0
Length of a string is number of characters in it
Welcome GS200 " is a string ,and its length is14
15
Comments
Multiple line
/*
You can include comments that can
occupy several lines.
*/
16
Arithmetic Operators and Operator Precedence
C++ arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% modulus operator
All operations inside of () are evaluated first
*, /, and % are at the same level of precedence and are evaluated next
+ and have the same level of precedence and are evaluated last
When operators are on the same level
Performed from left to right (associativity)
3 * 7 - 6 + 2 * 5 / 4 + 6 means
(((3 * 7) 6) + ((2 * 5) / 4 )) + 6 17
Expressions
If all operands are integers , then its called an integral expression
Produces an integral result Ex: 2 + 3 * 5
18
IDENTIFIERS Variables
ARE THE NAMES GIVEN BY THE PROGRAMMER TO DIFFERENT variables
It can contain characters(A-Z, a-z),Digits(0-9), & only one special symbol called
underscore(_).
First letter must be a character (A-Z, a-z) or underscore(_).
No commas or blank spaces allowed.
Upper case and Lower case characters are different.
Variable decleration
Variables are used to store values that can be changed during the program
execution.
- int a ;
float b ,c ;
cin>>a >>b;
cin>>c ;
Example -6
// Addition program for two integers c=a+b
# include <IOstream.h>
# include <conio.h>
Void main()
{
int a,b,c; /* Declaration */
cout<<("Enter the value of A: ");/* Prompt */
cin>>a; /* Read a */
cout<<("Enter the value of B: ");/* Prompt */
cin>>b; /* Read b */
c=a+b; /* c=a+b */
cout<<("The value of C is",c);
//Print the result
getch();
}
Example -7 :: what is the output o the following program ?
# include < iostream.h >
Void main()
{
int p,q,r,x;
p=2;
q=6;
r=p + q / p;
x=( p + q ) / p;
cout<<"r = "<<r<<"\n x = <x<<"\n";// \n means move to new line
}
MANAGING CONSOLE I/O
OPERATIONS
Escape character
FORMATTED I/O OPERATIONS
C++ supports a number of features which can be used for formatting
the output. These features include :-
setprecision() precision()
setfill() fill()
setiosflags() setf()
resetiosflags() unset()
Namita Pandey 2011BTechece020 . Shiva Johari 2011BTechcse015 . Pritam Kalwaniya 2011BTechcse008
Setting Width : width()
Example-8
cout.width(5);
cout<<543<<12<<\n;
//cout<<setw(5)<<543<<12<<\n; 5 4 3 1 2
cout.width(5);`
cout<<543;
cout.width(5);
cout<<12<<\n; 5 4 3 1 2
Example-12:
cout.fill(*);
cout.width(10); * * * * * * 5 2 5 0
cout<<5250<<\n;
1.234672e-01
cout.setf(ios::scientific, ios::floatfield);
x=.123467 ;
cout<<x;
getch();
}
Namita Pandey 2011BTechece020 . Shiva Johari 2011BTechcse015 . Pritam Kalwaniya 2011BTechcse008
Example-15:
#include<iostream.h>
#include<iomaniop.h>
void main()
{
int num;
cout<<enter an integer value;
cin>>num;
cout.setf(ios::hex, ios::basefield)
cout<<num<<, ;
Enter an integer value : 92
cout.setf(ios::oct, ios::basefield)
The hexadecimal, octal and decimal representation of 92 is: 5c, 134 , and 92
cout<<num<<, ; respectively.
cout.setf(ios::dec, ios::basefield)
cout<< and <<num<< respectively.;
}
Namita Pandey 2011BTechece020 . Shiva Johari 2011BTechcse015 . Pritam Kalwaniya 2011BTechcse008
Intput character function cin.get(char)
An alternative to the >> operator is the get() function, a member of the istream class
The get() function reads and stores the next character in the input stream, skipping
nothing in other words, it reads white space
Example:
char c;
cout << Enter a character: ;
cin.get(c); // reads the character entered
cout << You entered: << c << endl;
Output character function cout.put(char)
Member function of the ostream class
Used to output single chars
Examples
cout.put( 'A' );
Char ch=z;
Cout.put(ch);
cout.put( 65 ); //Using the ASCII value
Character Strings
message2: H e l l o w o r l d \0
char message3[12];
cin >> message3; // type "Hello" as input
message3: H e l l o \0 ? ? ? ? ? ?
Strings
String can be input using the extraction operator >>, but one or more white spaces indicates
the end of an input string.
Example :
char A_string[80], E_string[80];
cout << "Enter some words in a string:\n";
cin >> A_string >> E_string;
cout << A_string << E_string<< \nEND OF OUTPUT\n";
Output:
Enter some words in a string:
This is a test.
Thisis
END OF OUTPUT
getline
The function getline can be used to read an entire line of input into a string variable.
The getline function has three parameters:
The first specifies the area into which the string is to be read.
The second specifies the maximum number of characters, including the string delimiter.
The third specifies an optional terminating character. If not included, getline stops at \n.
Example :
char A_string[80]; // array of characters
cout << "Enter some words in a string:\n";//80 is the size of A_string
cin.getline(A_string, 80);
cout << A_string << \nEND OF OUTPUT\n";
Output:
Enter some words in a string:
This is a test.
This is a test.
END OF OUTPUT
getline
Example :
char A_string[5], E_string[80];
cout << "Enter some words in a string:\n";
cin >> A_string;
cin.getline (E_string, 9) ;
cout << A_string << "#" << E_string<< \nEND OF OUTPUT\n";
Output:
Enter some words in a string:
This is a test.
This# is a te
END OF OUTPUT
Example: Output example:
#include <string>
#include <iostream> Enter a line of text: This is a test line of text
int main(void) You entered: This is a test line of text
{ Enter a line of text, <space> as the delimiter:
This is another line of text
string str; //must include string
You entered: This
string str1;
string str2;
cout<<"Enter a line of text: ";
getline(cin, str); // cin.getline(str ,xx) -> if str defined as an array of size xx char
str[xx];
cout<<"You entered: "<<str<<endl;
cout<<"Enter a line of text, <space> as the delimiter: "<<endl;
getline(cin, str1, ' ');
cout<<"You entered: "<<str1<<endl;
return 0;
ignore()Member Function\
There is also a way to remove and discard characters from an input stream:
cin.ignore(N, ch);
means to skip (read and discard) up to N characters in the input stream, or until the character ch
has been read and discarded, whichever comes first. So:
cin.ignore(80, '\n');
says to skip the next 80 input characters or to skip characters until a newline character is read, whichever comes first.
The ignore function can be used to skip a specific number of characters or halt whenever a given character occurs:
cin.ignore(100, '\t');
means to skip the next 100 input characters, or until a tab character is read, or whichever comes first.
Example:
Example:
#include<iostream.h>
void main ()
{
char first ;
char last;
cout<<"Enter your first and last :";
firsth = cin.get();
cin.ignore(30,'\n');
last = cin.get();
cout<<"The initials letters are:
"<<first<<endl<<last<<endl;
cin.ignore(30,'\n');
cin.get();
}
Output example: