Solved

Classes and structures

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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