Перейти к содержанию

Lesson 84: Classes and objects

English

One class, many objects

  • A class is a plan for objects: it lists their attributes (data) and methods (what they can do).
  • An object is one thing made from the class. Light red(12); makes an object and runs the constructor, which sets its attributes.
  • Private attributes can only be used by the class's own methods. A getter is a public method that returns one of them.
OOP word In this lesson
class Light
objects red, green
attributes pin, timesOn (private)
methods begin, on, off, getTimesOn
  • Write the methods below the class, like void Light::on() { ... }. (If you write a method's body inside the class, this simulator's compiler step does not accept it.)

Войти или создать аккаунт

IGCSE, A-Level & AP