Results 1 to 4 of 4

Thread: C++ struct vs class

  1. #1
    Join Date
    Feb 2009
    Posts
    83

    C++ struct vs class

    hi
    i am interested to know the the difference between C++ struct and class? can any one tell me ?

    I heard you could create a class in another way: struct:
    Struct Object
    (
    int x;
    int y;
    );



    thank you

  2. #2
    Join Date
    Jan 2009
    Posts
    576

    Re: C++ struct vs class

    A class is actually a special structure, if you want, an improved structure (as c + + is an enhancement of c),there is in the classroom compared to the structures are:
    -The classes contain functions (methods).
    -The classes offer access rights (encapsulation).
    -The classes are the basis for OOP in C + +.
    and finally, a class can contain one or more structures.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: C++ struct vs class

    Access specifier is difference but as security is concern, this is a drawback of structure. Because for outside members are open by default to all. That is the one main reason, why programmers not use structure. where in class encourages encryption/data-hiding by default. So better to use class, than structure.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: C++ struct vs class

    For the difference between struct and class in C + + I can confirm the only difference is that struct defaults to public and private class ... that's all.

    when the encapsulation, it may well have access to members of the class directly without using the methods, the only thing is that we should not do it when the class guaranteed tips.

    Example:

    • class Point (public: int X, int Y; ) / / class
    • struct Point (int X, int Y; ), / / or struct
    code is a valid .
    For if it was against a date, there are verification to do
    struct Date (
    int days;
    int month;
    )
    invalid because we can have date D; D.day = 32, D.month = 11 / / D is invalid because there's only 30 days in November and not 32

    The must use encapsulation to ensure its validity.
    class Date (
    int days;
    int month;
    public:
    Date () (day = 1; months = 1; )
    void Set (int j, int m) (
    / / Full audits
    if (j <= 0) (printf ( "error"); return; )
    if (j> 31) (printf ( "error"); return; )
    if (m> 12) (printf ( "error"); return; )
    if (j == 2) & (m> 28) (printf ( "error"); return; )
    ....
    day = d; month = m;
    )
    )
    And like this we have on at any time a valid date.

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. Struct and Strings in C
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 05-04-2010, 12:31 PM
  3. Enums VS Struct
    By Rum in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 08:56 AM
  4. Ultra solid drives:Imation M-Class and S-Class
    By Regina in forum Portable Devices
    Replies: 1
    Last Post: 03-04-2009, 10:34 AM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,881,201.34362 seconds with 17 queries