Introduction to Structures and Classes
Putta Hemanth Kumar
Software Developer @ Samsung Research Institute || Code and Content || IIEST || DSA || C++ || System Design || 1.5K LinkedIn
Ram, now has a good clarity on Primitive Datatypes, now he’s learning User defined datatypes. While doing so he encountered Structures and Classes. Ram has no clue why does anyone have to define their own datatype when they have different primitive datatypes which are already defined.
Again he started to watch reels until he remembered that the next day is his little sister’s birthday. He immediately got off the study table and started to write on paper all the different food and things his sister is found of. He has filled the paper with different Chocolates and sweets.
He immediately bought everything he need for surprise, But he don’t want to give these all just like that. It will be a surprise if she sees all her favourite things at a time. Now he has started to prepare a big box with cardboard sheets and made sections in the box to fit all the things he bought
He finally prepared a custom box, filled it with all the surprise gifts and chocolates and some cash in a different slot in box and wrapped it tight as a beautiful present.
Next day after all the arrangements for the birthday celebrations has begun, he presented the box he prepared for his sister.
Sister has opened the box and got surprised to see all her favourite things in one single box and was very happy with the present and the efforts made by her brother to prepare this present with his own hands.
Ram then understood that no one else know what can be a custom present to his sister other than him, so he prepared a custom gift of all her likings. And he thought he can just use this box idea with different things on next birthday.
Later that night he understood that, yes we indeed need a custom datatype to hold all our required variables regarding a particular thing like height, weight and grade of a student in a custom student structure and then declare any number of students with this custom datatype and every student has build in variables of height, weight and grade.
领英推荐
Structure Syntax:
Now he understood everything was fine with the Giftbox but he has a bit of discomfort that everyone at the party has witnessed the money he gifted in the Gift box, and he thought to upgrade the cardboard box to a new level where some slots are just known to his sister so that she can only view that slots when she’s in private place and grab the cash and personal photos rather then displaying it to everyone while opening the box.
Now in the same box he inserted some secret chambers and gifted it. Now Ram is super happy that some sections in his custom box are private and some are public.
Ram got to understand this as Class, where some variables are publicly declared, some are private and some are protected. All these need to be accessed by creating an instance of this class.
Class Syntax:
Read about classes and structures more in here: