GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 4 of 203    Print  
What is the difference between class and structure?
 Structure: Initially (in C) a structure was used to bundle different type of data types together to perform a particular functionality. But C++ extended the structure to contain functions also. The major difference is that all declarations inside a structure are by default public.
 Class: Class is a successor of Structure. By default all the members inside the class are private.
 


  
Total Answers and Comments: 13 Last Update: September 18, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Gagan
 
There should be no confusions. The only difference between a structure and a class is that all members in a class are private by default whereas they are public in a structure.

Above answer was rated as good by the following members:
OSaienni, vjy_2107
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
July 05, 2005 00:42:34   #1  
girish        

RE: What is the difference between class and structure?
structure member are public in default and class members are privare in default.

 
Is this answer useful? Yes | No
March 20, 2006 04:46:33   #2  
Sharath        

RE: What is the difference between class an...

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


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
November 17, 2006 00:17:33   #3  
sunil yadav        

RE: What is the difference between class an...
Structure dosen't support the polymorphism inheritance and initilization.



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



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



Structuer is a collection of the different data type.

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
February 05, 2007 12:04:22   #4  
Gagan        

RE: What is the difference between class an...
There should be no confusions. The only difference between a structure and a class is that all members in a class are private by default whereas they are public in a structure.
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
July 16, 2007 08:58:23   #5  
Arun        

RE: What is the difference between class an...
Try inheritance polymorphism overloading encapsulation etc.
all these are possible.........

1. struct data is public by default.. but its private in a class
2. class is successor of struct in heirarchy..
3. struct is ovrloaded.. in C++ its not same with class..

 
Is this answer useful? Yes | No
October 22, 2007 14:46:43   #6  
Sanjay Kapoor        

RE: What is the difference between class an...
Originally posted by rajalekshmy+r
1)can we provide datahiding with structures? if yes how?
2)can structures suport run time polymorphism? If yes how? By writing code? Again whatever you can do in a class you can do in a struct. Why not write code and see for yourself?

#include <iostream>
using namespace std;

struct Whatever
{
virtual void foo() { }
virtual ~Whatever() { }
};

struct Derived : Whatever
{
void foo() { cout << "value " << value; }
Derived(int n) : Whatever() value(n) { }

private:
int value; // private data member
};

int main()
{
Whatever *pW new Derived(10);
pW->foo(); // virtual call
delete pW;
return 0;
}

Convinced now?

Regards

Paul McKenzie

 
Is this answer useful? Yes | No
September 04, 2008 04:41:35   #7  
sumitv Member Since: September 2008   Contribution: 1    

RE: What is the difference between class and structure?
Structure does support inheritance.

try out the following code.

#include<iostream>

using namespace std;

struct Base
{
int A;
};

struct Derived:public Base
{
int B;
void display();
};

void Derived::display()
{
cout<<endl<<"A "<<A<<endl;
}

int main()
{
Derived D;
D.A 111;
D.display();
getchar();
return 0;
}

Try out private and protected inheritance as well. It works. :)

Regards
Sumit

 
Is this answer useful? Yes | No
September 13, 2008 10:59:37   #8  
susantaown Member Since: September 2008   Contribution: 1    

RE: What is the difference between class and structure?
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct a
{
protected:
int x;
public:
a(int y)
{
x y;
}
void showdata()
{
cout<<"x "<<x;
}
};
void main()
{
a aa(2);
aa.showdata();
getch();
}

structure support inheritance in cpp

 
Is this answer useful? Yes | No
October 10, 2008 11:08:37   #9  
anubhava_b Member Since: October 2008   Contribution: 1    

RE: What is the difference between class and structure?
Similar to difference in default access specifier default inheritance too is different in Class Vs Structures.
 
Is this answer useful? Yes | No
January 30, 2009 00:23:01   #10  
Vins99 Member Since: January 2009   Contribution: 1    

RE: What is the difference between class and structure?
A structure and a Class in C++ are same in every aspect expect in structure by default data members and member functions are public and private in case of class.
 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape