Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT."— Presentation transcript:

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

2 Dynamic arrays int *x; x = new int[3]; x[0] = 2; x[2] = 4; x[3] = 6; Memory x 50 2 x[0] 46 x[1] x[2]

3 Dynamic arrays

4 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;

5 Two dimensional arrays 2D arrays are defined as [ ][ ]; 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).

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

7 Creating strings

8 Output

9 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] el x[1] l o x[2] x[3] x[4] 50 51 52 53 54

10 Dynamic strings

11 Output Garbage characters

12 Dynamic strings We need a null character to specify the end of the string. The length of the string is increased by one for the null character.

13 Output

14 Dynamic arrays You cannot print an entire integer array in the same manner. This code will just output the value of the pointer x, which is just a memory location.

15 Output

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

17 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 and width, and also functions to compute the area and diagonal. Rectangle

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

19 Defining classes class { public: ; … private: … };

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

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

22 Output Now what is we changed the public to private?

23 Rectangle class Now length and width are private? This means they cannot only Be accessed by functions defined In the class. Compilation produces two errors.

24 Rectangle class Says it cannot access private variables. If we want to keep these private we can define new functions to set the value of length and width.

25 Rectangle class We have defined two new functions to set the length and width of the rectangles. We use them to set length and width.

26 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 function 3.Create the following classes: 1.Circle 2.Sphere 3.String 4.3-D vector class 4.Problems from midterm


Download ppt "CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT."

Similar presentations


Ads by Google