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

Why do we use double in c++?

603


Why null pointer is used?

587


What is an undefined reference/unresolved external symbol error and how do I fix it?

610


What is a namespace in c++?

604


What is ios in c++?

653






What are the advantages of c++ over c?

588


what kind of projects are suitable for c and c++

631


Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

653


What is the difference between struct and class?

751


What are the effects after calling the delete this operator ?

563


Out of fgets() and gets() which function is safe to use and why?

731


Differentiate between an external iterator and an internal iterator?

578


Define token in c++.

721


What is data types c++?

548


What is the type of 'this' pointer?

607