Top Banner
CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT
13

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

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT.

CIS 101: Computer Programming and Problem Solving

Lecture11Usman Roshan

Department of Computer Science

NJIT

Page 2: CIS 101: Computer Programming and Problem Solving Lecture11 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 3: CIS 101: Computer Programming and Problem Solving Lecture11 Usman Roshan Department of Computer Science NJIT.

Defining classes

class <class name> {

public:

<type> <variable of function>;

private:

};

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

Rectangle class

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

Rectangle class

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

Constructors, destructors, andcopy constructor

• Constructor: get called when a new instance of the object is created.– rectangle myrec; //constructor gets called

• Destructor: gets called when you exit the scope of the variable– void myfunction(){

• rectangle myrec;• myrec.setlength(2); myrec.setwidth(1); myrec.print();

– } //once we exit this function the destructor gets called• Copy constructor: gets called everytime a copy of the object is

created– rectangle myrec2 = myrec;//not called in this case, we’ll have to write

a //separate function for this which (if time) we may see later– void myfunction(rectangle myrec);– void main(){

• rectangle myrec;• myfunction(myrec) //at this point a copy of myrec is created that is local to

//myfunction– }

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

Constructor

Everytime we create a rectangleits length and width are automaticallyset to 2 and 2.

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

Overloading constructor

You can have two or more constructors:convert and default

Also called function overloading

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

Copy constructor

Implicit copy constructor is calledwhich copies width and length ofr1 into width and length of r2.

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

String class

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

Strings

mystring

p

size 7

string(int s){ size = s; p = new char[size]; }

string mystring(7);

string( string& from) { size = from.size; p = from.p; }

string mystring2(mystring);

mystring2

p

size 7

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

Strings

mystring

p

size 7

string(int s){ size = s; p = new char[size]; }

string mystring(7);

string( string& from) { size = from.size;p = new char[size]; for(int i=0;i<size;i++){

p[i]=from.p[i];}

}

string mystring2(mystring);

mystring2

p

size 7

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

Lab problems

1. Create string class and add functions:1. Copy string2. Compare string for equality3. Concatenate strings4. Substring

2. Create Vector class1. Copy vector2. Vector equality3. Dot product4. Euclidean norm (length) of vector