Solved

Classes and structures

Posted on 2000-03-30
4
272 Views
Last Modified: 2010-04-02
What's  the difference between CLASS
and STRUCTURE?
Where is better to use Class or Structure?
What does "typedef" keyword mean?
0
Comment
Question by:radek_s
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2670601
>> What's  the difference between CLASS
>> and STRUCTURE?
In C++ almost nothing.   The class concept in C++ was created by adding features to the C struct concept.  Hopwever ALL the feature that were added to class were also added to struct.  So EVERYTHIGN that can be done with class can also be done with struct.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2670617
However there is a small difference between class and struct in C++ that is important, but does not affect their functionallity.   In a struct all data members defailt to public access and base classes default to being publicly inheirited from.  In a class all data members default to private access and base classes default to being privately inhierited from.  But this difference is only in the default access.  You can overide this difference by specifying the private/protected/public access specifiers.  so using these specifiers you can make class act like struct or struct act like class.

continues
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 2670626
The only difference between a class and a struct is that for a struct, all members are defaulted to public. In a class, they are defaulted to private (unless specified otherwise). The following are the same:

struct Obj
{
....
};

class Obj
{
public:
....
};

So, unless you need to use structs defined within legacy code, you should just use classes instead of structs;

---

"typedef" stands for type define. It can be used to define your own types.

typedef int MyInt;

MyInt num = 5;

It is usually used to shorten long or confusing types (such as those that may use templates);

typedef map< string, string, less<string> >  StringMap;

StringMap mapObj;
0
 
LVL 22

Expert Comment

by:nietod
ID: 2670640
>> Where is better to use Class or Structure?
this depends on your tastes.  There is no "best" answer.

Most programmers like to use struct for agggregates that use only C's features.  i.e. aggregates that only have data members and don't have member functions, static members or inheritance.  Then they use class for aggregates with the additional C++ features.

>> What does "typedef" keyword mean?
It defines a name that can be used in place of another type.  Like if you want to use MyPtrTyp in place of an uhnsigned character pointer you could do.

typedef unsigned char *MyPtrTyp;
MyPtrTyp Ptr;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now