[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]


    Search the Q&A Archives


...difference between class and structure

<< Back to: Comp.Object FAQ Version 1.0.9 (04-02) Part 5/13

Question by harsh
Submitted on 6/4/2004
Related FAQ: Comp.Object FAQ Version 1.0.9 (04-02) Part 5/13
Rating: Rate this question: Vote
what is the difference between class and structure



Answer by Bhupinder Singh Sehra
Submitted on 8/19/2004
Rating:  Rate this answer: Vote
one of the most basic difference is that classes are generally private in nature whereas structure is public  

 

Answer by shilpa
Submitted on 9/14/2004
Rating: Not yet rated Rate this answer: Vote
Dear user,
The ONLY DIFFERENCES between classes and structures are

1)  classes DEFAULT to having private members.  Structures DEFAULT to having public members. These defaults can be changed so classes can be made to work like structures and vice versa.

2)  classes DEFAULT to inheriting privately from base classes.  Structures DEFAULT to inheriting publicy from base classes. These defaults can be changed so classes can be made to work like structures and vice versa.

 

Answer by rajasheker
Submitted on 11/1/2004
Rating: Not yet rated Rate this answer: Vote
structure is a user defined datatype we can define our own data types
where as class is collection of member functions & variables

 

Answer by nitinsawhney
Submitted on 11/5/2004
Rating: Not yet rated Rate this answer: Vote
Functionally structures and classes are similar. In fact, you can use structures in almost exactly the same way that you use classes. The only formal difference between a class and a structure is that in a class the members are private by default, while in a structure they are public by default.

So for example:

class sample
{
  private:
      int data;

  public:
      void fun()
      {
        // some code
      }
};

is similar to

struct sample
{
  private:
      int data;

  public:
      void fun()
      {
        // some code
      }
};

Though in principle we can use a structure at every place where a class is used, in most situation programmers prefer to use structures to group data, and classes to group both data and functions.

 

Answer by SSS
Submitted on 11/11/2004
Rating: Not yet rated Rate this answer: Vote
dont know

 

Answer by Sudhakar Munjuloori
Submitted on 12/1/2004
Rating: Not yet rated Rate this answer: Vote
Member variable(s) / Function(s) that are declared in a structure are public by default. Where as member variable(s) / Function(s) that are declared in a class  will comes under private section by default.

 

Answer by Laxmikant
Submitted on 12/24/2004
Rating: Not yet rated Rate this answer: Vote
There is a difference though, in C++ structure the
member variables and functions are public by default
while in a class they are private by default. AFAIK
that seems to be the only difference other than the
names themselves. I suppose under the hood structure
and class is seen differently but at a higher level
its almost the same

To cut a unnecesarily long discussion short , the only difference between a
struct and a class "IN C++" is that by default , the scope of a struct
variable is public in case of a struct and its private in the case of a
class. Other than that , a struct is capable of doing everything that a
class does in c++.

 

Answer by vdg990
Submitted on 1/10/2005
Rating: Not yet rated Rate this answer: Vote
The class may have static members.

 

Answer by malarkodip
Submitted on 2/3/2005
Rating: Not yet rated Rate this answer: Vote
Structures are very similar to Classes. Like Classes, they too can contain members such as fields and methods. The main difference between classes and structures is, classes are reference types and structures are value types.

 

Answer by sandeep
Submitted on 2/6/2005
Rating: Not yet rated Rate this answer: Vote
By default the members of the class are private
and by deafult the data memebers are public

 

Answer by Fairoz Matte
Submitted on 2/20/2005
Rating: Not yet rated Rate this answer: Vote
what is basic difference between structure and class. why we can't use structures as a class in c++

 

Answer by peace
Submitted on 3/17/2005
Rating: Not yet rated Rate this answer: Vote
there is no difference. ha ha ha !!!

 

Answer by gouri
Submitted on 6/14/2005
Rating: Not yet rated Rate this answer: Vote
1) STRUCTURE is of value type,storage will be on stack whereas CLASS is of reference type, storage will be on heap.

2) STRUCTURE does not support inheritance while a CLASS does.

 

Answer by Umer
Submitted on 6/21/2005
Rating: Not yet rated Rate this answer: Vote
The only difference between structs and classes in C++ is that the members of a struct have public visibility by default, and the members of a class have private visibility by default.


 

Answer by vamseek84
Submitted on 7/8/2005
Rating: Not yet rated Rate this answer: Vote
the differences between structure and class are
1.the members in struct are default public, where as the members in class are default private.
2.We can derive new classes from a class where as, we can not do the same with structure

 

Answer by Ranjit
Submitted on 7/10/2005
Rating: Not yet rated Rate this answer: Vote
1.structures are value types but classes are reference type
2. you cannot define a default constructor for a structure but you can do it for a class
3. structures can not be inherited but classes can be

 

