View
232
Download
1
Tags:
Embed Size (px)
Citation preview
CIS 101: Computer Programming and Problem Solving
Lecture10Usman Roshan
Department of Computer Science
NJIT
Dynamic arrays
int *x;
x = new int[3];
x[0] = 2;
x[2] = 4;
x[3] = 6;
Memory
x
50 2
x[0]
4 6
x[1] x[2]
Dynamic arrays
Dynamic arrays
• Memory defined using new must be cleared up using delete. Otherwise your program may use up ALL the memory.
int *x = new int[10];
.
.
delete x;
Two dimensional arrays
2D arrays are defined as
<type> <array name>[<#rows>][<#columns>];
For example,
int A[10][20]
allocates space for a 2-D array of 200 integers. Any cell of the array can be accessed using A[i][j] where is between 0 and 9 (0 and 9 inclusive) and j is between 0 and 19 (0 and 19 inclusive).
StringsStrings are arrays of characters. A character x is defined using
char x;
We can set x to say ‘A’ using
x=‘A’;
Strings are just arrays of characters. So if we want to create a string of length 10 called mystring we would define it as
char mystring[10];
If we want to initialize mystring to say “Hello” we would define mystring as
char mystring[10] = { ‘H”, ‘e’, ‘l’, ‘l’, ‘o’ };
Creating strings
Output
Dynamic strings
char *x;
x = new char[5];
x[0] = ‘H’;
x[1] = ‘e’;
x[2] = ‘l’;
x[3] = ‘l’;
x[4] = ‘o’;
Memory
x
50 H
x[0]
e l
x[1]
l o
x[2]
x[3]
x[4]
50 51 52 53 54
Dynamic strings
OutputGarbage characters
Dynamic strings
We need a null character tospecify the end of the string.
The length of the string is increasedby one for the null character.
Output
Dynamic arrays
You cannot print an entire integer array in the same manner. This code will justoutput the value of the pointerx, which is just a memorylocation.
Output
Object Oriented Programming
You can imagine integers, characters, and arrays as objects. They contain data on which various operations can be performed. We can define new objects which contain more data and new operations on them.
Integers, characters, floats (real numbers), and pointers are fundamental data types (or fundamental objects if you like). These can be used to create new objects. These new objects are created using a special type of variable called class. A class can contain several fundamental data types and even other pre-defined classes.
Classes
Let’s say we want to define a rectangle object. This is the same defining a new type of variable called rectangle. Previous types we have seen so far are int, char, float, and their pointers.
int length;int width;int area();float diagonal();
This is our rectangle class.it contains the length andwidth, and also functionsto compute the area anddiagonal.
Rectangle
Classes
Variables and functions in classes fall into two categories: private and public.
Public variables and functions can be accessed by any function in your program.
Private variables can only be accessed by other functions of the class.
Let’s look at an example to understand this better.
Defining classes
class <class name> {
public:
<type> <variable of function>;
…
private:
…
};
Defining the rectangle class
Defining the rectangle class
Defining length and width variables
Defining function to compute area and diagonal of rectangle. There are no parameters because the width and length are defined in the class.
Defining the rectangle class
Setting length and width of rectangle
Computing area and diagonal of rectangle. Since area and diagonal is previously defined
in rectangle class, we don’t have to redefine it.
OutputNow what is we changed the public to private?
Rectangle class
Now length and width are private?
This means they cannot onlyBe accessed by functions definedIn the class.
Compilation produces two errors.
Rectangle class
Says it cannot access private variables.If we want to keep these private we can define new functionsto set the value of length and width.
Rectangle class
We have defined two newfunctions to set the lengthand width of the rectangles.
We use them to set lengthand width.
Lab problems
1. Update the rectangle class:1. Write a class function which adds one rectangle into another.2. Set length and width in one function instead of two.3. Update rectangle to 3 dimensions---add new variable for
height and modify rectangle and diagonal functions
2. Write a copy array function3. Create the following classes:
1. Circle2. Sphere3. String4. 3-D vector class
4. Problems from midterm