Unit 4 Oops 80 Slides
Unit 4 Oops 80 Slides
PROGRAMMING
(with C++)
Unit-4
Exception Handling
class Test {
public:
Test() { cout << "Constructor of Test \n"; }
~Test() { cout << "Destructor of Test \n"; }
}; Output:
Constructor of Test
int main() { Destructor of Test
try { Caught 10
Test t1;
throw 10;
} catch(int i) {
cout << "Caught " << i << endl;
}
}
Unit-4
Standard Library, Streams & File
endl
To insert new line and flush stream
cout<<endl;
cout<<setiosflags(ios::scientific)<<1.0000;
Output: 1.0000e+000
resetiosflags(long f)
Clears the flag specified by setiosflag e.g.
cout<<resetiosflags(ios::scientific)<<1.0000;
Output: 1.0000
e.g.
.width ( ) ;
Specify field width. (Can be used on input or output, but
only applies to next insertion or extraction).
e.g.
cout.width (4) ; // field is four positions wide
.fail ( ) ;
Tests if a stream operation has failed.
e.g.
cin.fail ( ) ; // true if a format error occurred
! cin; // same as above; true if format error
e.g.
cin.clear ( ) ; // allow I/O to resume on a "bad"
// stream, in this case "cin",
// on which error had previously
// occurred
Unit-4
Standard Template Library
Function Objects
Iterators
Containers Containers
#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
int main()
{
vector<int> v(10); /* now vector v is : 0,0,0,0,0,0,0,0,0,0 */
iota(v.begin(), v.end(), 10 ); /* now the vector v is
10,11,12,13,14,15,16,17,18,19 */
}