Questions and CORRECT Answers
similarity between procedural and object-oriented programming - CORRECT
ANSWER There is a very similar control flow between procedure and object-oriented
programming. Very similar call-and-return mechanisms, in one case using procedures and in the
other using methods on classes and objects.
difference between procedural and object-oriented programming - CORRECT
ANSWER The key distinction is that procedural languages organize by procedures with
data and functionality separate (though sometimes loosely collected into modules), whereas
object-oriented languages organize by classes and objects, with data and functionality effectively
bundled together
Benefits of object-oriented programming: Modularization - CORRECT ANSWER ->
Refers to the design approach where a software application is implemented as a collection of
independent units (subsystems) that communicate by exchanging only the absolutely necessary
information (data) that is required to perform an operation
-> In other words, they have low coupling
-> Furthermore, each unit (subsystem) performs a specific operation or a collection of very
closely related operations
-> In other words, they have high cohesion
-> Usually, a collection of related classes constitutes a subsystem
Object orientation is not the solution to every programming need - CORRECT
ANSWER Data base applications -> SQL, 4GLs
Embedded systems -> Assembly, .....
Why C++? - CORRECT ANSWER -> C++ is the fourth most popular programming
language according to the latest TIOBE index for 2019, behind Java, C, and Python
-> It is often praised for generally doing a good job of merging the benefits of object-orientation
with the power and speed of C
,-> Its template library provides a decent collection of starting points, and add-on packages like
Boost take things even further
-> Mastering C++ will also make you a better programmer at C, Java, C# and many other
languages - approaching things the other way is significantly more challenging
Why not C++? - CORRECT ANSWER -> C++ lacks garbage collection and forces users
to manage memory on their own (though some people see this as a plus)
-> C++ lacks some high level language features like reflection (Reflection: program asking
questions (eg. Checking objects type))
-> Templates, while beneficial, are still harder to use and more error prone than they should be in
practice
-> Some recent developments in the language are perceived as feature creep and unnecessary
Entire programs in C++ can be written using only regular functions not defined in any class -
CORRECT ANSWER ; in other words, classes are not mandatory
explain std::cout - CORRECT ANSWER Std:: reffering to namespace which is collection
of methods and objects. Std::cout Want to use cout stream of the std space
Instead of using std::endl you can use - CORRECT ANSWER \n
using namespace std; - CORRECT ANSWER Allows you to use cout and end1 without
std::
5 steps to building c++ program - CORRECT ANSWER multiple source files, C++
compiler (g++), Multiple object files, Linker (Id), executable
how to build in one step in command line - CORRECT ANSWER > g++ HelloWorld.cpp
-o HelloWorld
> ./HelloWorld
,How to build in multiple steps in command line - CORRECT ANSWER > g++ -c
HelloWorld.cpp
> g++ HelloWorld.o -o HelloWorld
> ./HelloWorld
#define constants - CORRECT ANSWER preprocessor definitions that do a simple
replacement during preprocessing and before compiling
ex. #define PI 3.14
const constants - CORRECT ANSWER named constant declarations where the
programmer commits to not changing a value, and this promise is enforced by the compiler
ex. const double pi = 3.14;
constexpr constants - CORRECT ANSWER constant expressions evaluated at compile
time, allowing them to be placed in read-only memory or to improve performance (new in
C++11) ex. constexpr double radius1 = 5.0;
(possible to calculate the value at compile time lets say there is function to add 2 numbers, we
can calculate that at compile time because it is not like the input numbers we are adding are from
user input)
Function in C++ have thing diff from functions in C - CORRECT ANSWER Optionally,
one or more modifiers designating special behaviour of the function or modifying how the
compiler treats or compiles it (this is where things can get ugly and deviate from C ...)
Modifer: how function is called, how its executed, allows you to have optimization.
Dont need to name parameters - CORRECT ANSWER when declaring a function but will
name when defined void swap (int *, int *);
Function Definition - CORRECT ANSWER A function definition is a function declaration
alongside a presentation of the body of the function
, Function Parameter:
What about our swap function though?
Recall that it exchanges the values in the two variables pointed to by its parameters!
Does this not break the pass by value rules? - CORRECT ANSWER Passed pointers to
stack frame but they pointed to the heap and manipulated the variables which is why it is fine.
C++ also allows parameters to be passed by reference by designating them with an - CORRECT
ANSWER & in the function declaration. The variable in the function definition is not a
local variable in this case, but rather an alias to the variable outside of the function and passed
into it as a parameter
As a result, changes in value within the function propagate outside the function as well
Only use pass by reference when this behaviour is necessary, otherwise things can get rather
confusing!
const - CORRECT ANSWER is a commitment to not modify something and so the
compiler will treat it as a constant and not let us modify it
3 ways function can exit - CORRECT ANSWER -> It executes a return statement,
providing a return value of the appropriate type (or not providing anything in the case of a void
function)
-> Reaching the end of the function body, which is only allowed in void functions or main(), in
which case this indicates successful completion
-> Calling a system function that does not return (like exit())
Function Modifiers: inline - CORRECT ANSWER The compiler should try to embed the
code for the function where it is called from, typically for performance reasons. copy and paste
the code every spot that is is called so you have the modularity of the function but have
performance of it being in the spot. Use inline for functions that are small and called often.
Function Modifiers: constexpr - CORRECT ANSWER The compiler should evaluate the
results of calling the function at compile time, making this usable with constexpr constants.