difference between classes and structure in c++.
Answers
Answered by
4
To understand the main difference between structure and class :-
The C++ class is an extension of the C language structure. Because the only difference between a structure and a class is that structure members have public access by default and class members have private access by default, you can use the keywords class or struct to define equivalent classes.
For example, in the following code fragment, the class X is equivalent to the structure Y:
class X {
// private by default
int a;
public:
// public member function
int f() { return a = 5; };
};
struct Y {
// public by default
int f() { return a = 5; };
private:
// private data member
int a;
};
Attachments:
Similar questions