.111 10-15 Flashcards
What is the pre-processor useful for?
defining constants (#define)
defining macros (#define)
including files (#include)
conditional compilation (#if/#ifndef)
How does the macro #define MIN(X,Y) (X) < (Y) ? (X) : (Y) work?
means where you see MIN(int,int) you substitute it with the comparison which will return whichever integer is smaller
how is conditional compilation helpful?
used to compile for different types of processor
can use #ifndef to protect header files from multiple inclusion errors
How do you do a binary shift in C?
«_space;= left shift
» = right shift
How does compilation work for a C file?
e.g. gcc -o main main.c
preprocessor (takes the source - main.c) -> C compiler (main.o) -> linker (main aka exectuable)
What are the 4 language types?
- imperative/procedural
- object oriented
- declarative
- (pure) functional
What are functional languages?
declarative languages w/ no explicit state
e.g. Haskell + Erlang
What are declarative languages?
describe the logic of the program w/ minimal explicit control of the flow of execution
e.g. SQL, HTML, Regex
What are object oriented languages?
imperative w/ implicit encapsulation of data + functions
e.g. C++, Java, C#, JavaScript + Python
How does procedural programming treat code + data?
treat as separate concepts
data modularity only occurs in structs
What is modularity?
it allows us to divide code to inc. reliability (reducing interaction between code) and make code more scalable!
it relies on standards (an API)
How does OOP treat code + data?
combines them into objects (methods + attributes)
What does it mean that objects provide encapsulation?
- data + code cannot be separated
- data is defined through attributes
- behaviour is defined through methods
What is a class?
specification that defines objects
- add attributes by defining variables inside the class
- add methods by declaring functions with ClassName::function
NEED ; after closing } at end of class
How do you create an object?
simply use the class name as a variable type
object = instance of a class
What does modularity rely on?
encapsulation
- module must contain its inner workings, protect them + only expose functionality through its interfaces
What ways can attributes + methods be declared as?
public or private
ideally private attributes + public methods
Why is access control important?
private attributes help keep other programmers directly reading/writing to your attributes
public methods helps promote modularity w/o exposing more functionality than desired
What are methods vital to classes?
accessors (getters)
mutators (setters)
constructors
destructors
What is an accessor?
public method that gets the value of a private attribute
no params but returns value
What is a mutator?
public methods that change the value of a private attribute
take 1 param to set attribute to but void (no return)
What does a constructor do?
public method that is automatically called when you create an object
helps initialise an object
may have multiple but each needs to have unique parameters for identification
What does a destructor do?
method that is invoked automatically whenever an object is to be destroyed
occurs if…
- var relating to that object goes out of scope
- object is explicitly deleted
Benefits of access control + encapsulation:
- programmers can’t create invalid/inconsistent variable values (accidentally or maliciously)
- promotes simpler, safer code
- programmer writing a class can now define + enforce its API