Ad

Friday, June 15, 2007

Introduction to Classes in C++

Classes are the building blocks of Object Oriented Programming in C++ language. Class gives us the ability to simplify certain types of programs by creating objects that forms the basis of Object Oriented programming.

Just like identifiers of built-in data types (ex. int, char etc.) are known as variables similarly identifiers (instances) of class are known as Objects.

Classes in C++ have two parts, data and functions; these are known as data members and member function respectively. Member functions are usually the means of accessing and modifying data members.

Anything inside the class can be either private to the class or public to the program. Classes may have both type of members (private and public) at the same time.

General form of Class:

  class class_name
  {
   access-specifier:
   member…
   member…
   member…

   access-specifier:
   member…
   member…
   member…
  }object-list;

Few points to remember:

  • Access-specifier can be anyone of the three private, public and protected.

  • Anything after an access-specifier has that type of access until another access-specifier is encountered.

  • There can be any number of access-specifier, but in general all the members having same access are grouped together under one access-specifier.

  • So,

    
    class c1
    {
     private;
       int a;
       int b;
          
     public:
       int c;
         
     private:
       int d;
    };

    Should be written as

    class c1
    {
     private:
       int a;
       int b;
       int d;
          
     public:
       int c;
    };
         
  • Object list is optional which is used to declare objects of the class.

private and public Access-Specifiers

private tells the compiler that the members following it are private to the class and can not be accessed by other parts of the program.

Ex.


  class c1
  {
   private:
     int a;
     int b;
     void func();
  };

Here, the variable a, b and function func() can can only be accessed by the members of the class c1.

On the other hand, public members are accessible to the other parts of the program also.


  class c1
  {
   private:
     int a;
     int b;
     void func();
   public:
     void func2();
     int c;
  }obj;

Here the variable c and function func2() are accessible from other parts of the program, but the private members are only accessible from the public function func2().

The public members of the class (i.e. func2() and c) can be accessed from other parts of the program with the help of the following syntax:

obj.c=10;

functions (public) are also accessed like this:

obj.func2();

A few points to remember:

  • Please note that in the previous example a, b and func() can not be accessed from other parts of the program with the help of (.) operator.

  • Member functions can simply access any of the private and public members of the class without the use of (.) operator.

  • For example, if func2() is a member function class c2 then it can access other members (both private and public) as shown below:

    func2()
    {
     a=b;
     b=c;
     func();
    }
    

Enough talking… Now let us move on to a simple program to illustrate:

  • How to define a class and its objects in C++.

  • Use of private and public access-specifiers.

  • How to define and use member functions.

  //C++ Program
  #include<stdio.h>
  #include<iostream.h>

  class employee
  {
   private:
   char name[30];
   int emp_no,age;
   float salary, net_salary, tax;
   void calculate()//in line function
   //used for the short ones
   {
   net_salary=salary-tax;
   }

   public:
   void input();
   void output();
  };

  //----FUNCTION DEFINITION STARTS----
  void employee::input()
  {
   printf("Enter Name: ");
   gets(name);
   printf("Enter Employee Number: ");
   cin>>emp_no;
   printf("Enter Age: ");
   cin>>age;
   printf("Enter Salary: ");
   cin>>salary;
   printf("Enter Tax Paid: ");
   cin>>tax;

   //the function below can only be invoked
   //from the member function like here
   //and not from the other parts of the program
   calculate();
  }

  void employee::output()
  {
   printf("\n\n EMPLOYEE DETAILS\n");
   //'\n' is used to print to the next line
   printf("Name: %s\n", name);
   printf("Age: %d\n", age);
   printf("Employee Number: %d\n", emp_no);
   printf("Net Salary: %f\n", net_salary);
  }
  //----FUNCTION DEFINTION ENDS----

  void main()
  {
   employee e1;

   e1.input();
   e1.output();
  }

A few points about the program:

  • Notice how member functions are used to modify data members of the class.

  • Notice the use of private function calculate(), that can only be accessed from other member function (i.e. input() and output()).

  • Notice how the member functions are declared and defined.

This article has gone a bit too long but once the discussion on topics like classes in C++ starts it never seems to end -;)

Good-bye for now!

3 comments:

  1. Anonymous5:57 PM

    how do i use c language instead of c++ for that codes u wrote .. thanks ..

    ReplyDelete
  2. @ Anonymous,

    You cannot use the above code in C language as classes are only supported in C++. C supports structure though.

    Please ask if you need more clarification.

    ReplyDelete
  3. Anonymous9:45 PM

    is there any tricks to learn classes in c++

    ReplyDelete

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.