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
Disadvantages of Inlining
Too many inlined functions increase the size of the binary executable (The same code is copied)
Increases compile time overhead for larger functions (functions more than a line)
All calling locations have to be recompiled if the code in the definition is changed
Takes a lot of memory
In many systems, the size of the code is more important than speed
Constructor
a function to ensure that the object is in a well-defined state at the time of creation
Automatically called when a new object is created - cannot be explicitly called
If more than one object is created, in what order are the respective constructors called?
In the order the objects are declared
3 types of constructors
Default
Parameterized
Copy
Default constrcutor
Created automatically if we don’t make one (default or otherwise)
Used to assign default values or dynamically make memory
Parameterized constructor
Has some arguments
Used to assign values to data members
Destructor
Called when object passes out of scope or is explicitly deleted Used to de-allocate and free memory No arguments Cannot be overloaded Default created if we don't provide one
If there is more than object, in what order are the destructors called?
Reverse order of declaration