Solved

Classes and structures

Posted on 2000-03-30
4
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

623 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