Programming Using Structures and Classes
Words: 550
Pages: 2
158
158
DownloadProgramming Using Structures and Classes
Firstname Lastname
University Name
Programming Using Structures and Classes
The report answers a few questions related to programming using structures and classes.
Under What Circumstances Must You Use Classes?
Classes can be used in systems where there is a need to represent real world objects that have certain features and perform a certain behavior. For instance, a class student with name, age, gender, id, etc. and the student can enroll to a course, take an exam, mark attendance, etc. Classes can be used in designing real world systems inheritance that defaults to private access need to be defined between real world objects or the data needs to be encapsulated i.e. hidden from a user. In classes, as the object’s features (data members) are private by default, they can only be altered through the interfaces called member functions.
Why do you use a class instead of a structure? Are they interchangeable? Explain your answer.
Structures and classes are essentially the same and can be used interchangeably. Both provide the features of data encapsulation and inheritance. In case of structures, the ‘private’ access specifier is exclusively mentioned, while in the case of classes, the ‘public’ access specifier is mentioned. In both the cases, functions are used to access private data.
As a general C++ programming convention, structures are used by programmers when representing plain old data with backward compatibility. Classes are used instead of structures in cases where backward compatibility is not required.
Wait! Programming Using Structures and Classes paper is just an example!
Secondly, in real world systems, when we need the access specifiers and inheritance to default to private, we generally use classes. For instance, when creating a library system, we would want to keep the data on books private, so the users without privileges cannot alter a book’s details such as title, author, isbn, etc. In case the information on the book needs to be edited, the authorized users can use the specific interfaces i.e. the member functions, unknown to the general public, e.g. editTitle(), to access the private data and update it.
Are classes more useful than structures?
Generally, when writing real world programs, we protect and hide the data from the users to ensure confidentiality and integrity. We then define interfaces to access the private data and these interfaces are accessible to only the authorized users. So, for the real world systems where the data must be protected and should have controlled access, classes are the most appropriate selection as with classes, unlike structures, the default access specifiers are private. So following the programming convention, whenever we need to create a system of objects where data is private, we use classes. Furthermore, the object oriented paradigm (C++, Java, C#, etc.) is based on only classes. So classes are more useful as they can be related to more, when designing real world object oriented systems.
Subscribe and get the full version of the document name
Use our writing tools and essay examples to get your paper started AND finished.