Difference between struct and class in c++ and c

R

Robben

Hi,

Part of the question could be OT, so please bare with me for that.
As I was told/know/read the main difference between struct and class is
that for a struct all member variables and methods are public.

I managed to write a small program as shown below, and managed to
successfully compile using c++ compiler but did not manage to do the
same with c compiler....

struct A
{
public:
A(){ printf(" A:: constructor \n"); }
~A(){ printf(" A:: destructor \n"); }
void print(){ printf(" A::print \n"); }
};


Does that mean what ever I had learned is pertaining only to C++ and
not C.



Thanks
 
R

Robben

I managed to find the reply from an earlier posting by some body else,
so please ignore my posting.
I should have checked earlier postings before asking a question around
here....

Any how the answer is..
"In reality, there is little difference between a C struct and a
C++ class, at least when it comes to implementation. The main
difference
is that C++ supports (not requires, supports) object-oriented design
techniques
and so makes it a lot easier to code an object-oriented design than in
C"
 
M

msalters

Robben schreef:
Hi,

Part of the question could be OT, so please bare with me for that.
As I was told/know/read the main difference between struct and class is
that for a struct all member variables and methods are public.

I managed to write a small program as shown below, and managed to
successfully compile using c++ compiler but did not manage to do the
same with c compiler....

struct A
{
public:
A(){ printf(" A:: constructor \n"); }
~A(){ printf(" A:: destructor \n"); }
void print(){ printf(" A::print \n"); }
};


Does that mean what ever I had learned is pertaining only to C++ and
not C.

Yep. Constructors, destructors, the keywords class,public,protected and
private are all new in C++. These are needed to make C++ an OO
language.
That's not a goal of C.

You should avoid mixing the languages, certainly until you're more
familiar with both.

HTH,
Michiel Salters
 
T

tmartsum

Same with virtual ....
You opions must be from the fact that there in C++ is no difference
between

struct A
{

}
and
class A
{
public:

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top