Friday 16 November 2012

Objects and Classes in C++

   


Objects and Classes in C++


In object-oriented programming languages like C++, the data and functions (procedures to


manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects.

Features of Class:

Classes contain data known as members and member functions. As a unit, the collection of members and member functions is an object. Therefore, this unit of objects makes up a class.

How to write a Class:

In Structure in C programming language, a structure is specified with a name. The C++ programming language extends this concept. A class is specified with a name after the keyword class.
The starting flower brace symbol '{'is placed at the beginning of the code. Following the flower brace symbol, the body of the class is defined with the member functions data. Then the class is closed with a flower brace symbol '}' and concluded with a colon ';'.
Sample Code
  1. class exforsys
  2. {
  3.    data;
  4.    member_functions;
  5.    ...........
  6. };

There are different access specifiers for defining the data and functions present inside a class.

Access specifiers:

Access specifiers are used to identify access rights for the data and member functions of the class. There are three main types of access specifiers in C++ programming language:
  • private
  • public
  • protected
  • A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class.
  • Public members are accessible from outside the class.
  • A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.
When defining access specifiers, the programmer must use the keywords: private, public or protected when needed, followed by a semicolon and then define the data and member functions under it.


Sample Code
  1. class exforsys
  2. {
  3.    private:
  4.    int x,y;
  5.    public:
  6.    void sum()
  7.    {
  8.       ....
  9.       ....
  10.    }
  11. };

In the code above, the member x and y are defined as private access. The member function sum is defined as a public access.

General Template of a class:

General structure for defining a class is:
Sample Code
  1. class classname
  2. {
  3.    access_specifier:
  4.    data_member;
  5.    member_functions;
  6.  
  7.    access_specifier:
  8.    data_member;
  9.    member_functions;
  10. };

Generally, in class, all members (data) would be declared as private and the member functions would be declared as public. Private is the default access level. If no access specifiers are identified for members of a class, the members are defaulted to private access.
Sample Code
  1. class exforsys
  2. {
  3.    int x,y;
  4.    public:
  5.    void sum()
  6.    {
  7.       ....
  8.       ....
  9.    }
  10. }
In this example, for members x and y of the class exforsys there are no access specifiers identified. exforsys would have the default access specifier as private.

Creation of Objects:

Once the class is created, one or more objects can be created from the class as objects are instance of the class.
Just as we declare a variable of data type int as:
int x;
Objects are also declared as:
class_name followed_by object_name;

Example:

exforsys e1;
This declares e1 to be an object of class exforsys.
For example a complete class and object declaration is given below:
Sample Code
  1. class exforsys
  2. {
  3.    private:
  4.    int x,y;
  5.    public:
  6.    void sum()
  7.    {
  8.       ....
  9.       ....
  10.    }
  11. };
  12.  
  13. void main()
  14. {
  15.    exforsys e1;
  16.    ....
  17.    ....
  18. }

For example:
Sample Code
  1. class exforsys
  2. {
  3.    private:
  4.    int x,y;
  5.    public:
  6.    void sum()
  7.    {
  8.       ....
  9.       ....
  10.    }
  11. }e1;
The above code also declares an object e1 of class exforsys.
It is important to understand that in object-oriented programming language, when a class is created no memory is allocated. It is only when an object is created is memory then allocated.

No comments:

Post a Comment