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

Is rust better than c++?

588


What does the linker do?

578


What would happen on forgetting [], while deallocating an array through new?

621


Do you know about latest advancements in C++ ?

646


What causes a runtime error c++?

566






What are put and get pointers?

568


What is the type of this pointer in c++?

616


What is srand c++?

561


Discuss the effects occur, after an exception thrown by a member function is unspecified by an exception specification?

623


Is ca high or low level language?

569


Show the declaration for a static member variable.

514


What are the various arithmetic operators in c++?

564


When does a 'this' pointer get created?

605


What do you mean by vtable and vptr in c++?

611


What's the order in which the local objects are destructed?

822