Lecture 18: Intro to Classes

Mar 18, 2024  β”‚  Last updated Mar 17, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

  • Various linked list algorithms:
    • Inserting a node
    • Searching for a value
    • Deleting a node
  • Passing linked lists to functions
  • Linked list variations

Textbook Chapter 13

void clear_list(Node *&head) {
    while (head) {
        Node *temp = head;
        head = head->next;
        delete temp;
    }
}

Today’s topics

Textbook Sections 10.2-10.3

Object oriented programming

Abstraction

“The act of separating the essential qualities of an idea or object from the details of how it works or is composed” - Nell Dale and Chip Weems

Abstraction in Computer Science

Procedural abstraction

Say I provide a header file and precompiled object file for the following functions:

// Reads a date formatted as year-month-day from source
void read_date(Date &date, std::istream &source);

// Writes the date to the output stream as year-month-day
void write_date(const Date &date, std::ostream &out);

Data abstraction

Example: int

Note: According to our textbook, built-in types are ADTs

Example: a new list type called IntList

Example: a new list type called IntList

To implement an ADT, we need to define a class

Classes

Objects

Class definition: general form

class ClassName {
public:
    // Public member functions (maybe some variables)
private:
    // Private member variables and functions
};

Example: Time class

In general, anything functions that the user of the class needs to access should be public, and anything else should be private - including member variables!

private members

  • hours
  • minutes
  • seconds

public members

  • write(std::ostream &out)
  • set(int h, int m, int s)
  • int compare(Time other)
  • void increment()

Side tangent: setters and getters

All this being said, the C++ FAQ recommends avoiding trivial getters/setters

time.h

Common for a class to have its own header file and implementation file (.cpp)

#ifndef TIME_H
#define TIME_H
class Time {
public:
    void write(std::ostream &out);
    void set(int h, int m, int s);
    int compare(Time other);
    void increment();
private:
    int hours;
    int minutes;
    int seconds;
};
#endif // TIME_H

emoji Classes Check-in 1/2

How much memory is allocated when the following code is executed?

  1. 0 bytes
  2. 5 bytes
  3. 8 bytes
  4. 24 bytes
  5. Undefined
class Student {
public: 
    void set(int id, const char *name);
    void write(std::ostream &out);
private:
    char name[20];
    int id;
};

emoji Classes Check-in 2/2

What is the main difference between a struct and a class?

  1. A struct is a type of object, a class is a blueprint for creating objects
  2. structs can have public member variables, classes can’t
  3. classes can have functions, structs can’t
  4. struct members are public by default, class members are private by default
  5. structs are allocated on the stack, classes are allocated on the heap

Using classes

bg right fit

Declaring objects

Implementing classes

Implementing the Time::set function

// in time.cpp
void Time::set(int h, int m, int s) {
    hours = h;
    minutes = m;
    seconds = s;
}

Calling member functions

Say we want to find the index position of the word “World” in a string:

Python

hello = "Hello, World!"
pos = hello.find("World")

C++

string hello = "Hello, World!";
int pos = hello.find("World");

How is find implemented? No idea! Thanks, abstraction.

Calling member functions of our own class

Finishing off the class

Coming up next

Textbook Sections 10.2-10.3



Previous: Lecture 16: More Linked lists
Next: Lecture 19: More Classes