Classes And Objects

Introduction : Class is the user defined data type representing similar objects. It binds data and functions together.
Syntax :
 class class_name
{
private :
//variable declaration
//function declaration
protected :
//variable declaration
//function declaration
public :
//variable declaration
//function declaration
};
Example
class account
{
int accno;
char type[10];
float balance;
float deposit(float amount)
{
balance+=amount;
}
float withdraw(float amount)
{
balance+=amount;
}
};

Definition of function

  • outside the class: Definition is done by using scope resolution operator(::).
  • Syntax:
    class_name ::function_name(parameter list)
    {
    function body
    }
  • Example :
    void display()
    {
    cout<<"This is the example of defining the function outside the class";
    }
  • Inside the class,function is defined normally without any operator.
  • Syntax:
    function_name(parameter list)
    {
    function body
    }
  • Example :
void display()
{
cout<<"this is the example of defining the function inside the class";
}

Some points about classes and objects 
  1. Classes does not occupy any memory unlit and unless objects of that class are made.
  2. The data members and member functions are referred by using dot operator(.) with object of the class.
    Example:
    Student S1;
    S1.marks;
  3. Arrays can also be used as the data member of the class. It can be used to perform operations in the functons but cannot be used directly by objects.
Scope of class and its members :
Class represents a group of data and functions which are divided into :
  1. PUBLIC : These data members can be directly accessed by any function(whether present inside the class or outside the class)
  2. PRIVATE :These data members can be used privately by member function of the class in which it is declared. It use the concept of data hiding.
  3. PROTECTED : These data members can be accessed by member functions of the class and the friends of the class in which it is declared.
Global Class : The class which is defined outside the body of the function. Its object can be declared from anywhere in the program.

Local Class The class which is defined inside the function. Its object can be declared only within the function where class is defined.

Global Object : an object is said to be global if it is declared outside all the functions and is available for all functions of the class.

Local Object : An object is said to be local if it is declared inside a function and this type of object cannot be used outside the function where it is declared.

Nested Class : Class declared within another class. The outer class is known as Enclosing class and the inner one is known as Nested class.

Object Declaration Syntax and example :
class_name object list(separated by comma);
student s1,s2,s3;

No comments:

Post a Comment