Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs....

42
Objects and Classes Ali Malik [email protected]

Transcript of Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs....

Page 1: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Objects and Classes

Ali Malik [email protected]

Page 2: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Game Plan

Recap

Representing Complicated Types

Classes

Structs

Page 3: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Announcement

Page 4: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Recap

Page 5: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,
Page 6: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,
Page 7: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Let’s imagine our language only has 3 function words:

[I, the, there]

The Idea

Deep into that darkness peering, long I stood there, wondering, fearing, doubting, dreaming dreams no mortal ever dared to dream before.

- Edgar Allan Poe

I first met Dean not long after my wife and I split up. I had just gotten over a serious illness that I won’t bother to talk about, except that it had something to do with the miserably weary split-up and my feeling that everything there was dead.

- Jack Kerouac

Page 8: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

The Idea

[ 1 , 0 , 0 ] [ 4 , 1 , 1 ]

Page 9: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

The Idea

[ 1 , 0 , 0 ] [ 4 , 1 , 1 ]

The closer this angle, the more similar the texts

Page 10: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,
Page 11: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Designing Axess

Page 12: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A database of students.

What makes up a student?

● They have a name

● They have a SUID

● They have taken a certain number of units

Designing Axess

Page 13: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A database of students.

What makes up a student?

● They have a name (string)

● They have a SUID

● They have taken a certain number of units

Designing Axess

Page 14: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A database of students.

What makes up a student?

● They have a name (string)

● They have a SUID (string)

● They have taken a certain number of units

Designing Axess

Page 15: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A database of students.

What makes up a student?

● They have a name (string)

● They have a SUID (string)

● They have taken a certain number of units (int)

Designing Axess

Page 16: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A database of students.

What makes up a student?

● They have a name (string)

● They have a SUID (string)

● They have taken a certain number of units (int)

Designing Axess

How can we capture this in code?

Page 17: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A First Idea

Page 18: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

struct Student {std::string name;std::string suid;

int unitsTaken;};

A First Idea

Page 19: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A First IdeaWhat are the issues with using just structs?

● Public access to all internal state data.

● Users of struct need to explicitly initialize each data member.

● In short: not one neat package of an object

Page 20: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

A First IdeaWhat are the issues with using just structs?

● Public access to all internal state data.

● Users of struct need to explicitly initialize each data member.

● In short: not one neat package of an object

There must be a better way!

Page 21: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

“A struct simply feels like an open pile of bits with very little in the way of encapsulation or functionality. A class feels like a living and responsible member of society with intelligent services, a strong encapsulation barrier, and a well defined interface”

- Bjarne Stroustrup

A First Idea

Page 22: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Classes

Page 23: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

ObjectsWe use objects all the time (string, vector etc.)

Objects encapsulate behaviour

Classes allow you to define your own type as if it were built into C++

Page 24: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

We want to be able to use Student like a built-in type.

C++ lets the programer do this!

But first, some design...

Classes

Page 25: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Public interface:

● Get name, suid, and number of units

● Get class year (based on units)

● Check if international/local student

Classes

Page 26: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

We are going to break the class up between the interface (Student.h) and implementation (Student.cpp):

StudentClass(StudentClass.pro)

Classes

Page 27: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Members of a struct by default public

Member of a class are by default private

Structs vs Classes

struct Student {std::string name;std::string suid;

int unitsTaken;};

class Student {public:

std::string name;std::string suid;

int unitsTaken;};

Page 28: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Members of a struct by default public

Member of a class are by default private

Structs vs Classes

class Student {std::string name;std::string suid;

int unitsTaken;};

class Student {private:

std::string name;std::string suid;

int unitsTaken;};

Page 29: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

There was a lot of std:: and Student:: in our code

Why?

Scope Resolution

Page 30: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

The standard library uses common names

● string● max ● count

It is easy for libraries to conflict in their names

NamespaceClash(NamespaceClash.pro)

Namespaces

Page 31: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Most modern languages use namespaces to fix this

Namespaces

Page 32: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Most modern languages use namespaces to fix this

# Generate a random number in Pythonimport random

print random.random()

Namespaces (Python)

Page 33: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Most modern languages use namespaces to fix this

# Read file in JavaScriptconst fs = require('fs);

const data = fs.readFileSync('file.txt')

Namespaces (JavaScript)

Page 34: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Most modern languages use namespaces to fix this

# Generate a random number in Python#include <algorithm>

std::count(v.begin(), v.end(), 5);

Namespaces (C++)

Page 35: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Questions

Page 36: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Now we have a usable Student class.

Let’s now represent the database!

vector<Student> database;

Classes

Page 37: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Now we have a usable Student class.

Let’s now represent the database!

vector<Student> database;

Classes

Page 38: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Now we have a usable Student class.

Let’s now represent the database!

Database db;

Classes

Page 39: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

The Database class will internally have a vector of Student

Public interface:

● Add student to database

● Check if student is in database

● Get students in a single year

Classes

Page 40: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

Classes

Example:

StudentClass(StudentClass.pro)

Page 41: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,

C++ doesn’t know how to use operators on types defined by us:

● We can tell it how to via operator overloading.

An algorithm needed a function that could capture a local variable

● More about this next time.

Classes - Issues

Page 42: Objects and Classes - Stanford University...Recap Representing Complicated Types Classes Structs. Announcement. Recap. Let’s imagine our language only has 3 function words: [I, the,