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 is the difference between multiple and multilevel inheritance in c++?
Define pre-condition and post-condition to a member function in c++?
Where Malloc(), Calloc(), and realloc() does get memory?
True or false, if you keep incrementing a variable, it will become negative a) True b) False c) It depends
How to demonstrate the use of a variable?
What is different in C++, compare with unix?
What does n mean in c++?
Explain how would you handle a situation where you cannot call the destructor of a local explicitly?
an integer constant must have atleast one a) character b) digit c) decimal point
What is a try block?
Differentiate between C and C++.
Do inline functions improve performance?
What is the difference between mutex and binary semaphore?
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?
How do you add an element to a set in c++?