Solved

Classes and structures

Posted on 2000-03-30
4
279 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

Industry Leaders: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
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 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…

739 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