Docsity

Understanding Classes and Structs: Vector and Point Examples, Slides of Data Structures and Algorithms

Partial preview of the text

Download Understanding Classes and Structs: Vector and Point Examples and more Slides Data Structures and Algorithms in PDF only on Docsity!

User-defined Datatypes

Representing a (Geometric) Vector

• In the context of geometry, a

vector consists of 2 points: a

start and a finish

• Each point itself has an x and y

coordinate

Start =

End =

int main() { double xStart = 1.2; double xEnd = 2.0; double yStart = 0.4; double yEnd = 1.6; }

Start =

End =

void printVector(double x0, double x1, double y0, double y1) { cout << "(" << x0 << "," << y0 << ") - > (" << x1 << "," << y1 << ")" << endl; }

int main() { double xStart = 1.2; double xEnd = 2.0; double yStart = 0.4; double yEnd = 1.6; printVector(xStart, xEnd, yStart, yEnd); // (1.2,2.0) - > (0.4,1.6) }

class

• A user-defined datatype which groups

together related pieces of information

Vector

xStart xEnd yStart yEnd

class definition syntax

name

class Vector { public: double xStart; double xEnd; double yStart; double yEnd; };

• This indicates that the new datatype we’re

defining is called Vector

Fields can have different types

class MITStudent { public: char *name; int studentID; };

MITStudent

name studentID

Instances

• An instance is an occurrence of a class.

Different instances can have their own set of

values in their fields.

• If you wanted to represent 2 different

students (who can have different names and

IDs), you would use 2 instances of MITStudent

student

name =?

studentID =?

student

name =?

studentID =?

Accessing Fields

• To access fields of instances, use

variable.fieldName

class MITStudent { public: char *name; int studentID; };

int main() { MITStudent student1; MITStudent student2; student1.name = "Geza"; }

student

student

name = “Geza”

studentID =?

name =?

studentID =?

student

student

name = “Geza”

studentID = 123456789

name =?

studentID =?

Accessing Fields

• To access fields of instances, use

variable.fieldName

class MITStudent { public: char *name; int studentID; };

int main() { MITStudent student1; MITStudent student2; student1.name = "Geza"; student1.studentID = 123456789; }

Accessing Fields

• To access fields of instances, use

variable.fieldName

class MITStudent { public: char *name; int studentID; };

int main() { MITStudent student1; MITStudent student2; student1.name = "Geza"; student1.studentID = 123456789; student2.name = "Jesse"; student2.studentID = 987654321; cout << "student1 name is" << student1.name << endl; cout << "student1 id is" << student1.studentID << endl; cout << "student2 name is" << student2.name << endl; cout << "student2 id is" << student2.studentID << endl;Docsity.com

Start = (0.4, 0.8)

End = (0.9, 1.5)

  • A point consists of an x and y

coordinate

  • A vector consists of 2 points: a

start and a finish

  • A point consists of an x and y

coordinate

  • A vector consists of 2 points: a

start and a finish

class Vector { public: double xStart; double xEnd; double yStart; double yEnd; };

Vector

xStart xEnd yStart yEnd

Start = (0.4, 0.8)

End = (0.9, 1.5)

(^) Doesn’t show that coordinates can be grouped into points

Start = (0.4, 0.8)

End = (0.9, 1.5)

  • A point consists of an x and y

coordinate

  • A vector consists of 2 points: a

start and a finish

Point

x y

class Point { public: double x; double y; };