Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer...
Transcript of Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer...
![Page 2: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/2.jpg)
Today’s Plan
Announcements
Recap
Abstraction
OOP
!2
![Page 3: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/3.jpg)
Announcements
- If you already have a Gradescope account from previous semesters you still need to make sure you have access to this course
- Compile and run (remotely) hello_world.cpp on the Linux machines in the lab. If you can’t do it, please go to 1001B and ask for help from a UTA
- Project 1 posted today!
!3
![Page 4: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/4.jpg)
Recap
Minimize software size and interactions Simplify complex program to manageable level
Break down into smaller problems
Isolate functionalities
Minimize and control interactions
So how do we do this?
!4
![Page 5: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/5.jpg)
Abstraction
!5
![Page 6: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/6.jpg)
Abstraction Example
!6
![Page 7: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/7.jpg)
Abstraction Example
You always use them, switch from one to another seamlessly and probably don’t think too much about them
!7
![Page 8: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/8.jpg)
Printers
Come in all shapes and sizes
Can have different complex mechanisms (Laser, Laserjet, Inkjet, Dot matrix … )
Easy to use - something common to all of them - abstraction
!8
![Page 9: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/9.jpg)
What is a printer?
!9
![Page 10: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/10.jpg)
What is a printer?
A printer reproduces graphics or text on paper
!10
![Page 11: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/11.jpg)
What is a printer?
A printer reproduces graphics or text on paper
Separate functionality from implementation (i.e. what can be done from how it’s actually done)
!11
![Page 12: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/12.jpg)
Wall of Abstraction
Painstaking work to design technology and implement printers
Press button Or Send print job from application
Information barrier between device (program) use and how it works
Design and implementation
Usage
!12
![Page 13: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/13.jpg)
Abstractions are imprecise
A printer reproduces graphics or text on paper
Wall of abstraction between implementer and client
How does client know how to use it?
!13
![Page 14: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/14.jpg)
Abstractions are imprecise
A printer reproduces graphics or text on paper
Wall of abstraction between implementer and client
How does client know how to use it?
Provide an interface (what the user needs to interact) In Software Engineering typically a set of attributes (data or properties) and a set of actions
!14
![Page 15: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/15.jpg)
Lecture Activity
Attributes (data):
Actions (operations):
Designing the interface: think about what the user needs
to do / know about
!15
![Page 16: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/16.jpg)
Interface for Printer
Attributes (data): Ink level Paper level Error codes
Actions (operations): Print Rotate (landscape/portrait) Color / Black & White
How this is done
is irrelevant to the client
!16
![Page 17: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/17.jpg)
Information Hiding
Interface —> client doesn’t have to know about the inner workings
Actually client shouldn’t know of or have access to implementation details
It is dangerous to allow clients to bypass interface
In this course
it always means software
Safe Programming
!17
![Page 18: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/18.jpg)
Reasons for Information Hiding
Harmful for client to tamper with someone else’s implementation (code)
- Voluntarily/involuntarily break it - misuse it
- Reduces flexibility and modifiability by locking implementation in place
- Increases number of interactions between modules
!18
![Page 19: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/19.jpg)
Object Oriented Design
!19
![Page 20: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/20.jpg)
Principles of Object Oriented Programming (OOP)
Encapsulation Objects combine data and operations
Information Hiding Objects hide inner details
Inheritance Objects inherit properties from other objects
Polymorphism Objects determine appropriate operations at execution
!20
![Page 21: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/21.jpg)
Principles of Object Oriented Programming (OOP)
Encapsulation Objects combine data and operations
Information Hiding Objects hide inner details
Inheritance Objects inherit properties from other objects
Polymorphism Objects determine appropriate operations at execution
Coming soon
!21
![Page 22: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/22.jpg)
Object-Oriented Solution
Use classes of objects Combine attributes and actions data members + member functions
Create a good set of modules Self contained unit of code
!22
![Page 23: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/23.jpg)
Encapsulation
!23
![Page 24: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/24.jpg)
!24
![Page 25: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/25.jpg)
Class
class SomeClass {
access_specifier // can be private, public or protected
data_members // variables used in class
member_functions // methods to access data members
}; // end SomeClass
!25
![Page 26: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/26.jpg)
Class
Language mechanism for
Encoding abstraction
Enforce encapsulation
Separate interface from implementation
A user-defied data type that bundles together data and operations on the data
!26
You have already been working with classes.
Which ones?
![Page 27: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/27.jpg)
Information Hiding
!27
![Page 28: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/28.jpg)
Class
class SomeClass {
public: // public data members and member functions go here
private: // private data members and member functions go here
}; // end SomeClass
Access specifier
Access specifier
Information Hiding
!28
![Page 29: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/29.jpg)
!29
![Page 30: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/30.jpg)
!30
Your program:
std::string s = “aa”;std::string s2 = “bb”;
std::string
s.append(s2);
“aabb”
![Page 31: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/31.jpg)
#ifndef SOME_CLASS_HPP_ #define SOME_CLASS_HPP_ #include <somelibrary> #include “AnotherClass.hpp” class SomeClass { public:
SomeClass(); //Constructor int methodOne(); bool methodTwo(); bool methodThree(int
someParameter);
private:
int data_member_one_; bool data_member_two_;
}; //end SomeClass #endif
#include “SomeClass.hpp” SomeClass::SomeClass() {
//implementation here }
int SomeClass::methodOne() {
//implementation here }
bool SomeClass::methodTwo() {
//implementation here }
bool SomeClass::methodThree(int
someParameter) {
//implementation here }
SomeClass.hpp (same as SomeClass.h)
SomeClass.cpp
Interface Implementation
!31
![Page 32: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/32.jpg)
Include Guards: Tells linker “include only if it has not been included already by some other module”
#ifndef SOME_CLASS_HPP_ #define SOME_CLASS_HPP_ #include <somelibrary> #include “AnotherClass.hpp” class SomeClass { public:
SomeClass(); //Constructor int methodOne(); bool methodTwo(); bool methodThree(int
someParameter);
private:
int data_member_one_; bool data_member_two_;
}; //end SomeClass #endif
#include “SomeClass.hpp” SomeClass::SomeClass() {
//implementation here }
int SomeClass::methodOne() {
//implementation here }
bool SomeClass::methodTwo() {
//implementation here }
bool SomeClass::methodThree(int
someParameter) {
//implementation here }
SomeClass.hpp (same as SomeClass.h) SomeClass.cpp
Interface Implementation
!32
![Page 33: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/33.jpg)
Separate CompilationInclude A.hpp Include B.hpp Include C.hpp main
!33
g++ -o my_program A.cpp B.cpp C.cpp main.cpp
Name of executable
Both Compile and Link
A.o B.o C.o main.o
![Page 34: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/34.jpg)
Compile and Link separately with g++
g++ -c A.cpp B.cpp C.cpp main.cpp will generate
A.o B.o C.o main.o
Then
g++ -o my_program A.o B.o C.o main.o
Will link the object files into a single executable named my_program
!34
![Page 35: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/35.jpg)
Class RecapAccess specifiers: determines what data or methods are public, private or protected (more on protected later)
Data members: the attributes/data
Member functions: the operations/actions available on the data - Mutator functions: modify data members
- Accessor functions: retrieve the value of data members Use const to enforce/indicate it will not modify the object e.g. string getName() const;
- Constructor(s)
- Destructor
Take care of what happens when object goes in/out of scope
!35
![Page 36: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/36.jpg)
Class / Object
A class is a user-defied data type that bundles together data and operations on the data
Class: type (like int)
Object: instantiation of the class (like x - as in int x)
Just like variables, objects have a scope - they are born (instantiated/constructed) - they are killed (deallocated/destroyed)
!36
![Page 37: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/37.jpg)
Object instantiation and usage
#include “SomeClass.h” int main() {
SomeClass new_object; /instantiation of SomeClass calls constructor
int my_int_variable = new_object.methodOne();bool my_bool_variable = new_object.methodTwo();
return 0;
} //end main
object (dot) methodcalls the member function for this object
!37
Constructor is called here
![Page 38: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/38.jpg)
Constructorsclass SomeClass {
public: SomeClass(); //default constructor SomeClass( parameter_list ); //parameterized constructor // public data members and member functions go here
private: // private members go here
};// end SomeClass
DECLARATION / INTERFACE:
Default Constructor automatically supplied by compiler if no constructors are provided. Primitive types are initialized to 0
If only Parameterized Constructor is provided, compiler WILL NOT supply a Default Constructor and class MUST be initialized with parameters
!38
Executed when object is declared. Initializes member variables and does whatever
else may be required at instantiation
![Page 39: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/39.jpg)
Constructorsclass SomeClass {
public: SomeClass(); //default constructor SomeClass( parameter_list ); //parameterized constructor // public data members and member functions go here
private: // private members go here
};// end SomeClass
IMPLEMENTATION:
SomeClass::SomeClass() { }// end default constructor
OR: SomeClass::SomeClass(): member_var1_(initial value), member_var2_(initial value) { }// end default constructor
SomeClass::SomeClass(type parameter_1, type parameter_2): member_var1(parameter_1), member_var2(parameter_2) { }//end parameterized constructor Member Initializer List!39
DECLARATION / INTERFACE:
![Page 40: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/40.jpg)
Constructorsclass SomeClass {
public: SomeClass() = default; //default constructor SomeClass( parameter_list ); //parameterized constructor // public data members and member functions go here
private: // private members go here
};// end SomeClass
IMPLEMENTATION:
SomeClass::SomeClass(type parameter_1, type parameter_2): member_var1(parameter_1), member_var2(parameter_2) { }//end parameterized constructor !40
DECLARATION / INTERFACE:
C++ 11
Tells compiler to provide default constructor!
![Page 41: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/41.jpg)
Destructor
class SomeClass {
public: SomeClass(); SomeClass( parameter_list );//parameterized constructor // public data members and member functions go here~SomeClass(); // destructor
private: // private data members and member functions go here
};// end SomeClass
Default Destructors automatically supplied by compiler if not provided.
Must provide Destructor to free-up memory when SomeClass performs dynamic memory allocation
!41
Executed when object goes out of scope or explicitly
deleted to release memory
![Page 42: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/42.jpg)
Lecture Activity
Write the interface for a printer class:
class Printer {
access_specifier // can be private, public or protected
data_members // variables used in class
member_functions // methods to access data members
}; // end Printer
!42
![Page 43: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/43.jpg)
Interface as Operation Contract
Documents use and limitations of a class and its methods
Function Prototype and Comments MUST specify: - Data flow Input => parameters Output => return - Pre and Post Conditions
!43
![Page 44: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/44.jpg)
Operation Contract
In Header file:
/** sorts an array into ascending order // @pre 1 <= number_of_elements <= MAX_ARRAY_SIZE // @post an_array[0] <= an_array[1] <= ... // <= an_array[number_of_elements-1]; // number_of_elements is unchanged // @param an_array of values to be sorted // @param number_of_elements contained in an_array // @return true if an_array is sorted, false otherwise */ bool sort(const int& an_array[], int number_of_elements);
!44
Function prototype
![Page 45: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/45.jpg)
Back to some principles of Software Engineering
!45
![Page 46: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/46.jpg)
Unusual Conditions
Values out of bound, null pointer, inexistent file…
How to address them (strive for fail-safe programming): State it as precondition Return value that signals a problem Typically a boolean to indicate success or failure Throw an exception (later in semester)
!46
![Page 47: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/47.jpg)
Solution guidelines
Many possible designs/solutions
Often no clear best solution
“Better” solution principles: High cohesion Loose Coupling
!47
![Page 48: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/48.jpg)
Cohesion
Performs one well-defined task
Well named => self documenting e.g. sort()
Easy to reuse Easy to maintainRobust (less likely to be affected by change)
SORT ONLY!!! E.g. If you want to output, do that in another function
!48
![Page 49: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/49.jpg)
Coupling
Measure of dependence (interactions) among modules i.e. share data structures or call each other’s methods
Minimize but cannot eliminate Objects must collaborate!!! Minimize complexity
!49
![Page 50: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/50.jpg)
Reduce Coupling
Methods should only call other methods:
- defined within same class
- of argument objects
- of objects created within the method
- of objects that are data members of the class
!50
![Page 51: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/51.jpg)
Control Interaction
Pass-by-value bool my_method(int some_int);
Pass-by-reference if need to modify object bool my_method(ObjectType& some_object);
Pass-by-constant-reference if function doesn’t modify object bool my_method(const ObjectType& some_object);
!51
![Page 52: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/52.jpg)
Modifiability
No global variables EVER!!!
Named Constants const int NUMBER_OF_MAJORS = 160;
int scores [NUMBER_OF_MAJORS]; for(index = 0 through NUMBER_OF_MAJORS - 1) Process
!52
![Page 53: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/53.jpg)
Modifiability
!53
![Page 54: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/54.jpg)
ReadabilityWrite self-commenting code
Important to strike balance btw readable code and comments
- don’t write the obvious in comments x += m * v1; //multiply m by v1 and add result to x
Use descriptive names for variables and methods
!54
BAD!!!
/**@return: the average of values in scores*/double getAverage(double* scores, int size){
double total = 0;
for (int i = 0; i < size; i++){
total += scores[i];}
return ( total / (double)size );}
![Page 55: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/55.jpg)
Naming Conventions
string my_variable;
or
string myVariable;
Classes ALWAYS start with capital MyClass
!55
In this course I will strive for: class MyClass
MyClass class_instance;
string my_variable;
string my_member_variable_;
void myMethod();
int MY_CONSTANT;
https://google.github.io/styleguide/cppguide.html
http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rl-comments
Be consistent!!!
![Page 56: Abstraction and OOP · on the Linux machines in the lab. If you can’t do it, ... A printer reproduces graphics or text on paper Wall of abstraction between implementer and client](https://reader033.fdocuments.net/reader033/viewer/2022042011/5e72692cecf3091edb3f4f77/html5/thumbnails/56.jpg)
Project 1
Animal class (baseline): - implement a trivial class - compile multi-file program with g++ (Animal.cpp, main.cpp) - submit to Gradescope
Projects 1-4 build on each other, do not fall behind!
!56