C++ 5
C++ 5
NO:5
Ans 1
#include<iostream.h>
#include<conio.h>
Int m[2],a;
Void main()
Cin>>m[0]>>m[1]>>m[2];
A=m[0]+m[1]+m[2];
Cout<<a;
Try
If(a=0);
Cout<<”error in file”;
Else
Cout<<a;
Getch();
Ans 2
Ans 3
It is only in the most exceptional cases that I want my application to
finish without tidying up after itself. Yes I want this facility but
this is the exception (no pun intended) not the rule.
But notice the key word here is (detected). We have detected the
breach in contract so there is a possibility that we could do
something about it, probably not a lot we can do but we don't need to
penalize other applications (ie your DB because the connection is
unexpectedly been severed) because our client has misbehaved.
But we are talking very much in generalities here and each situation
is unique and will require its own ****ysis.
Yes we both agree that the application should terminate (probably as
quickly as possible). I just don't think terminate() is a good idea in
most situations and thus my tendency not to use exception
specifications unless I have a high level of control.
=========================================
#include <iostream.h>
#include <exception.h>
class sys_exception { };
int main ( )
{
try
{
f( );
}
catch (sys_exception& se)
{
std::cerr << "System Exception thrown" << std::endl;
}
catch (std::exception& e)
{
std::cerr << "Exception thrown" << std::endl;
}
catch (...)
{
std::cerr << "Unknown Exception thrown" << std::endl;
}
return 0;
}
===================================
Ans 4
So compared to error reporting via return-codes and if, using try / catch /
throw is likely to result in code that has fewer bugs, is less expensive to
develop, and has faster time-to-market. Of course if your organization
doesn't have any experiential knowledge of try / catch / throw, you might
want to use it on a toy project first just to make sure you know what
you're doing — you should always get used to a weapon on the firing
range before you bring it to the front lines of a shooting war.
Ans 5
int main()
double dX;
try // Look for exceptions that occur within try block and route to atta
ched catch blck(s)
cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl;
{
cerr << "Error: " << strException << endl;
In this code, the user is asked to enter a number. If they enter a positive
number, the if statement does not execute, no exception is thrown, and
the square root of the number is printed. Because no exception is thrown
in this case, the code inside the catch block never executes. The result is
something like this:
Enter a number: 9
The sqrt of 9 is 3
Enter a number: -4
Error: Can not take sqrt of negative number
By now, you should be getting the basic idea behind exceptions. In the
next lesson, we’ll do quite a few more examples to show how flexible
exceptions are.
Ans 6
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Number1[40], Number2[40];
double Operand1, Operand2, Result;
char Operator;
try {
cout << "First Number: ";
cin >> Number1;
cout << "Operator: ";
cin >> Operator;
cout << "Second Number: ";
cin >> Number2;
Operand1 = atof(Number1);
Operand2 = atof(Number2);
case '-':
Result = Operand1 - Operand2;
break;
case '*':
Result = Operand1 * Operand2;
break;
case '/':
Result = Operand1 / Operand2;
break;
}
return 0;
}
Ans 7
a.) A local class is declared within a function definition. Declarations in a local class can
only use type names, enumerations, static variables from the enclosing scope, as well as
external variables and functions.
For example:
int main()
{
local* z; // error: the class local is not visible
// ...}
B.) C++ strings allow you to directly initialize, assign, compare, and reassign with the
intuitive operators, as well as printing and reading (e.g., from the user), as shown in
the example below:
string name;
cin >> name;
// read string until the next separator
// (space, tab, newline)
if (name == "")
{
cout << "You entered an empty string, "
<< "assigning default\n";
name = "John";
}
C++ strings also provide many string manipulation facilities. The simplest string
manipulation that we commonly use is concatenation, or addition of strings. In C++,
we can use the + operator to concatenate (or "add") two strings, as shown in the
example below:
string result;
string s1 = "hello ";
string s2 = "world";
result = s1 + s2;
// result now contains "hello world"
Notice that neither s1 nor s2 are modified! The operation reads the values and
produces a result corresponding to the concatenated strings, but doesn't modify the
two original strings.
The += operator can also be used. In that case, one string is appended to another
one, as shown in the following example:
string result;
string s1 = "hello";
// without the extra space at the end
string s2 = "world";
result = s1;
result += ' ';
// append a space at the end
result += s2;
Now, result contains the string "hello world".
You can also use two or more + operators to concatenate several (more than 2)
strings. The following example shows how to create a string that contains the full
name from first name and last name (e.g., firsname = "John", lastname = "Smith",
fullname = "Smith, John"):
#include <iostream>
#include <string>
using namespace std;
int main()
{
string firstname, lastname, fullname;
c.)
#include <iostream>
#include <ostream>
#include <string>
private:
T val_;
basic_ostream<C> &(*manipFun_)(basic_ostream<C> &, T);
};
template<typename T, typename C>
basic_ostream<C> &operator<<(
basic_ostream<C> &os,
const ManipInfra<T, C> &manip
)
{
manip(os);
return os;
}
return os;
}
int main()
{
cout << setWidth(10) << right << "foo" << endl;
}
Ans 8
for eg.
char *charptr;
int *intptr;
Ans 10
First, unless you are using a really old version of the C++ library - by which I
mean pre- ANSI / ISO standard C++ - the names of the header file is just fstream
- the standard C++ library headers now have no .h suffix. Note that the C++
standard was published in 1998 so most compilers have had time to catch up to
the standard.
fstream is part of the C++ IOStream library so you may also require certain
other headers as well such as iostream and/or iomanip.
Now I cannot provide you with "the codes" because "the codes" do not exist.
How you wish to use the IOStream library features is up to you and presumably
depends on your application's requirements.
Sorry but I am not going to be able give you a complete reference - that would
take too long - I suggest you obtain a good standard C++ library reference -
such as "The Standard C++ Library a Tutorial and Reference" by Nicolai M.
Josuttis - which I use all the time. The chapter on IOStreams is 100 pages long -
although this covers more than just the file access aspects of IOStreams.
Now to operate on a file you use std::ifstream objects for reading (input) and an
std::ofstream objects for writing (output). These are the types for char sized
characters. They have equivalents for wide character type (wchar_t) characters -
std::wifstream and std::wofstream. Note that the std:: prefix is used because,
like practically all C++ standard library entities, these types are defined in the
std namespace. I am not going to go into namespaces here, if you do not
understand them then refer to a good, modern, C++ language reference.
Before using a file you must open it - for reading, writing or both. You can create
a new file (useful in this case), or open an existing file (useful for reading and
writing an existing file). A file can be opened either when a file stream object is
created - using one of their constructors - or later by calling an open member
function.
One of the things you wish to do is create a file - so presumably you wish to write
to it in this case. You can do this like so:
#include <fstream>
//...
// In some function:
std::ofstream outFile("myfile.txt" );
if ( ! outFile )
{
// Open failed - take error handling action.
}
This opens the file myfile.txt for writing, creating the file if necessary and
emptying any previous contents. This is the default open mode for std::ofstream.
The default open mode for std::ifstream is open an existing file for reading. This
is the only way std::ostream and std::ifstream differ. Both allow these default to
be overridden by the use of a second parameter to their constructors (or open
member functions):