Classes in C++ A lot of this stuff is trivia, but it can be hard to - - PowerPoint PPT Presentation

classes in c a lot of this stuff is trivia but it can be
SMART_READER_LITE
LIVE PREVIEW

Classes in C++ A lot of this stuff is trivia, but it can be hard to - - PowerPoint PPT Presentation

Classes in C++ A lot of this stuff is trivia, but it can be hard to discern up front. Classes in C++ are complex and have a ton of rules, most of which I look up as I go Ill try to point out the core concepts and assign readings / practice


slide-1
SLIDE 1

Classes in C++

slide-2
SLIDE 2

A lot of this stuff is trivia, but it can be hard to discern up

  • front. Classes in C++ are complex and have a ton of rules,

most of which I look up as I go I’ll try to point out the core concepts and assign readings / practice

slide-3
SLIDE 3

Because there are so many rules, you have to do the reading Coming to class will not be enough

slide-4
SLIDE 4

Let’s say I wanted to represent a rectangle

slide-5
SLIDE 5

class Rectangle { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } unsigned int area() const { return (length * width); } };

slide-6
SLIDE 6

class Rectangle { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } unsigned int area() const { return (length * width); } };

Member variables

slide-7
SLIDE 7

class Rectangle { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } unsigned int area() const { return (length * width); } };

Constructor

slide-8
SLIDE 8

class Rectangle { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } unsigned int area() const { return (length * width); } };

Method

slide-9
SLIDE 9

Rectangle myRectangle = Rectangle(12,14);

“Give me an empty Rectangle and then call its constructor to fill it in.”

slide-10
SLIDE 10

Rectangle(unsigned int len, unsigned int wid) { length = len; width = wid; }

This is called a “constructor” It is a special method, called whenever a Rectangle is created, to set it up

slide-11
SLIDE 11

Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { }

Here I use an initialization list

In C++, initialization is a special thing, and this method is preferred (More efficient, for reasons we’ll learn later)

slide-12
SLIDE 12

Special Constructors

  • A default constructor is one without any arguments
  • C++ makes this for you if you don’t define a constructor
  • A copy constructor copies an object
  • C++ will generate this too
slide-13
SLIDE 13

int main() { Rectangle myRectangle = Rectangle(12,14); Rectangle myOtherRectangle(myRectangle); cout << "The area of myOtherRectangle is “ << myOtherRectangle.area() << endl; return 0; }

Initialization by copy-constructor

slide-14
SLIDE 14

But you can define it instead, if you want!

slide-15
SLIDE 15

Rectangle(const Rectangle &other) : length(other.length), width(other.width) { }

Note: must be const…

slide-16
SLIDE 16

I can define things outside of classes too…

// shapes.h class CartesianPoint { public: double distanceFrom(CartesianPoint &other) const; } // shapes.cc double CartesianPoint::distanceFrom(CartesianPoint &other) const { return sqrt((x - other.getX()) * (x - other.getX()) + (y - other.getY()) * (y - other.getY())); }

Functions smaller than a line or two should usually go outside of the header file

slide-17
SLIDE 17

Why keep things private?

  • Reveal as little about your implementation as possible
  • Because if someone else assumes details, I’ll break their code
  • Almost never make member vars public
slide-18
SLIDE 18

Consider what would happen if we wrote 20k lines

  • f code that used x and y, but then we wanted to

change to the top left and bottom right point

We’d waste a ton of time rewriting it

slide-19
SLIDE 19

Consider what would happen if we wrote 20k lines

  • f code that used x and y, but then we wanted to

change to the top left and bottom right point

slide-20
SLIDE 20

This is the biggest mistake new programmers make: declare everything public. Hiding things makes things harder, and that’s exactly what we want

slide-21
SLIDE 21

Exercise: write impl of rectangle that caches area

slide-22
SLIDE 22

In class exercise: use top-left coordinate, bottom-right coordinate instead

slide-23
SLIDE 23

I can define things outside of classes too…

// shapes.h class CartesianPoint { public: double distanceFrom(CartesianPoint &other) const; } // shapes.cc double CartesianPoint::distanceFrom(CartesianPoint &other) const { return sqrt((x - other.getX()) * (x - other.getX()) + (y - other.getY()) * (y - other.getY())); }

Functions smaller than a line or two should usually go outside of the header file

At the end of a function, const means “I don’t change anything”

slide-24
SLIDE 24

Polygon Rectangle Triangle

Now let’s say I want…

slide-25
SLIDE 25

class Shape { public: virtual double area() const = 0; };

Abstract class

Pure virtual method

slide-26
SLIDE 26

class Shape { public: virtual double area() const = 0; };

Abstract class

Pure virtual method I’m not defining it here, but subclasses must

slide-27
SLIDE 27

class Shape { public: virtual double area() const = 0; };

Abstract class

Pure virtual method I’m not defining it here, but subclasses must Because Shape has a pure virtual method, no concrete Shape can exist, it is an abstract class

slide-28
SLIDE 28

class Shape { public: virtual double area() const = 0; };

Abstract class

Pure virtual method I’m not defining it here, but subclasses must Because Shape has a pure virtual method, no concrete Shape can exist, it is an abstract class Big note: the = 0 does not mean to return 0, it means nothing defined

slide-29
SLIDE 29

class Rectangle : public Shape { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } Rectangle(const Rectangle &other) : length(other.length), width(other.width) { } virtual double area() const { return (length * width); } };

This syntax means “Rectangle inherits from Shape”

slide-30
SLIDE 30

class Rectangle : public Shape { private: unsigned int length; unsigned int width; public: Rectangle(unsigned int length, unsigned int width) : length(length), width(width) { } Rectangle(const Rectangle &other) : length(other.length), width(other.width) { } virtual double area() const { return (length * width); } };

This syntax means “Rectangle inherits from Shape”

https://stackoverflow.com/questions/860339/difference- between-private-public-and-protected-inheritance

slide-31
SLIDE 31

Subclasses

  • Inherit all member variables, can modify public / protected ones
  • Can redefine virtual methods
  • It may help to think of these as “redefinable” methods
slide-32
SLIDE 32

Storage

slide-33
SLIDE 33

Storage

We’re not going to talk about pointers yet, so don’t think about that today

slide-34
SLIDE 34

length width area() double area() name() double name() This is what a Rectangle looks like to C++ ctr..

slide-35
SLIDE 35

length width area() double area() name() double name() This is what a Rectangle looks like to C++ ctr.. Constructors, etc…

slide-36
SLIDE 36

length width area() double area() name() double name() A class is like a recipe to make a rectangle “To make a rectangle, I need to fill in length / width” ctr..

slide-37
SLIDE 37

x y getX() getY()

  • thers…

This is what a CartesianPoint looks like

slide-38
SLIDE 38

So what does a Triangle look like?

slide-39
SLIDE 39

CartesianPoint point2 area() name() ctr.. CartesianPoint point3 CartesianPoint point1

slide-40
SLIDE 40

CartesianPoint point2 area() name() ctr.. CartesianPoint point3 CartesianPoint point1 Stuff the box for CartesianPoint here

slide-41
SLIDE 41

CartesianPoint point2 area() name() ctr.. CartesianPoint point3 x y getX() getY() point1 Same for these

slide-42
SLIDE 42

So the rule is, if you see a class as a member variable of another class, it’s like stuffing a box inside a box

slide-43
SLIDE 43

Next class, we’ll see that these boxes are laid out in memory