12.1 What is generic Programming? How is it implemented in C++?
Answer:
Generic programming is an approach where generic types are used as parameters in algorithms so that they work for variety of suitable data types and data structures.
It is implemented in C++ using class templates.
12.2 A template can be considered as a kind of macro. Then what is the difference between them?
Answer:
Templates are a feature of the C++ programming language at allows a functions or class to work on many different data types without being written for each one. Templates are of great utility programmers in C++, especially when combined with multiple inheritance and operator overloading. The C++ STL provides many usefu functions within a framework of connected templates.
On the other hand the # define directive is typically used to associate meaningful identifiers with constants, keywords and commonly used statements or expressions. Identifiers that represent constants are sometimes called “symbolic constant” or “manifest constant”.
Identifiers that represents statements or expressions are called “macro”. In this preprocessor documentation only the term “macro” is used.
12.3 Distinguish between overloaded functions and junction templates.
Answer:
Basic difference is function body will not differ in function overloading.
12.4 Distinguish between the terms class template and template class.
Answer:
Class template is a generic class. The class template definition is very similar to an ordinary class definition except the prefix template and the use of type T.
On the other hand a class created from a class template is called a template class. The process of creating a specific class from a class template is called instantiation.
12.5 A class (or function) template is known as a parameterized class (or function). Comment.
Answer:
When an object of specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specified data type at the time of actual use of the class or function, the templates are called parameterized classes or functions.
12.6 What is an exception?
Answer:
Exceptions are run-time anomalies or unusual conditions that a program may encounter while executing.
12.7 How is an exception handled in C++?
Answer:
C++ exception handling mechanism is basically built upon the three keywords, namely, try, throw and catch. The keyword try is used to preface a block of statements which may generate exceptions. This block is known as try block. When an exception is detected, it is thrown using throw statement in the try block. A catch block defined by the keyword “catch” catches the exception thrown statement in the try block and handles it appropriately. This relationship is shown below:
12.8 What are the advantages of using exception handling mechanism in a program?
Answer:
We often come across some peculiar problems other than logic or syntax errors. These error occurs at run time and the whole program may be crashed. To avoid these types of errors we use exception handling mechanism in a program.
12.9 When should a program thrown an exception?
Answer:
When an exception is detected in try block then a program should throw an exception to catch block.
12.10 When is a catch(…) handler is used?
Amswer:
To catch all exceptions catch(…) handler is used.
12.11 What is an exception specification?When is it used?
Answer:
Exception specification is a way to restrict a function to throw only certain specified exceptions. The general form of using an exception specification is:
type function(argument list)throw(type-list){
function body
}
12.12 What should be placed inside a “try” block?
Answer:
A throw statement should be placed inside a try block.
12.13 What should be placed inside a “catch” block?
Answer:
A appropriate message for which an exception is caught shoud be placed in catch block.
12.14 When do we use multiple “catch” handlers?
Answer:
When there are more than one exception in a program, then we use multiple catch statements.
Programming Exercises
12.1 Write a function template for finding the minimum value contained in an array.
#include<iostream>usingnamespace std;constint size =5;template<classT>classvector{
T v[size];public:vector(){}vector(T *b);voidshow();
T minimum(vector<T>&m);};template<classT>vector<T>::vector(T *b){for(int i=0;i<size;i++)
v[i]= b[i];}template<classT>
T vector<T>::minimum(vector<T>&m){int j =0;for(int k=1;k<size;k++){if(m.v[j]>m.v[k])
j = k;}return m.v[j];}template<classT>voidvector<T>::show(){
cout <<"(";for(int t=1;t<size;t++)
cout <<", "<< v[t];
cout <<" )"<< endl;}intmain(){int x[size]={5,7,3,1,8};float y[size]={1.2,1.5,3.1,1.1,0.501};
vector<int> v1;
vector<float> v2;
v1 = x;
v2 = y;
cout <<"Minimum value = "<< v1.minimum(v1)<<" of array";
v1.show();
cout <<"Minimum value = "<< v2.minimum(v2)<<" of array";
v2.show();return0;}
12.2 Write a program containing a possible exception. Use a try block to throw it and a catch block to handle it promptly.
#include<iostream>#include<cmath>#definePI3.1416usingnamespace std;voidpower_factor(float a){if(a>1|| a<-1)throw(a);else
cout <<"Voltage(V) is lagging from current(I) by : "<<acos(a)*180/PI <<"degree\n";}intmain(){float a;try{
cout <<"Enter power factor ";
cin >> a;power_factor(a);}catch(float b){
cout <<"Caught an exception \n";}return0;}
12.3 Wrtie a program that demonstrates how certain exception types are not allowed to be thrown.
#include<iostream>usingnamespace std;voidempty()throw(){
cout <<"In empty()\n";}voidwith_type(float x)throw(int){if(x==1)throw(1);elseif(x==1.1)throw(2.1);}intmain(){try{empty();with_type(1);}catch(int n){
cout<<"Caught an int = "<< n;}catch(float){
cout<<"Caught a float ";}return0;}
12.4 Write a program to demonstrate the concept of re-throwing an exception.
#include<iostream>usingnamespace std;voiddivision(int a,int b){try{if(b==0)throw b;else
cout <<" a/b = "<<(float)a/b << endl;}catch(int){
cout <<"Caught an exception as first throwing \n";throw;}}intmain(){int a, b;
cout <<"Enter the value of a & b : ";
cin >> a >> b;try{division(a, b);}catch(int){
cout <<"Caught an exception as re-throwing \n";}return0;
12.5 Write a main program that calls a deeply nested function containing an exception. Incorporate necessary exception handling mechanism.
#include<iostream>usingnamespace std;longintsquare(int i){return i*i;}longintsum(int n){longint s;
s =0;for(int i=1;i<=n;i++){
s+=square(i);}return s;}voiddisplay(int m){try{if(m<0)throw m;else
cout <<sum(m)<<endl;}catch(int n){
cout<<"Caught an exception \n";}}intmain(){int n;
cout <<"Enter a positive number ";
cin>>n;display(n);return0;}
终于来到了网络的最后一篇,继续加油!
IP 知识全家桶
IP 基本认识
IP 在 TCP/IP 参考模型中处于第三层,也就是网络层。
网络层的主要作用是:实现主机与主机之间的通信,也叫点对点(end to end)…