Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Classes and structures

Posted on 2000-03-30
4
Medium Priority
?
282 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 300 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

885 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