INB371 Week 4 Lecture

4 Pages
Unlock Document

Malcolm Corney

INB371 Week 4 Lecture Contents Classes in C++ Vector – Implementation Stacks and Queues - Implementation with Vector Linked Data Structures - implementing Stack with Linked List Classes All programs we’ve used so far have used procedural style of programming – we will now move on to defining classes and creating objects. We will be making a 2 dimensional point. If we were using a type, we’d create a struct with x and y coordinates in it, which can then be used as function results. The same functionality from a struct can be used by defining a Point class. The main difference is that in a class you can declare public and private variables. If the point class is declared with public variables, you can create a new Point class called ‘pt’ and then use ‘pt.x’ to get the x field inside the Point class. This is considered bad practice though, as you want to declare variables as private, and then use methods inside the class to access the variables. E.g. creating a “GetX()” method, which returns x, instead of using ‘pt.x’. When making a class, you’d declare everything in a header file, and then implement it later (see previous lecture notes for how header files work). There is one complication with declaring methods though, and that is when the getX and getY definitions are written, if there are multiple classes with getX and getY, the compiler doesn’t know which to use. Access methods are written like int point::getX() in the implementation file, which says which header to use. Conventional variables are uninitialized by default. OOP relies on constructors for the initialization of variables in an object. In C++, Destructors are also needed to reclaim memory once an object is no longer required. Java and C# use ‘Garbage Collection’ to scan heap for unreferenced objects and remove them, but this doesn’t happen in C++. It has no garbage collector, hence the requirement of destructor methods. Prototypes for constructor and destructor are included in the header file. To declare a destructor, you use the syntax ‘~Classname();’ (have a ~ before the class name). In the header file, you don’t have any bodies, you simply declare any methods or variables. Then in a separate file, you set up the implementation. Once it’s been implemented you can use ‘Typename method(var,var)’ to construct. ‘Point origin(1,2)’ etc. Constructing an object like this: Point::Point(int x, int y) { x = x; y = y; } // Bad is bad, because when the compiler reads it, it will not be able to see the instance variable x and y, it will only see the method’s x and y. To get around this, you can instead use this->varname. The ‘this->’ keyword points to the method itself, so saying this->varname would look for varname in the instance, instead of the function. Point::Point(int x, int y) {this->x = x; this->y = y) // Good Vector Now we’ll look at how the Vector class from the STL library works, and how to declare/define one. We will do this by making an IntVector class. It will have less things check lecture slide First, you’ll create the interface, then the implementation. TABLE ON LECTURE SLIDES CONTAINING METHODS WE WILL HAVE IN THE CLASS. Creating the header The IntVector header file uses ifndef and define to make sure there isn’t more than one copy of the definition. The class is named IntVector, and we start off by declaring the prototypes of the public methods, such as the Constructor, Destructor, etc. Each method should also be commented with the usage, what it takes and what it does. In a few of the functions, we’re declaring the int index as ‘unsigned’, which means you can’t use negative numbers – if you put a negative number, it won’t compile. This is a safety against incorrect inputs. We then declare all the private methods and variables. You finish it off with an #endif. The class is setting up a CAPACITY constant with a method of 100, and then making an int element array of size CAPACITY. There is also a ‘count’ variable to count how many elements are actually in the vector, so that when it knows when it runs out. IntVector Implementation When it comes to implementing, you must #include “intVector.h” (include the intvector header file). There’s nothing to do in the constructor except for setting ‘count’ to 0, because the element array is already set to a size of CAPACITY as found in the header. In the constructor, there’s nothing to do, so you just leave it empty again. Size returns count, isEmpty returns true or false depending on if count is 0 or not. Clear sets the count to 0, which means any new elements added will simply override old elements in the vector. The add method checks if the count is at the CAPACITY const (100). If it is, the vector is full, and it outputs an error. If it’s not full, it stores the new value at count++. The Get method checks if the index is greater than the count, which will see if it exists in the vector or not, and if it does exist, it will return that element. The Set method changes an element at index to a different value. To insert a value at a specific element, you must shuffle every other element in the array to make a space. To do this you have to loop from the last element to the first, and move them all up one by shifting their indexes up an extra space. You also have to increment the count by one at the end. The remove method does the opposite of the insert method, by shuffling down instead of up. A vector with a capacity limit isn’t useful. We need to be able to make it dynamically increase in size. To do this, you have to edit the header declaration to make a private const called INITIAL_CAPACITY, and you need to have a pointer to the variable ‘elements’. You also need to provide a private method called increaseCapacity, which is responsible for increasing the capacity. Now, the constructor will create
More Less

Related notes for INB221

Log In


Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.