Pages

Sunday, 24 August 2014

pointers and Polymorphism

Polymorphism
Compile time Polymorphism: early binding, static binding or static linking here objects are bound to its functions at compile time.
Run time polymorphism:  late binding selection of the appropriate function is done at rum time.

Callback functions: pointer to function (cannot be dereferenced)
return_type (*func_pointr_name) (arg_list) ;

Creating array of object using pointers
classname *ptr = new classname [10] ; [default constructor should be present in the class]
we can also achieve the same in the following way
classname *ptr[10];
ptr[0]= new classname ; // each member of the array is individually initialized
ptr->show () ; ptr ++; //will point to the next object in the array

this pointer: used to represent the object using which the member function is invoked.

If a base class pointer is used to point to a derived class object then only those members which are inherited from the base class can be accessed. If a member has the same name in the derived class then the pointer will always access the base class member.
But if casting is used then the derived member can be accessed in the case of overidding
((DC*)bptr)->show();

Virtual Functions: If the function declaration in the base class is preceded with keyword virtual  then the C++ determines which function to use in run time  based on which object the base pointer points to. if it isn’t a virtual function then the base pointer will always call the function in the base, even it points to an object  of the derived class.
Rules For virtual functions:
Pure virtual function:   virtual void display () =0 ;  a class containing one or more pure virtual functions is called an Abstract class….used for inheritance , no object can be created.

INHERITANCE

INHERITANCE
Base class is publicly inherited: Public members of the base class become public members of the derived class.
Base class is privately inherited: Public members of the base class become private members of the derived class.
Private members can never be inherited. But they can be accessed through inherited member functions of the base class.

protected visibility modifier: a member declared as protected is accessible by member functions within its class and any class immediately derived from it.
A protected member can never become public in the derived class, it can either be protected or private (not available for further inheritance)
If inheritance is done in protected mode then both the public and the protected members become protected in the derived class.
Multiple Inheritance : the base classes are separated by commas. If both the base classes have the same function name, then ambiguity can be removed by defining a named instance M::display ();
When derived class overrides the base class function then we can use
b.A::display () ; where b is derived class object and A is the base class.

Virtual Base Classes
Multiple inherited paths can be avoided if the common base class is made a virtual base class.
class b1 : public virtual A
Abstract Classes used only for inheritance (base class), no objects are created
Constructors in Derived classes
If any of the base class has a constructor then it is mandatory for the derived class to have constructors to pass on the values for the constructors of the base class.
How are the base classes constructed? In the order in which they appear in the declaration of the derived class (Multiple) and for multilevel it is executed in the order of inheritance.
Virtual base gets precedence over an ordinary base


Nesting of Classes: Constructors of all the member objects should be called before its own constructor body is executed.

Operator Overloading and Type Conversions

Operator Overloading and Type Conversions
We cannot change its syntax and its original meaning.
operator op is the function name.
member function x.operator op (y) ;    A=B+2 ; legal but A=2+B ; illegal (as the left hand operand is responsible for invoking the member function)
friend function operator op (x,y);  A=2+B; illegal
Example: Overloading >>

Rules of operator overloading:



Type Conversions
  • From basic to class
  • From class to basic
  • From one class type to another
From basic to Class type: By the use of constructors (it takes a single argument whose type is to be converted).
Char * str “hi”;
Myclass ob ;
ob=str; implicitly calls the costructor

From class to basic Type: we have to use a conversion function or a casting conversion function.
The casting conversion function must be class member with no arguments and must not specify any return type.
One class to another class type
Can be done either by a constructor or by a conversion function (Complier will treat in same way).
If constructor is used then the single argument will be the source class, which will be passed to the destination class for conversion.  
If conversion function (defined in the source class, and conversion also in source class) is used then the typename will refer to the destination class.
Summary of Type Conversions.

Constructors and Destructors

