Answer: – A structure is a collection of one or more variables called data members of different data types, grouped together under a single name and new custom data type is formed and is called abstract data type (ABT). The individual variables are called member or data member. By using structure, we can make a group of variable, arrays, pointers, etc. these new data types found by using structure in independent of computer hardware. The data members of structure are blue print and it initialize memory location only when we declared its structure variable with help of structure name either global or local. The data members are similar to variables but it can be use in the program with help of dot operator.

Class, on the other hand, is also an abstract data type. It is a collection of one or more variables with different data type and number of function in three different class specifiers. Here variables are called data members and functions are called member function. The data member and the member function are grouped as single entity. The data member and the member function are declared in three different section called class specifier. They are called PRIVATE member, PUBLIC member and PROTECTED member. Private member use member function and data members in the same class. Public members are useful for use data members and member function in the same class as well as in the different class. Function of protected member is similar to the private members but its member function and data member are used in inheritance class.

Structure of C++ work similar to class of C++. Structures are used in programming for structured data type whereas class is used in programming as object data type. Structure uses only data members but class uses data members and member function. Data members of structure can be declared without any section where as data members and member function of class can be declared in three different section.

An example of a structure and a class of a C++ programming is as fallows –

Structure –

struct student
{
                int roll;
                char name[20];
                int total;
                char div[10];
};

Class –

class student
{
private:
                int roll;
                char name[20];
                int total;
                char div[10];
public:
                void input()
                {
                                cout<<"Enter Roll No : ";
                                cin>>roll;
                                cout<<"Enter Name : ";
                                cin>>name;
                                cout<"Enter Total Marks : ";
                                cin>>total;
                                cout<<"Enter Division : ";
                                cin>>div;
                }
                void output()
                {
                                cout<<"Roll No : "<<roll<<endl;
                                cout<<"Name : "<<name<<endl;
                                cout<<"Total Marks : "<<total<<endl;
                                cout<<"Division : "<<div<<endl;
                }
}

 

Advertisements