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

Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

2077


List the special characteristics of constructor.

742


What is lambda expression c++?

584


What is the output of the following program? Why?

632


What does flush do c++?

574






What is wrapper class in c++?

638


We use library functions in the program, in what form they are provided to the program?

611


When do you call copy constructors?

661


If I is an integer variable, which is faster ++i or i++?

601


What is c++ coding?

665


How to defines the function in c++?

630


what are Access specifiers in C++ class? What are the types?

640


What do you mean by a template?

636


What is size_type?

558


Can recursive program be written in C++?

647