1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
How to Write Scalable Code
Rule #2: Don't reinvent the wheel
Integrate high-quality third-party classes into your code
Write reusable code. Assume that others will integrate your classes into their applications
Libraries
Libraries are simply collections of prewritten, precompiled code
Procedural languages provide a sets of functions
Object Oriented languages provide sets of classes
C++ is half and half
Anyone can create a library…
Creating a Library
Creating a C/C++ library is remarkably simple
Designing a good one is quite hard, but more on that later
Create your classes, and C functions as normal
Separate out your function and class declarations into header files
Separate your function/method implementation into .cpp files
Do NOT include a main function
Compile your code with the -c flag
This means "compile only", and does not attempt to create a final, executable program… instead you will get one or more object files. (.o files)
Using a Custom Library
To use a library, we simply add the precompiled .a file when we compile the application (the thing with a main method in it)
You can add as many libraries as you like on the command line
This provides strong decoupling of library code from application code…
Different programmers can develop the different parts at different times
C++ Standard Libraries
C++ provides many libraries as standard, above those in C
<iostream> library that deals with basic input and output
Imported using #include <iostream>
It serves as a modern alternative to the stdio.h library (printf and scanf)
<string> library provides the string type
<regex> <queue> <stack> <list> <map> <strstream> <utility>
From C:Â
<cstring> provides string related functions (string.h)Â
<cmath> provides cos(), sin(), round(), sqrt(), pow() and more
<cstdlib> provides conversion, memory handling, sorting, search ..
iostream - What’s New
cin: Equivalent to scanf for input
cout: Equivalent to printf for outputÂ
cerr: Used for printing errorsÂ
clog: Employed for logging purposes
Namespaces
The C++ standard library uses a namespace call std
Namespaces provide space where we can define or declare identifiers. i.e. variables, methods and classes
A namespace is designed to differentiate functions, classes, variables with the same name available in different libraries
In essence, a namespace also defines a scope
The string Class
In C, we represent strings as a null terminated sequence of chars in memory and use a pointer to the start of that memory to refer to it (char *)…
This can be a bit limiting however…
We either used fixed length arrays
Or need to deal with dynamic memory allocation (malloc/free)
C++ wraps this functionality in the string class
The string class definition can be imported using #include
string provides access to a wide range of methods useful for string manipulation