DYNAMIC BINDING

Dynamic binding is less powerful than pointers-to-functions, but more comprehensible & less error-prone
i.e., since the compiler performs type checking at compile-time

Dynamic binding allows applications to be written by invoking general methods via a base class pointer, e.g.,

class Base { public: virtual int vf (void); };
Base *bp = /* pointer to a subclass */;
bp->vf ();
However, at run-time this invocation actually invokes more specialized methods implemented in a derived class, e.g.,
class Derived : public Base
{ public: virtual int vf (void); };
Derived d;
bp = &d;
bp->vf (); // invokes Derived::vf()
The answer depends on the type of binding used…
  • 1. Static Binding
: the compiler uses the type of the pointer to
perform the binding at compile time. Therefore,
Vector::operator[](vp, 0)
will be called
  • 2. Dynamic Binding

: the decision is made at run-time based upon
the type of the actual object.
Checked
Vector::operator[]
will be called in this case as
(*vp->vptr[1])(vp, 0)

Efficiency vs. flexibility are the primary tradeoffs between static & dynamic binding Static binding is generally more efficient since
1. It has less time & space overhead
2. It also enables method inlining
Dynamic binding is more flexible since it enables developers to
extend the behavior of a system transparently
However, dynamically bound objects are difficult to store in shared memory

EXAMPLE –

class Base
{
    public:
        virtual void Op();
}
class Derived: public Base
{
   public:
     void Op()
     { 
         cout << "Output through Derived!" << endl; 
     }
}
main
{

   Base * pB = new Derived();
   pB->Op(); //dynamic binding, uses virtual table lookup at run time..
}

File Handling in CPP;

—Files
  • —data structures that are stored separately from the program (using auxiliary memory)
  • —streams must be connected to these files
—Input File Stream
  • —extracts, receives, or gets data from the file
—Output File Stream
  • —inserts, sends, or puts data to the file

—C++ Files and Streams

  • C++ views each files as a sequence of bytes.
  • Each file ends with an end-of-file marker.
  • —When a file is opened, an object is created and a stream is associated with the object.
  • —To perform file processing in C++, the header files <iostream.h> and <fstream.h> must be included.
             —<fstream.> includes <ifstream> and <ofstream>

HERE IS THE PPT FOR FILE HANDLING REFER TO IT FOR MORE-

Templates

A template is a  formula for creating a generic class or a function. C++ templates enable you to define a family of functions or classes that can operate on different types of information.
A function template by itself is not a type, or a function, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template).

Que- Find maximum of different data types using templates in a generic function;

#include "iostream"
#include "string"
#include"conio.h"

using namespace std;

template <typename T>
T maximum (T a, T b) 
{ 
    return a < b ? b:a; 
} 
int main ()
{
    int i = 50;
    int j = 40;
    cout << "Maximum in (i, j): " << maximum(i, j) << endl; 
    double f = 16.9; 
    double g = 76.1; 
    cout << "Maximum in(f, g): " << maximum(f, g) << endl; 
    string h = "Tanu"; 
    string k = "Welcome"; 
    cout << "Maximum in(h, k): " << maximum(h, k) << endl; 
    cout<<"\nPROGRAMMING @ C#ODE STUDIO";
    getch();
    return 0;
}

 

template_ezy

EXCEPTION HANDELING 1;

A throw expression signals the error or exceptional condition.
You can use an object of any type as the operand of a throw expression.
This object is typically used to convey information about the error.

this is made up of these blocks :
1:try block
2:catch block
3:throw block

TRY- You use a try block to indicate which areas in your program that might throw exceptions you want to handle immediately. You use function try block to indicate that you want to detect exceptions in the entire body of a function.

 

try
{
    divide(10, 0);
}
catch(int i)
{
    if(i==DivideByZero)
    {
        cerr<<"Divide by zero error";
    }
}

CATCH – You can declare a handler to catch many types of exceptions. The allowable objects that a function can catch are declared in the parentheses following the catch keyword
(the exception_declaration). you can catch objects of the fundamental types, base and derived class objects,
references, and pointers to all of these types.

catch (networkIOException& e)
 {

   Log error message in the exception object.
   OBJ << e.what();
}

THROW – You use a throw expression to indicate that your program has encountered an exception.

    try
    {
    if(b==0)
    {
        throw b;
    }
    else
    {
        cout<<"\nANSWER IS : "<<a/b;
    }
    }

Pointers:

What is a variable?

—”the type specifies how to interpret the data stored in main memory and how long the variable is
—”the value is the actual data stored in the variable after if has been interpreted according to a given type
Pointer variable
—A pointer is a variable that contains the memory location of another variable.
—Syntax:-

type * variable name

