Lecture 3: Introduction to Classes in C++ Flashcards
Concept of classes in OOP
• Classes in OOP: parts of code that form the elements of the programs ○ While the program is executed, instances or "objects" of the class are made which are essentially pieces of software which model and behave like real-life objects § They can interact with one another using functions to solve the given problems ○ A main skeleton or blueprint from which individual objects are created § Contain member variables to represent attributes and member functions to represent behavior and/or state
What is the principle of abstraction and how does it relate to OOP?
• Principle of abstraction: Capture only the details that are relevant to the current perspective
○ All other details are omitted
○ A class can be made on the basis of those details
E.g. a map may need to show roads, but it does not need to show sewage pipes
What are the advantages of abstraction?
Any details can be picked Provides a skeleton for the class of objects
Access specifiers
a keyword that determines what kind of access is given to the main program for each class member, variable or function
Private
Only within the functions of the same class
Protected
accessible to functions of the same and derived classes
Public
anywhere where the object is accessible
Difference between structures and classes
Default access specification:
Private for classes
Public for structures
Member functions
perform actions on the data members of the class, and are part of the class
Public member functions
provide an interface with the private members of the class
How is the concept of data encapsulation used in classes?
Public access is only used for member functions
○ Public access of variables goes against the fundamental OOP concept of data encapsulation - Data must be hidden from direct view
4 common types of member functions
setters
getters
constructors
destructors
What is inlining?
Instead of jumping to the function definition from the point of function call, the compiler places a copy of the inline function at the call point
Inlining may not always be performed. Explain why.
○ A REQUEST, not a command to the compiler
May not perform inlining if the following are present:
□ Loop
□ Static variables
□ Recursive functions
□ Switch/goto statement
Advantages of inlining
Function call overhead doesn’t occur, so saves time, memory and power
More effective for smaller functions
Saves the overhead of push/pop variables (Push: )
Saves overhead of return from a function