Tutorial
using C++ for 2 nd
year computer
science students
This tutorial mainly focus
on Laboratory assignments
Pointer
Pointer and functions( call by value &
reference ,pointer to function & passing a
function to another)
Pointer and arrays
Pointer and strings
Array of pointer
Pointer to pointer
Class and Template class
Accessing a member of a class
Class templates
Exception Handling
Try
Catch
throw
File Operations
Opening and closing of files
Stream state member functions
Reading & writing a character from a
file
Structure, class and file operations
Array of class object and file
operations
Random access file processing
Turbo C++ Graphics
Libraries
Introduction to graphics using C++
Chapter one
Conditional statements
1. if statements
2. if-else statements
3. switch-case statements
Loop statements
1. for loop
2. while loop
3. do-while loop
Breaking control statements
1. Break statement
2. Continue statement
3. goto statements
Revision about Variables,
statement
Register Variables: used when the variables is
reading or accessing repeatedly.
Syntax: register char temp;
Static Variables : The content of the variables
retained through out the program.
Syntax: static int x, y; it is the opposite of
automatic variable
External Variables : variables declared out side
the main.
Syntax : extern int x, y;
Automatic Variable : a variable declared inside
a function
If statement
Syntax:
If (expression)
For example:
A=20;
B=40;
{ If ( A>B)
statement
{ 1;
statement cout<<“Largest
2; Value is”<< A<<e
}
}
if-else statement
Syntax: For example // Wrong Syntax
if ( i>j)
If (expression) if ( a>b)
Temp=a;
else
statement 1 Temp=b;
For example // Right syntax
else if ( a>b)
statement 2 Temp=a;
else
Temp=b;
switch statement
Syntax: For example:
Switch( exp){
switch( input)
{
case 1: case Y:
statement
case y:
1; cout<<“ the statement is correct\n
break;
break; case n:
case 2: case N:
cout<<“ the statement is incorrect
statement break;
2; default:
break; cout<<“ the statement is false”<<
}
default:
for loop
Syntax:
for( initial condition; test condition; increment or
decrement)
{
statement_1;
statement_2;
}
For example:
for( int i=0;i<5;i--) {
i=i-1;
cout<<i<<‘\t’;
}
for loop con’t….
Initial condition may not be
required to be declared for some
cases.
For example :
reading a string from a key board
or a file. For this case use the
following syntax:
cout<<“a=“<<a<<“b=“<<b<<“c=“<<
c<<endl;
float average (float x, float y, float z)
{
float temp;
temp=(x+y+z)/3.0;
return(temp);
}
Passing a function to another
Syntax:
function
return_type function_name (pointer_to_function
(other argument));
For example:
float sum (float (*) (float, float), float, float);
Pointer and Multi
dimensional array
Pointer Initialization example
ptr=&value[ 0][ 0][ 1];
Pointer Variable
class
{
int a, b;
}
File Handling System
Input/Output with files
ofstream: Stream class to write on files
ifstream: Stream class to read from
files
fstream: Stream class to both read and
write from/to files.
Basic file operation
// basic file operations
#include <iostream.h>
#include <fstream.h>
int main ()
{
ofstream myfile;// File declaration
myfile.open ("example.txt“); // opening
file
myfile << "Writing this to a file.\n";
myfile.close(); // closing file
return 0;
}
Opening file
In order to open a file with a stream
object we use its member function
open():
Syntax : open (filename, mode);
Filename: is a null-terminated character
sequence of type const char * (the same
type that string literals have)
representing the name of the file to be
opened.
Mode : is an optional parameter with a
combination of the following flags.
ios :: in Open for input operations
ios ::out Open for output operations
ios :: Open in binary mode : Default is
binary
ios :: app text mode
Set the initial position at the end
of the file.
If this flag is not set to any value,
ios :: the initial position
All output operations areis the
trunc beginning
performed of at the
the file.
end of the file,
appending the content to the
current content of the file. This
flag can only be used in streams
ios :: ate open
If the for
fileoutput-only operations
opened for output
operations already existed
before, its previous content is
File stream flag con’t...
Example:
#include<iostream.h>
#include<conio.h>
void main() {
ifstream infile;
ifstream.open( “text”);
if ( infile.bad() ) {
cerr<<“ Open failure “<<endl;
exit(1);
}
}
fail ( )
Returns true in the same cases as bad(),
but also in the case that a format error
happens, like when an alphabetical
character is extracted when we are
trying to read an integer number.
To check whether a file has been opened
for input or output successfully, or any
invalid operations are attempted or
there is unrecoverable error.
For example
#include <iostream.h>
#include<conio.h>
void main()
{
ifstream infile;
infile.open (“text”);
while( !infile.fail() )
{
cout<<“couldn’t open file”<<endl;
continue;
}
}
eof()
Returns true if a file open for reading has
reached the end.
Used to check whether the file reach the
end of file or not
For example:
main( )
{
ifstream infile;
infile.open (“text”);
while ( !infile.eof () )
{
}
}
good()
It is the most generic state flag: it
returns false in the same cases in which
calling any of the previous functions
would return true.
In order to reset the state flags
checked by any of these member
functions we have just seen we can
use the member function clear(),
which takes no parameters.
Checking state flags
In addition to eof(), which checks if the end of
file has been reached, other member functions
exist to check the state of a stream (all of them
return a bool value):
bad()
Returns true if a reading or writing operation fails. For
example in the case that we try to write to a file that is
not open for writing or if the device where we try to
write has no space left.
fail()
Returns true in the same cases as bad(), but also in the
case that a format error happens, like when an
alphabetical character is extracted when we are trying
to read an integer number.
eof()
Returns true if a file open for reading has reached the
end.
good()
It is the most generic state flag: it returns false in the
same cases in which calling any of the previous
functions would return true.