Answer by Gaurav
Submitted on 7/13/2005
Rating: Not yet rated Rate this answer: Vote
In C++ there is one and only one difference between Structure and Classes, which is that in  Classes data & functions are by default private and in Structure they are public by default. That is, we can use Structure in place of Class in C++.

 

Answer by bhk
Submitted on 7/16/2005
Rating: Not yet rated Rate this answer: Vote
my nickname

 

Answer by latha
Submitted on 7/26/2005
Rating: Not yet rated Rate this answer: Vote
class:-The variables and methods are private default.

Structure:-But in structure the functions and variables are taken as public default.

 

Answer by nagaraju
Submitted on 8/4/2005
Rating: Not yet rated Rate this answer: Vote
in structure all members are deafultly declared as public where as in class all members are deafultly privite

 

Answer by bala
Submitted on 8/25/2005
Rating: Not yet rated Rate this answer: Vote
i didn't know

 

Answer by pallavi
Submitted on 9/19/2005
Rating: Not yet rated Rate this answer: Vote
pallavi

 

Answer by kar
Submitted on 10/10/2005
Rating: Not yet rated Rate this answer: Vote
a structure is used in C language to create an object oriented programing environmnet but in java and some other languages classes are containes which along with their instances called objects can be used to implement object oriented programming

 

Answer by Nishchal
Submitted on 11/4/2005
Rating: Not yet rated Rate this answer: Vote
1. The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive.

2.When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

3It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values.
4 It is an error to initialize an instance field in a struct.
5 There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do.
6 A struct is a value type, while a class is a reference type

 

Answer by jyo
Submitted on 11/15/2005
Rating: Not yet rated Rate this answer: Vote
class have axis specifiers where a structure doesn't have

 

Answer by sandu
Submitted on 12/2/2005
Rating: Not yet rated Rate this answer: Vote
the only difference between a structure and a class is that structure members have public access by default and a class members have private access by default

 

Answer by Rajesh Kasturi
Submitted on 12/16/2005
Rating: Not yet rated Rate this answer: Vote
Structures are value types and classes are refference type
For structures memory is allocated in Stack and for classes memory is allocated in heap.
We can not inherit the structures.
we can inherit the classes.

 

Answer by Anamika
Submitted on 12/19/2005
Rating: Not yet rated Rate this answer: Vote
1) Main difference between class and structure is that the default members in class are private while in structure are public.

2) We can use cladd directly as a user defined data type in CPP while we have to create the typedefination to use a structure variable in case of structure.


We can write member functions in both structure and Class in C++. and also can use the public,private keywords in both.

 

Answer by Kaushal
Submitted on 12/20/2005
Rating: Not yet rated Rate this answer: Vote
Although Classes and structures are lot of similarities, there are primary difference and a few minor differences. Difference between a structure and class is centered on how a structure is stored and accessed. A structure is a value data type, and a class is a reference data type. if total size of the data members being stored is 16 bytes or less, use a structure, else Class.

 

Answer by Mahajan
Submitted on 1/5/2006
Rating: Not yet rated Rate this answer: Vote
main difference between classes and structures is, classes are reference
types and structures are value types

 

Answer by prp
Submitted on 2/1/2006
Rating: Not yet rated Rate this answer: Vote
The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and a class members have private access by default, you can use the keywords class or struct to define equivalent classes.

For example, in the following code fragment, the class X is equivalent to the structure Y:

class X {

  // private by default
  int a;

public:

  // public member function
  int f() { return a = 5; };
};

struct Y {

  // public by default
  int f() { return a = 5; };

private:

  // private data member
  int a;
};

If you define a structure and then declare an object of that structure using the keyword class, the members of the object are still public by default. In the following example, main() has access to the members of obj_X even though obj_X has been declared using an elaborated type specifier that uses the class key class:

#include <iostream>
using namespace std;

struct X {
int a;
int b;
};

class X obj_X;

int main() {
  obj_X.a = 0;
  obj_X.b = 1;
  cout << "Here are a and b: " << obj_X.a << " " << obj_X.b << endl;
}

The following is the output of the above example:

Here are a and b: 0 1


 

Answer by Deepak S
Submitted on 3/5/2006
Rating: Not yet rated Rate this answer: Vote
In structure only the arguments are defined
in class the functions can alsobe defined

 

Answer by Ashwini
Submitted on 5/4/2006
Rating: Not yet rated Rate this answer: Vote
1:By default,the members of structures are public while that for class is private

2: strutures doesn't provide something like data hiding which is provided by the classes

3: structures contains only data while class bind both data and member functions

 

Answer by Lakshmi
Submitted on 5/10/2006
Rating: Not yet rated Rate this answer: Vote
"Structure can be declared without a tag at the first time, but not in case of class."
Wrong. The fault is in the example code. Old code:
struct { int something; } sFoo;

sFoo sfoo; // works fine



class { int something; } cFoo;

cFoo cfoo; // error: cannot instantiate

