0% found this document useful (0 votes)
6 views22 pages

CS3505 Lecture5

The document covers Lecture 5 of CS 3505, focusing on dynamic arrays and their implementation in C++. It discusses the structure of assignment A2, including the use of HaruPDF and Spiral classes, as well as the importance of modularity in code design. Additionally, it addresses the use of static libraries, pointer management, and the creation of resizable integer arrays, emphasizing key concepts like const correctness and memory management.

Uploaded by

liang19880404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

CS3505 Lecture5

The document covers Lecture 5 of CS 3505, focusing on dynamic arrays and their implementation in C++. It discusses the structure of assignment A2, including the use of HaruPDF and Spiral classes, as well as the importance of modularity in code design. Additionally, it addresses the use of static libraries, pointer management, and the creation of resizable integer arrays, emphasizing key concepts like const correctness and memory management.

Uploaded by

liang19880404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS 3505: Software Practice II

Lecture 5:
Dynamic Arrays
Const
Lecture Quizzes
• I added another dropped score
– Make these last ones more of a practice
• The goal is to encourage timely review of
lecture material
A2
• Three main chunks of code
– HaruPDF
• setup a page/close a page
• place a character at a position/orientation on page
– Spiral
• Move along a spiral in character-sized jumps
– Project
• get text
• loop over text
– get spiral position
– print on pdf
A2 Visually
Not
Spiral HaruPDF HaruPDF
Spiral

Project Project

• Keep modular
• Haru class does haru tasks
#justharuthings HaruPDF
• Spiral class does spiral actions
Project
• main talks to each to do the
task Spiral
A2: Spiral
• You need to adapt the circle
math so the spiral goes

radius * sin(θ)
clockwise from “12 o’clock”.
– First, understand the circle math
radius
• A spiral has an increasing
radius
– Adapt the math in the example radius * cos(θ)
code (centerX, centerY)
– There is a center point, a radius,
and angle that produces a point
on the circle.
– What does it mean to have the
letters stay close as the spiral
grows?
• What are some “gotchas” with
the spiral math?
Code Style
• We are sometimes
going to discuss code
• Sometimes some of
your code
• This is a common
activity in software
development
• It should not be done in
a mean way
– Think of constructive
changes
– But you also have to be
willing to learn from it
Using a Static Library

• Need 3 things in specific spots


• Just refer to these rules
– Compiling from .cpp to .o just needs header
information. Add an include path.
– Linking from .o to an executable needs
• A library path
• A library name

• That is all!
Using a Static Library

• When compiling the .cpp -> .o files


– Path to the header files (-I part)
– No library info used

point.o: point.cpp point.h


$(CC) $(CFLAGS) –I../pointlib/include –c point.cpp
Using a Static Library
• When linking the .o and .a -> executable
– Path to the .a file -L
– Name of the .a file -lname or just libname.a
• -l adds back in the lib and .a
• libraries go on the end of build line with ordering
– A library that has code that another library needs goes after the
calling library
– Add twice for circular dependencies

point: main.o point.o


$(CC) $(CFLAGS) -L$(POINTLIBDIR) -o point main.o
point.o -lpoint
NULL and nullptr
• A pointer that is not pointing to an object
should be set to nullptr
– NULL is older C++
C-style Arrays
int vals[3]; // where does this live?
int vals[3] = {1,2,3};
int *vals = new int[3];
C++11 allows
int *vals = new int[3] {1,2,3};
Make A Resizable Int Array
• What are some basic operations it should
support?

• What might the internals look like?


Class DArray
• Data Members
int len_; // or size_t
int *data_;
• Constructor
Darray(int len = 5) {
len_ = len;
data_ = new int[len_];
}
• Need destructor as well to delete [] data_;
Object Lifespan
• Data Members • What does this do?
int len_; // or size_t Darray d;
int *data_;

• Constructor Where do the instance


Darray(int len = 5) { variables live?
len_ = len;
data_ = new int[len_];
} When do things go
away?
Access
• Make it act like a normal array?
int operator[](int index) const {
return data_[index];
}

int& operator[](int index) {


return data_[index];
}

- what is the const keyword there?


How to read const
• const says things cannot be altered
const int pi = 3; // must initialize
const int * vp; // * to const int
int const * vp; // same
int * const cp // const * to varying int

• Const modifies token to left


– read right to left
– if on far left, move over 1 to right
Const in Methods
• Can declare return type const
const int val() { … }
• Can declare the method cannot change
data members
int val() const { … }

– a getter should not modify the state of the object


Const in Parameters
• Parameters can be const
• One important use – cheat the
pass-by-value cost
void method( BigObject b) {…}
void method( BigObject& b) {…}
void method( const BigObject& b) {…}
• The first has an expensive copy cost, the
second makes b changeable in the
method, and the third saves the copy but
no changes allowed.
Issues with const
• const is infectious.
– If something is made const, then code it uses
also needs to be const to guarantee no
changes
Back to the Resizable Array
• What would a resize method have to do
• What would code for this look like?
Resizing
• Need to void resize(int size) {
– allocate a new array int *newdata = new
– copy existing data int[size];
– delete old array for (int i = 0;
i<len_ && i<size;
– set data members
i++)
newdata[i] = data_[i];
delete[] data_;
• We will not discuss data_ = newdata;
exceptions and len_ = size;
errors for now. }
Try it Out
• Declare some vars DArray other;
DArray vals(4);

• Access them vals[0] = 1;


vals[1] = 2;
vals[2] = vals[1] + 1;
• Resize vals.resize(10);
• Assign other = vals;

You might also like