—You start by specifying the type of data stored in the location identified by the pointer.
—The asterisk tells the compiler that you are creating a pointer variable.
—Finally you give the name of the variable.
Declaring a Pointer Variable
žTo declare ptr as an integer pointer:

  int *ptr;

žTo declare ptr as a character pointer:

  char *ptr;

Address operator:

—Once we declare a pointer variable we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following example:

  ptr=&num;

—This places the address where num is stores into the variable ptr. If num is stored in memory 21260 address then the variable ptr has the value 21260.
Pointer Variable

Assume ptr is a pointer variable and x is an integer variable

Now ptr can access the value of x.

HOW!!!!

Write:  *variable .    For example:

 Cout<<  *ptr;

x=10;

ptr=&x;

 

—

Friend function (theory):

Basically, when you declare something as a friend, you give it access to your private data members.
Friendship is not inherited, transitive, or reciprocal. 
Derived classes don’t receive the privileges of friendship (more on this when we get to inheritance in a few classes)
The privileges of friendship aren’t transitive.  If class A declares class B as a friend, and class B declares class C as a friend, class C doesn’t necessarily have any special access rights to class A.
If class A declares class B as a friend (so class B can see class A’s private members), class A is not automatically a friend of class B (so class A cannot necessarily see the private data members of class B).
Characteristics of Friend Function
•It is not in the scope of the classto which it has been declared as friend.
•Since it is not in the scope of the class, it cannot be called using the object of that class.
•It can be invoked like a normal function without the help of any object.
•Unlike member functions , it cannot be access the member names directly and has to use an object name and dot membership operator with each member name(eg. B.x)
•It can be declared either in the public or the private part of a class without affecting its meaning.
•Usually it has the objects as arguments.
Friend Class

Member functions of one class can be friend functions of another class. In such cases they are defined using the scope resolution operator as shown below:

eg-

class student
{
int roll;
char name[30];
int marks;
int input()
{
cout<<“\nINPUT ROLL NO.:”;
cin>>roll;
cout<<“\nINPUT NAME:”;
cin>>name;
cout<<“\nINPUT MARKS /100:”;
cin>>marks;
}
friend class result; //this statement allows user to access data to other class,
};
class result
{
public:
int disp()
{
student s;
s.input();
int t=45;
if(s.marks<45)
cout<<“\nSTUDENT FAIL”;
else
cout<<“\nSTUDENT PASSED”;
}
};

refer – FRIEND FUNCTION

here class result can access private data of student..

Predefined Mnipulators:

Following are the standard manipulators normally used in the stream classes:

•endl
•hex, dec, oct
•setbase
•setw
•setfill
•setprecision
•ends
•ws
•flush

(a) Endl-the endl is an output manipulator to generate a carriage return or line feed character. Theendl may be used several times in a C++ statement.

For example,

(1)

cout << “ a “ << endl << “b” << endl;

(b) Setbase()  The setbase() manipulator is used to convert the base of one numeric value into another base. Following are the common base converters in C++.

dec   – decimal base (base = 10)

hex   – hexadecimal base (base = 16)

(c)   Setw ()  

The setw ( ) stands for the set width. The setw () manipulator is used to specify the minimum number of character positions on the output field a variable will consume.

The general format of the setw manipulator function is

setw( int w )

Which changes the field width to w, but only for the next insertion. The default field width is 0.

For example,

cout << setw (1) << a << endl;

cout << setw (10) << a << endl;

(d)  Setfill()

The setfill ( ) manipulator function is used to specify a different character to fill the unused field width of the value.

The general syntax of the setfill ( ) manipulator is

setfill( char f)

which changes the fill character to f. The default fill character is a space.

For example,

setfill ( ‘ . ’ ) ; / / fill a dot ( . ) character

setfill ( ‘ * ’ ) / / fill a asterisk (*) character

(e) Setprecision()

  The setprecision ( ) is used to control the number of digits of an output stream display of a floating point value. The setprecision ( ) manipulator prototype is defined in the header file <iomanip.h>.

The general syntax of the setprecision manipulator is

Setprecision (int p)

Which sets the precision for floating point insertions to p. The default precision is 6  eg:- 0.0000001

(f)  Ends

  The ends is a manipulator used to attach a null terminating character (‘’) at the end of a string. The ends manipulator takes no argument whenever it is invoked. This causes a null character to the output.

(g) Ws 

 The manipulator function ws stands for white space. It is used to ignore the leading white space that precedes the first field.

1.Noskipws

Skipws

(h)  Flush   The flush member function is used to cause the stream associated with the output to be completed emptied.

For input on the screen, this is not necessary as all output is flushed automatically. However, in the case of a disk file begin copied to another, it has to flush the output buffer prior to rewinding the output file for continued use. The function flush ( ) does not have anything to do with flushing the input buffer.