The problem is not about classes and structures; it's about default publicity. Structures are default public and classes are default private. When declared in that fashion, since there is no explicit constructor, a constructor is generated by the compiler for each. The constructor for the structure is default public, so the structure constructs just fine. However, the constructor for the class is default private, meaning that at instantiation time, the programmer does not have the access privelege to construct it. The error that results in most compilers talks about being unable to instantiate the class, and that can easily be misunderstood as a code bug, when in fact it's a usage bug.


 

Answer by vilu
Submitted on 7/13/2006
Rating: Not yet rated Rate this answer: Vote
only one differences that is  default access of both structure as well as class.The default access of the class is private and other-side in structure default access is public.

 

Answer by Ash
Submitted on 7/24/2006
Rating: Not yet rated Rate this answer: Vote
1)   By default, the members of a struct are public, and the members of a class are private.
2)   Structs are by default inherited publicly and classes by default are inherited privately.

 

Answer by giri
Submitted on 8/8/2006
Rating: Not yet rated Rate this answer: Vote
Difference is class object are stored in heap and structure objects are stored in stack, so while accessing the structure is faster than class

 

Answer by mou
Submitted on 8/11/2006
Rating: Not yet rated Rate this answer: Vote
The only difference between structure and class  is  that structure is default public, whereas class is default private...

 

Answer by Satya
Submitted on 8/22/2006
Rating: Not yet rated Rate this answer: Vote
The only difference between struct and class is the default access,

By default all the memebers in Struct have Public access, but in class by default all the members have Private access.

 

Answer by vikas yadav
Submitted on 9/25/2006
Rating: Not yet rated Rate this answer: Vote
Classes are reference type while Structures are value type, i.e. data of classes are stored in a heap while the data of a structure stored in a stack.

 

Answer by Dolby
Submitted on 11/2/2006
Rating: Not yet rated Rate this answer: Vote
We can use functions in Class but in structure we can not use

 

Answer by anishrai
Submitted on 11/11/2006
Rating: Not yet rated Rate this answer: Vote
structure programms when saved as ".c" extension,it will not allow even to declare functions in it.but when saved with ".cpp" extension, it is treated same as the class.struct supports all the concepts of a class with ".cpp" extension

 

Answer by Suni Yadav
Submitted on 11/16/2006
Rating: Not yet rated Rate this answer: Vote
"''A structure is an entity that is defined in terms of other structures or primitive datatypes. As a result, a structure is normally considered to be a complex-, or compound datatype. With stuctures the default access type is public. Classes are similar to structures in that they are made up of other datatypes, but they differ in one significant respect - classes contain information on the program functions (formally termed "methods") that will be used to manipulate the data that the class can store. The default access type for classes is private.

structures are value typed where as classes are refernce typed

Class can be inherited But Structure can't be inherited

'In structures we cannot initilase the variable during the declaration while in classes we can.

Structure s does not support polymorphism while class does

 

Answer by shoaib
Submitted on 11/30/2006
Rating: Not yet rated Rate this answer: Vote
shoaib

 

Answer by sharan
Submitted on 12/20/2006
Rating: Not yet rated Rate this answer: Vote
Structs-Values types,Cannot be instantiated,
Not inheritable,All are default by public

 

Answer by bashir
Submitted on 1/31/2007
Rating: Not yet rated Rate this answer: Vote
by default private is in class if we declare if not declare then private is by default structer.


 

Answer by sara
Submitted on 4/9/2007
Rating: Not yet rated Rate this answer: Vote
[b]Structure:[/b] In C a structure was used to bundle different type of data types together to perform a particular function. But C++ extended the structure to contain functions also.  
Class: Class is a successor of Structure. By default all the members inside the class are private.

[B]The Difference:[/B]
Structure doesn'tsupport the polymorphism, inheritance and initialization.

In a Structure all the data types are public but in class they are private.

In a Structure we can't initialse the value to the variable but in class variable we assign the values.

Structure is a collection of the different data type.

 

Your answer will be published for anyone to see and rate.  Your answer will not be displayed immediately.  If you'd like to get expert points and benefit from positive ratings, please create a new account or login into an existing account below.


Your name or nickname:
If you'd like to create a new account or access your existing account, put in your password here:
Your answer:

FAQS.ORG reserves the right to edit your answer as to improve its clarity.  By submitting your answer you authorize FAQS.ORG to publish your answer on the WWW without any restrictions. You agree to hold harmless and indemnify FAQS.ORG against any claims, costs, or damages resulting from publishing your answer.

 

FAQS.ORG makes no guarantees as to the accuracy of the posts. Each post is the personal opinion of the poster. These posts are not intended to substitute for medical, tax, legal, investment, accounting, or other professional advice. FAQS.ORG does not endorse any opinion or any product or service mentioned mentioned in these posts.

 

<< Back to: Comp.Object FAQ Version 1.0.9 (04-02) Part 5/13


[ Home  |  FAQ-Related Q&As  |  General Q&As  |  Answered Questions ]

© 2008 FAQS.ORG. All rights reserved.