Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-07-2009
Member
 
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:
Quote:
Struct Object
(
int x;
int y;
);



thank you
Reply With Quote
  #2  
Old 13-07-2009
Xan Xan is offline
Member
 
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.
Reply With Quote
  #3  
Old 13-07-2009
Member
 
Join Date: May 2008
Posts: 2,383
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.
Reply With Quote
  #4  
Old 13-07-2009
Member
 
Join Date: May 2008
Posts: 2,293
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "C++ struct vs class"
Thread Thread Starter Forum Replies Last Post
Upgrade Sony Ericsson Xperia Play Micro SD card from class 2 8GB to class 4 32GB Aaryan2011 Portable Devices 8 08-10-2011 11:06 PM
Struct and Strings in C Ash maker Software Development 5 05-04-2010 12:31 PM
Enums VS Struct Rum Software Development 5 28-01-2010 08:56 AM
Ultra solid drives:Imation M-Class and S-Class Regina Portable Devices 1 03-04-2009 10:34 AM
Good news for CBSE CLASS X & CLASS IX - visit learnnext surya_expert Education Career and Job Discussions 0 13-12-2008 12:09 PM


All times are GMT +5.5. The time now is 11:50 AM.