Differences between Structures in C and C ++
Differences between Structures in C and C ++
Let's understand this through an example:--
Structure in C language
![]() |
structure in C |
Here in above example ,we saw that how Structure define and call in C Language.
Structure in C++ language
![]() |
structure in C++ |
Here , we seen that differences in structure define and calling C++ i.e
1- we can not only initialize or call as member variable in structure body( int bookid,char title,float price)but function are also initialize or call as member function in structure body(void input(),void
display()).
2- In above example ,we can seen that input() function access directly bookid,title and price
without any b1.bookid,b1.title,b1.price written as we seen in C language Structure and
at the time of input() function calling by main () function it's directly call as b1.input() and
we can say that input function belong to b1 object.if we take another object like b2 and b2 call input() function then we can say input() function belong to b2 as well .
3- we can secure and safe our data from corrupted in C++ Language.
How can we secure and protected our data from corrupted in C++ ?
C++ Language provides three access specifiers: Private , Public and Protected
![]() |
access specifiers in C++ |
In above example , we can seen that Private member i.e int bookid, char title , float price and rest of
member is Public member therefore we can access only public member outside of structure body i.e
higlighted portion i.e b1.bookid=-100 is not accessible to check this lets compile and check error in
below example--
Here is the error that 'book::bookid is not accessible' it's means bookid is private member and remember one that we use "if (bookid<0){bookid= -bookid;}" it's mean we want bookid number is
always positve whether user enter a negative number..
If this article is helpful to you then please like and share so that other will get benefits from it
or if you have any query then please comments.. thank you..
Comments
Post a Comment