2. What is a class?
■ A class is a container that can hold many different types of objects.
■ What is another type of container that we have learned that can only contain objects
of the same type?
– Arrays!
■ int arr[3] holds 3 ints only.
■ A class “Person” can hold many different types of objects inside of it.
3. Classes are…
■ The ingredients to a cake!
– Many different components needed.
– Egg cartons are arrays, holding only eggs.
■ The statistics of a movie!
– How long is the movie?Who plays in it?
– A class can hold all of that information!
■ A hotel!
– Is it full?What’s its name?
– Hotel can hold an array of Rooms, which also is a class!
4. Overview of Classes
■ Private vs. Public
– Private means only functions inside of the class can use it!
– Public means anyone from any other function (like main()) can use it!
■ Constructor
– Allows you to initialize all variables of the class to some base number like 0.
■ Getters and Setters
– Usually member variables are private, so you will always need a getter/setter!
■ Calling the Class
– Class.function();
5. Private vs. Public
■ Classes are inherently private, meaning that they start off as private unless otherwise
specified.
■ Only the class’s own functions can touch its private member variables.
– NOT OKAY: OKAY:
6. Constructor
■ When we have the code int x; …What is x?
– Undefined! Because we don’t have a constructor for an int variable.
– Imagine being able to always set an undefined variable to zero…!
■ Two types of constructors:
9. Getters and Setters
■ How fat is my cat?
■ How do I change my cat’s name?
■ My cat bit me! How do I change its niceness?
■ Answer: Getters and Setters!
– You need to write a function to return or change its private member variables.