TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   C++ struct vs class (http://forums.techarena.in/software-development/1213159.htm)

avvia 13-07-2009 10:38 AM

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:
Quote:

Struct Object
(
int x;
int y;
);



thank you

Xan 13-07-2009 10:40 AM

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.

opaper 13-07-2009 10:46 AM

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.

Zecho 13-07-2009 10:53 AM

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:

Quote:

  • 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
Quote:

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.
Quote:

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.


All times are GMT +5.5. The time now is 05:26 AM.