Ad

Sunday, June 24, 2007

Classes & Objects: Dynamic Memory Allocation

Just as variables of pre-def data type are dynamically allocated, same way objects of class can also be dynamically allocated. While the method of doing this is no different but the way members of such objects are accessed is a bit different. This is why I thought of writing a separate article on it.

Before discussing any further, look at the program below:

  // Example Program in C++
  #include<iostream.h>

  // class
  class myclass
  {
   // by default everything is
   // private to the class
   int var1, var2;
   void add();

   public:
   int sum;

   void input(int,int);
  };

  // function definitions
  void myclass::input(int a, int b)
  {
   var1=a;
   var2=b;
   add();
  }

  void myclass::add()
  {
   sum=var1+var2; 
  }

  // main function starts
  void main(void)
  {
   myclass obj; // simple object

   myclass *pobj; // pointer
   // of type 'myclass'

   pobj=&obj; // now 'pobj' pointer
   // points to the object 'obj' of
   // class 'myclass'

   // members are accessed by the '.'
   // operator 
   obj.input(10,20);
   cout<<obj.sum;
   cout<<endl;

   // in case of pointers of class,
   // members are accessed by the
   // '->' arrow operator
   pobj->input(30,40);
   cout<<pobj->sum;
   cout<<endl;
  }

Did you notice the difference?

Members of the object of the class as you know are accessed like this:

object.membername;

While, in case of pointers of the class, the members are accessed like this:

objectptr->membername;

This is the difference I wanted to show you!

As in case of dynamic memory allocation, we have to declare pointer of the class, therefore members of the dynamically allocated objects will be accessed by the (->) arrow operator.

Arrays and pointers are closely related. While the members of the pointers of objects are accessed by the (->) operator, in case of arrays of objects, we have to use ‘.’ operator instead.

The example program below will clear the confusion, if any:

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

  class employee
  {
   private:
   char name[30];
   int emp_no,age;
   float salary, net_salary, tax;
   void calculate();

   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;
   printf("\n\n");

   calculate();
  }

  void employee::output()
  {
   printf("Name: %s\n", name);
   printf("Age: %d\n", age);
   printf("Employee Number: %d\n", emp_no);
   printf("Net Salary: %f\n", net_salary);
   printf("\n\n");
  }

  void employee::calculate()
  {
   net_salary=salary-tax;
  }
  //----FUNCTION DEFINTION ENDS----

  // main function starts
   void main(void)
   {
   employee *emp_obj;
   int num_emp;

   printf("Enter Number of Employees: ");
   cin>>num_emp;

   emp_obj=new employee[num_emp];
   // terminate if memory allocation
   // failed
   if(emp_obj==NULL) exit(1);

   // take input
   for(int i=0;i<num_emp;i++)
     // object are accessed as arrays
     // so '.' operator is used
     // we can also write
     // (emp_obj+i)->input(); to do the
     // same thing
     emp_obj[i].input();

   printf("\n\n EMPLOYEE DETAILS\n");

   // give output
   for(i=0;i<num_emp;i++)
     emp_obj[i].output();
  }

As in the comments, we can either use

   emp_obj[i].input();
   or
   (emp_obj+i)->input();

to do the same thing, in the first case the object is referenced as an array while in the other case, it is referenced as pointer. So the appropriate operator is used in each case.

Hope this article helps!

2 comments:

  1. Anonymous7:27 AM

    thnx, ALOT
    this article was a life-saver :)

    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.