CompTIA Tech+ (FC0-U71)Software Development ConceptsMedium
A developer is designing a program that needs to represent a 'Car' with properties like 'make', 'model', 'year', and 'color', and behaviors such as 'startEngine()' and 'drive()'. Which programming construct is specifically designed to group these related data (properties) and functions (behaviors) into a single blueprint?
- AArray
- BVariable
- CClass
- DFunction
Show answer & explanationAnswer & explanation
Correct answer: C. Class
A Class is a blueprint or a template for creating objects. It defines the properties (attributes/data) and behaviors (methods/functions) that all objects of that type will have. This perfectly matches the requirement to group 'make', 'model', 'year', 'color' with 'startEngine()' and 'drive()' for a 'Car'.
Why the other options are wrong
- A. An Array is a data structure for storing collections of similar items, not for defining complex entities with properties and behaviors.
- B. A Variable is a storage location identified by a memory address, holding a single piece of data.
- D. A Function is a block of code designed to perform a specific task, but doesn't group related data with it.
Class (OOP)
In object-oriented programming, a class is a blueprint or a template for creating objects. It defines the common properties (attributes) and behaviors (methods) that all objects of that type will possess.
- Acts as a template from which objects are created.
- Encapsulates data (properties) and methods (behaviors).
- Does not consume memory until an object (instance) is created from it.
Memory trick: A Class is like a cookie cutter, making many identical cookies (objects).