We know that structure is a collection of heterogeneous data elements under a common name. It is a convenient tool for handling a group of logically related data items. It is a user-defined data type with a template that serves to define its data properties. Although structure is a user-defined data type, but it has some limitations.
Class is a collection of data members and member function bundled together as a unit. Using class, encapsulation as well as data hiding has been possible. Similarly, inheritance and polymorphism has been possible due to class. So, it is the more advanced data structure than the structure of C and it has been removed the drawbacks of the structure. The main drawbacks of structure of Care as follows:
- The standard C does not allow the struct data type to be treated like built-in data types. For example
{
float real;
float imaginary;
};
struct c1,c2,c3;
c3=c1+c2;
The member variable of structure 'complex' can easily be assigned values using the dot(.) operator, but we cannot add two complex numbers or subtract one from the other. So, c3=c1+c2 or c3=c1-c2 both are illegal in C.
But, in C++, we can design a complex class and add or subtract them using + or- operator by the help of polymorphism. For example:
class complex
{
private:
float real;
float imaginary;
public:
complex operator+(operator);
complex(float, float);
}
Complex c1.c2,c3;
C3=c1+c2;
- Another important limitation of C structure is that they do not permit data hiding. Structure members can be directly accessed by the structure variables by any function anywhere in their scope. So, structure members are public members.
For example:
The data members of the structure can be easily accessed by the structure variable in any part of the program such as c1.real, c1.imaginary even from the 'main()' function but the data members of the class cannot be accessed directly outside their member function. So, c1.real or c1.imaginary is not allowed directly in the 'main()' function of the program.
- Another limitation of structure is that the member of one structure cannot be transferred to the another structure but 'class' of COP has solved this limitation so that the features of one class can be easily transferred to another class by means of inheritance.
No comments:
Post a Comment