Thursday 23 May 2013

Structure in C++


Structure in C
l  Structure is a collection of dissimilar elements.
l  Structure is a way to group variables.
l  Structure is way to create user defined data type

sruct book
{
  int id;
  float price;
  char title[20];
};

l  Here, book is a user defined data type.
l  In above example, structure book is a group of three variables id, price and title.
l  We can easily understand that these variables are of different type.
l  Do not forget to put semicolon at the end of structure body.
l  Till the structure is only defined and not actually used to create variables, no space is allocated for these variables.
l  When defining structure, mentioning variables in structure body is not actually creating variables (hence no memory consumed), instead we are just defining a memory model for those variables that could be created using data type book.

Creating structure variables

sruct book
{
  int id;
  float price;
  char title[20];
};

main()
{
  struct book b1,b2;
  …
  …
}

l  Here, we declare two variables of type book.
l  Each variable b1 and b2 will consume 26 bytes of memory space (i.e. sum of the memory consumed by member variables)
l  In C, keyword struct should always be prefixed during each use of such user defined data type.

Accessing members of structure

sruct book
{
  int id;
  float price;
  char title[20];
};
main()
{
  struct book b1,b2;
  printf(“Enter id, price and title of book”);
  scanf(“%d%f”,&b1.id, &b1.price);
  gets(&b1.title[0]);
  b2=b1;
  …
  …
}

l  Variables b1 and b2 both are of type book consuming 26 bytes each, containing three variables: id, price, title.
l  Members cannot be accessed freely, it should always be prefixed with variable of book type and dot( . ) operator.
l  We can copy the contents of b1 in b2, as shown in the above example. This helps an easy access to group of variables. No need of copying each and every element one by one.

Differences between structures of C and C++

l  In C++ a structure can have both variables and functions as members. In C a structure can have only variables as members.
l  In C++ members can be private, protected or public but in C there is no facility of access specifiers (Private, protected and public are access specifiers and we will discuss them in the next segment).
l  In C++, a structure can have inheritance. On the other hand in C a structure does not support inheritance.
l  In C++, the struct keyword can be omitted in the declaration of structure variables, but in C, the struct keyword is mandatory during the use of structure defined data-type.
Example:
#include<iostream.h>
struct complex
{
     private:
     int a;
     int b;
     public:
     void getdata()
     {
          cout<<”Enter two numbers”;
          cin>>a>>b;
     }
     void showdata()
     {
          cout<<”a=”<<a<<”b=”<<b;
     }
};
void main()
{
  complex c1,c2;
  c1.getdata();
  c2.getdata();
  c1.show();
  c2.show();
}


l  In C++, structure may contain functions along with variables as members.
l  Here, we defined four members; two are member variables (a and b) and two are member functions (getdata() and showdata())
l  Notice that we define member variables with access specifier private. Also, member functions with access specifier public.
l  Those members are private, cannot be accessed from outside the structure body.
l  Those members are public, can be accessed from outside the structure.
l  We can define structure in C++ exactly in the same way as we do in C. This is due to support backward compatibility.
l  If we do not mention any access specifier for members of structure, then it is assumed public.

Class and Structure in C++ are similar

l  Just replace the keyword struct with keyword class.

#include<iostream.h>
class complex
{
     private:
     int a;
     int b;
     public:
     void getdata()
     {
          cout<<”Enter two numbers”;
          cin>>a>>b;
     }
     void showdata()
     {
          cout<<”a=”<<a<<”b=”<<b;
     }
};
void main()
{
  complex c1,c2;
  c1.getdata();
  c2.getdata();
  c1.show();
  c2.show();
}

l  You can very well understand that the definition style and accessing methods of class members are exactly same as we did in structure example.
l  The only difference between class and structure in C++ is that by default the members of a class are private; on the other hand, by default the members of a structure are public.
l  A class is a way to bind the data and its associated functions together. (encapsulation)
l  In C++, class and structure both allows the data and functions to be hidden, if necessary, from the external use. (data hiding)
l  In C++, when defining a class or structure, we are creating a new abstract data type that can be treated like any other built in data type.

A common question arises why concept of class exist when structure fulfill all the requirements. The strong reason concerns maintaining upward compatibility from C. In C++, a C-style structure is also perfectly acceptable in a C++ program. Since in C all structure members are public by default, this convention is also maintained in C++. Further because class is syntactically separate entity from struct, the definition of a class is free to evolve in a way that will not be compatible with a C like structure definition. Since the two are separated, the future direction of C++ is not restricted by compatibility concerns.

No comments:

Post a Comment