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
When do we use copy constructors?
How long it will take to learn c++?
What would happen on forgetting [], while deallocating an array through new?
What is a friend function in c++?
What are c++ redistributables?
What is the copy-and-swap idiom?
What is a namespace in c++?
Why do we need runtime polymorphism in c++?
Explain virtual class?
What is the default width for ouputting a long integer using the insertion operator?
What are the various situations where a copy constructor is invoked?
What is array in c++ example?
Explain storage qualifiers in c++.
How does c++ structure differ from c++ class?
Write a program which is required to process the time of a clock in hours and minutes, entered from the keyboard. With this program, there are two requirements for any data entered by a user: 1. The data must be of the correct type (in this case, two ints). 2. The data must be in the correct range: this means that, for the minutes, negative numbers and any number above 59 must be rejected; for the hours, negative numbers and any number above 23 must be rejected. Output error message for invalid data input. Output the time one and a half hour after the time input. i.e. Hour: 22 Min: 32 One and a half hour after 22:32 is 00:02