Principles of Reuse

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/7

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

8 Terms

1
New cards

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

2
New cards

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…

3
New cards

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)

4
New cards

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

5
New cards

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 ..

6
New cards

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

7
New cards

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

8
New cards

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