Constructors and Destructors
Special member function whose task is to initialize the objects of a class.
A constructor that accepts no parameter is called the default constructor.
A::A (); if no constructor is defined, then the compiler will supply the default constructor.
It can be also defined inline as A () { }; (Once we define a constructor, it is a must? That we define the “do nothing” implicit constructor)
It will be a compilation error if we declare the constructor in the private section. It always must be declared in the public section.
Passing values to parameterized constructors:
  • By calling the constructor Explicitly
Myclass object1 = Myclass (12,12);
  • By calling the constructor Implicitly
Myclass object1 (12,12) ;
Constructors can be inline and can have default values.
The parameters of a constructor cannot be of the class type to which it belongs but can accept a referece to its own class as a parameter.
A::A(A); // illegal
A::A(A&);// Legal (This is Called Copy Constructor)
Simple A = B will not invoke the copy constructor (But it is legal, it will simply assign the values member by member). Only Class_name A (B); or Class_name A=B ; will invoke the copy constructor.
Dynamic Constructors (using new)
Example
Class String {
  Char * str ;
public :
String () {};
String (char *) ;
};

String::String (char *s)
{
      Str= new char [strlen(s)];
     Strcpy (str,s);
}


Constructing Two-dimensional Arrays
class matrix {
  int **p ;  // pointer to the matrix
 int d1,d2;   .. dimensions of the matrix
 matrix (int , int );

} ;
Matrix::matrix (int x,int y)
{
   d1=x;
  d2=y;
 p = new int*[d1] ; // creates an array of pointers of dimensions d1
  for (i=0;i<d1;i++)
   p[i]=new int [d2];
}

const Objects
const matrix X(m,n) ; so the values m and n cannot be changed . And the constant object can call only other  const member fnctions (const keyword appears after the function signature)
Destructors
className::~className(){};
Why required? When an object goes of scope, the destructor is not called implicitly.

Classes and Objects

Classes and Objects
Difference between structures in C and C++, in C structures doesn’t permit data hiding and they cannot be treated like built-in data types. In C++ structures can have both data items and functions.
Difference between a class and a structure in C++ is the fact that by default the members of a class are private, whereas by default the members of a structure are public.
Inside the class we have the function declaration using prototype and the data members.
Class is an abstract data type that can be treated as a built-in data type.
Only member functions can have access to both private data and private functions (they cannot be accessed through an object of the class). But public members can be accessed from outside the class (through an object of that class).
The above figure clearly explains the concept of data Hiding in C++ .
When we declare an object of a class the necessary memory is allocated. Else the class definition is nothing but simply a template.
Defining member functions
  • Outside the class definition
  • Inside the class definition (Inline function but the keyword is not needed). But to make an outside function inline we have to use the keyword inline.
If defined outside then the function name has to be preceded with the identity label, which specifies which class it belongs to. Hence several classes can have the same function name.
A member function can directly call another member function without using the dot operator if they belong to the same class. This is known as nesting of member functions.
Memory Allocation for Objects
Static Data Members
static int count;
All the object share the same copy of the variable
data-type class-name::variable-name ; // definition of a static member. Why should we define it separately ? because it is stored separately and not as a part of an individual object.
Static Member Functions
  • Can have access only to static members.
  • Can be called directly using the class name without creating objects
Class-name:: function-name;

Friend Functions
Private members can be accessed from outside the class, only by using friend function. A friend function is not member of any class.
In the above example the function void is a friend function, so even though it is a non-member it has full access to the private members of the class ABC.
>> A member function of one class can be defined as a friend function to another class.
     friend return-type Other-class::func1();
>> All the meber functions of one class can be defined as friend function of another class
Example program:
#include<iostream.h>
#include<conio.h>
class d;    // this is known as forward declaration

class b {

  int x;
  public:
  void getb (int, d );


};


class d {

 int y ;
 public :
 friend class b;

};


friend  void getb(int x1, d h)
  {
      x=x1;
      cout<<x1;
      h.y=30;
      cout<<h.y;


  }
int main ()
{
 clrscr();
 b h ;
 d i ;
 h.getb(10,i);
 getch () ;
 return 0 ;
}
Const Member Functions
When a function is declared as const, then the function cannot alter any data in the class



Pointers to Members