Lecture 3: Introduction to Classes in C++ Flashcards

1
Q

Concept of classes in OOP

A
• 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the principle of abstraction and how does it relate to OOP?

A

• 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the advantages of abstraction?

A
Any details can be picked 
Provides a skeleton for the class of objects
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Access specifiers

A

a keyword that determines what kind of access is given to the main program for each class member, variable or function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Private

A

Only within the functions of the same class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Protected

A

accessible to functions of the same and derived classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Public

A

anywhere where the object is accessible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Difference between structures and classes

A

Default access specification:
Private for classes
Public for structures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Member functions

A

perform actions on the data members of the class, and are part of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Public member functions

A

provide an interface with the private members of the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How is the concept of data encapsulation used in classes?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

4 common types of member functions

A

setters
getters
constructors
destructors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is inlining?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Inlining may not always be performed. Explain why.

A

○ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Advantages of inlining

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Disadvantages of Inlining

A

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

17
Q

Constructor

A

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

18
Q

If more than one object is created, in what order are the respective constructors called?

A

In the order the objects are declared

19
Q

3 types of constructors

A

Default
Parameterized
Copy

20
Q

Default constrcutor

A

Created automatically if we don’t make one (default or otherwise)
Used to assign default values or dynamically make memory

21
Q

Parameterized constructor

A

Has some arguments

Used to assign values to data members

22
Q

Destructor

A
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
23
Q

If there is more than object, in what order are the destructors called?

A

Reverse order of declaration