Pages

Sunday, 24 August 2014

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


No comments:

Post a Comment