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
What does return 0 do in c++?
What is the difference between multiple and multilevel inheritance in c++?
How do you add an element to a set in c++?
What is the difference between structures and unions?
What does it mean to declare a member variable as static?
What is a volatile variable in c++?
What is a stack c++?
Why is c++ not purely object oriented?
What is the difference between while and do while loop?
What is c++ runtime?
What is the difference between while and do while loop? Explain with examples.
Is dev c++ free?
How to tokenize a string in c++?
Distinguish between new and malloc and delete and free().
what is C++ objects?