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
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
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.
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.
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:
code is a valid .
- class Point (public: int X, int Y; ) / / class
- struct Point (int X, int Y; ), / / or struct
For if it was against a date, there are verification to do
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 32struct Date (
int days;
int month;
)
The must use encapsulation to ensure its validity.
And like this we have on at any time a valid date.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;
)
)
Bookmarks