Home TechnologyProgrammingC++ Subscribe to RSS
 

Is Object In Classes Different From The Structure Variable Of A Particular Structure Type In C++?

Answer Question

1 Answer - Sort by: Date | Rating

    A structure in C++ is defined as 'a collection of simple variables'.  It can have variables of different data types, and each variable (data item) within the structure is called MEMBER of the structure.
    Example of a structure declaration would be as follows:
    struct Distance
    {
       int feet;
       float inches;

    };

    To use this structure a variable of Distance data type has to be defined like this:
    Distance a;
    And members can be accessed using the DOT OPERATOR like this:
    a.feet = 5;

    Similarly, if we look at the definition of a class, it is defined in much the same way.  It also has data items, its variables are also defined in the same way, and data members are accessed with DOT OPERATOR.

    class myClass
    {
       int d1;
       float d2;
    };

    myClass a;
    a.d1 = 5;

    So, objects in classes (data items of a class) are NOT different from structure variables (data items or members of a structure).  In fact in C++ structures and classes can be used interchangeably.  But the way it is mostly taught is that structures can only hold data members(data items) while classes can have data members(data items) and well as member functions (functions defined or declared within a class).

    Lastly, one important thing that a class has and structure doesn't is PRIVATE, PROTECTED AND PUBLIC specifies that protect data members and member functions from unnecessary access
    0 0

    Rocksolid 

    answered 3 years ago

      Answer Question - Answers are editable for 5 min.

      If you do not Sign-in or Register your answers will be anonymous,

      your answers may also be checked before going online.

      More

      More

         
         

        Ask a Question via Twitter

        Send a question to @askblurtit and we will publish it online and send you a reply everytime you receive an answer.

        Blurtit Store

        Get T-shirts, hoodies, caps and more at the Blurtit store

        Blurtit International