C - Notes (Data Planet)
C - Notes (Data Planet)
Write program on your own after watching lecture. If you are stuck then check
video and start writing again but do not copy paste.
Home Work
Write program on your own after watching lecture. If you are stuck then check
video and start writing again but do not copy paste.
Example-2
Answer for
what is friend
function?
Explain it’s
properties
This is for
additional
knowledge,
do not
include in
answer.
Write C++ program for addition of complex number
Method-1: Through member without returning object
Method-2: Through member by returning object
Method-3: Through friend function
Write C++ program for addition of time objects
Method-1: Through member without returning object
Member of Member
Member of Member
7.
Write output on your
own
For ex: increment of integer pointer
setfill()
It is used to set fill character for unused columns while
printing a value. Default is space (Blank)
Syntax -
cout<<setfill('*')<<x;
cout<<setprecision(3)<<x;
if x is a float value, it will be truncated at 3rd digit.
setiosflags()
It is used to set various flags of ios class which formats
output in specific manner.
Syntax -
cout<<setiosflags(flagbit)
Some of the flag bit and their actions are listed below
flag bit actions
ios::left Value is printed left justified
ios::right Value is printed right justified(default)
ios::dec Integer is printed in decimal format(default)
ios::oct Integer is printed in octal format
ios::hex Integer is printed in hexa-decimal format
ios::uppercase Upper case in Hexa-decimal number
ios::showbase Show base in output (0 for octal and 0x for hex)
ios::showpos show positive (it shows sign + while printing +ve number)
ios::scientific Print float value in exponential notation
6. resetiosflags()
It is used to reset flags to default
Syntax -
cout<<resetiosflags(flagbit)
Member functions
C++ also provides some standard functions in stream classes
which can format output.
Such functions are not directly included in output statement,
instead they are called through ‘cout' object.
manipulator function
setw() width()
setfill() fill()
setprecision() precision()
setiosflags() setf()
resetiosflags() resetf()
for ex -
cout<<setw(5)<<x;
cout.width(5);
cout<<x;
User-defined manipulator
}
Keyword try is used to preface or enclose the block of code
where exception may occur.
When exception occurs, it is thrown by using keyword throw
in try block.
Keyword catch, catches the exception and handles it
appropriately.
The try and catch keywords come in pairs
Multiple catch blocks are allowed.
Program
#include<iostream>
using namespace std;
int main()
{
int x,y;
float d;
try
{
if(y==0)
throw(“\nDenominator can not be zero”);
d = float(x)/y;
cout<<”\nDivision =”<<d;
}
catch(char const *msg)
{
cout<<msg;
}
return 0;
}
Advantages of templates.
1) Use of template eliminates unnecessary duplication of code.
2) Size of the program is reduced.
3) Errors are minimized.
4) We can create generic storage classes.
Types of templates
There are two types of templates
1) Function template –
If template is used as a data type of parameters of function then
it is referred as function template.
2) class template –
If template is used as data type of data member of class then it
is referred as class template.
return 0;
}
#include<iostream>
using namespace std;
template<class T>
class Array
{
T a[10];
public:
void read()
{
int i;
cout<<”\nEnter elements”;
for(i=0;i<10;i++)
cin>>a[i];
}
void print()
{
a1.read();
a2.read();
cout<<”\nArray-1”;
a1.print();
cout<<”\nArray-2”;
a2.print();
return 0;
}