Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 300cbb0

Browse files
difference between structures and classes in C++
1 parent 48212ad commit 300cbb0

File tree

1 file changed

+43
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 11 OOP/Difference_between_structures_and_classes_in_cpp

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
struct Demo1
6+
{
7+
int age;
8+
int color;
9+
10+
/* Unlike C language where structure could only host data members in C++ , structures can only have functions*/
11+
void Display()
12+
{
13+
cout<<"age "<<age<<" color "<<color<<endl;
14+
}
15+
16+
/* an important difference aspect, in a structure of C++ all access modifiers are PUBLIC */
17+
};
18+
19+
20+
class Demo2
21+
{
22+
int age;
23+
int color;
24+
25+
void Display()
26+
{
27+
cout<<"age "<<age<<" color "<<color<<endl;
28+
}
29+
30+
/* an important difference aspect, in a class of C++ all access modifiers are by default PRIVATE */
31+
};
32+
33+
34+
int main(void)
35+
{
36+
Demo1 d1;
37+
d1.Display();
38+
39+
Demo2 d2;
40+
// d2.Display() // ->does not work as the function is private
41+
42+
return 0;
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /