What is the difference between Class and Structure?

Answer Posted / aashu gupta

There are only and only 2 differences between structure and class :
1. By default members are public in structures and private in class
2. Default inheritance in structure is public and private in class

you can verify the above differences by executing following code:
#include<iostream>

//code compiles and executes correctly means that //inheritance and polymorphism is allowed in structu
//res

using namespace std;

struct Base
{
int A;
virtual void display()=0; //polymorphism is allowed in structure
};

struct Derived:Base //public in struct and private in class
{
int B;
void display();
};

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

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

////////////////////////////////////////////////

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between multiple and multilevel inheritance in c++?

601


Define pre-condition and post-condition to a member function in c++?

658


Where Malloc(), Calloc(), and realloc() does get memory?

607


True or false, if you keep incrementing a variable, it will become negative a) True b) False c) It depends

1855


How to demonstrate the use of a variable?

642






What is different in C++, compare with unix?

605


What does n mean in c++?

630


Explain how would you handle a situation where you cannot call the destructor of a local explicitly?

535


an integer constant must have atleast one a) character b) digit c) decimal point

554


What is a try block?

634


Differentiate between C and C++.

708


Do inline functions improve performance?

642


What is the difference between mutex and binary semaphore?

610


We all know that a const variable needs to be initialized at the time of declaration. Then how come the program given below runs properly even when we have not initialized p?

576


How do you add an element to a set in c++?

549