26
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

  • View
    232

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

CIS 101: Computer Programming and Problem Solving

Lecture10Usman Roshan

Department of Computer Science

NJIT

Page 2: CIS 101: Computer Programming and Problem Solving Lecture10 Usman 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]

Page 3: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Dynamic arrays

Page 4: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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;

Page 5: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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

Page 6: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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’ };

Page 7: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Creating strings

Page 8: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Output

Page 9: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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

Page 10: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Dynamic strings

Page 11: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

OutputGarbage characters

Page 12: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 13: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Output

Page 14: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 15: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Output

Page 16: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 17: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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

Page 18: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 19: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Defining classes

class <class name> {

public:

<type> <variable of function>;

private:

};

Page 20: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 21: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 22: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

OutputNow what is we changed the public to private?

Page 23: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Rectangle class

Now length and width are private?

This means they cannot onlyBe accessed by functions definedIn the class.

Compilation produces two errors.

Page 24: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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.

Page 25: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

Rectangle class

We have defined two newfunctions to set the lengthand width of the rectangles.

We use them to set lengthand width.

Page 26: CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT

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