[\<- 01/07](01-07.md) # OOP: Classes, Objects, and ADTs ## Object Oriented Programming - OOP is a programming language model organized around **objects** rather than "actions" and **data** rather than logic - There are 4 major principles that make a language Object Oriented - Encapsulation - Data Abstraction - Polymorphism - Inheritance ### Encapsulation - Hiding mechanism, hiding of data implementation - Private variables - Public accessor methods ### Abstractions - A concept or an idea which is not associated with any particular instance - Expressing the "intent" and not the "implementation" - **Abstract** classes and **virtual** members ### Polymorphism - One name, many forms! - What does `a+b` mean? - What is `a` and what is `b`? ### Inheritance - "is a" or "has a" relationship between objects - Dog "is a" mammal - Ferrari "is a" car, "has an" engine ## Class **A class is a new kind of data type** - A collection of data, such as integers, characters, and so on - We implement data structures as classes - A class has the ability to include special functions, called **member functions** which are designed specifically to manipulate the class ### Example: Thinking Cap ``` class thinking_cap{ public: void slots(char new_green[], char new_red[]); void push_green() const; void push_red() const; private: char green_string[50]; char red_string[50]; }; ``` - Note: the `const` means that the function cannot change the data in the variables of the class - `const` should be specified for all "getter" functions ### Files for Thinking Cap - The `thinking_cap` class definition, which we have just seen, is placed with documentation in a file called `thinker.hxx` outlined here - The implementations of the three member functions will be placed in a separate file called `thinker.cxx`, which we will examine in a few minutes ### Using the Thinking Cap - A program that wants to use the `thinking_cap` must **include** the thinker header file (along with its other head inclusions) - The program starts by calling the slots member function for student ``` #include #include #include "thinker.h" int main(){ thinking_cap student; thinking_cap fan; student.slots("Hello", "Goodbye"); fan.slots("Go Broncos!", "Boo!"); student.push_green(); //expected output: "Hello" fan.push_green(); //expected output: "Go Broncos!" student.push_red(); //expected output: "Goodbye" } ``` ### Thinking Cap Implementation - The below code belongs in the `thinking_cap` class definition ``` void thinking_cap::slots(char new_green[], char new_red[]){ assert(strlen(new_green) < 50); assert(strlen(new_red) < 50); strcpy(green_string, new_green); strcpy(red_string, new_red); } void thinking_cap::push_green{ cout << green_string << endl; } ``` ### A Common Pattern - Often, one or more member functions will place data in the member variables so that other member functions may use that data ### Summary - Classes have member variables and member functions. An object is a variable where the data type is a class - You should know how to declare a new class type, how to implement its member functions, how to use the class type - Frequently, the member functions of an class type place information in the member variables, or use information that's already in the member variables - In the future, we will see more features of OOP --- ## Class Constructors - **Constructors** provide an initialization function **that is guaranteed to be called** - **A constructor is a member function** with **these special properties** - A constructor is **called automatically** whenever a variable of the class is declared - The **name of a constructor must be the same as the name of the class** - A constructor **does not have any return value** - You must not write `void` (or any other return type) at the front of the constructor's head ### Example ``` namespace scu_coen79_2A{ class point{ public: //CONSTRUCTOR point(double i_x=0; double i_y=0); void shift(double x_amount, double y_amount); void rotate90(); double get_x() const { return x; } //inline function double get_y() const { return y; } //inline function private: double x; //x coordinate of this point double y; //y coordinate of this point }; } ``` - Notice that the above code has a couple of inline functions - It is best practice to only do this for very short functions ### Write a Constructor for Thinking Cap ``` //prototype in the class definition thinking_cap(); //definition thinking_cap::thinking_cap(){ strcpy(green_string, "Hi"); //default green_string is "Hi" strcpy(red_string, "Bye"); //default red_string is "Bye" } ``` ### Automatic Constructor - If you write a class with no constructor, - The compiler automatically creates a simple default constructor - **This automatic default constructor doesn't do much work** ### Value Semantic - The value semantics of a class determines how values are copied from one object to another - In C++, the value semantics consists of two operations: - Assignment operator - Copy constructor ### Assignment Operator **Example** - DS1 is a data structure that includes the names of employees in a hospital - DS2 is an object that can include names of employees - We want to assign the values in DS1 to DS2. This is what we do: ``` employeeName DS2; DS2 = DS1; ``` - The **automatic assignment operator** simple **copies each member variable** from the object on the left of the assignment **Note**: Automatic assignment operator **does not always work** (future lectures) ### Copy Constructor **Example** - DS1 is a data structure that includes the names of employees in a hospital - We want to create an object DS2 while making it an exact copy of DS1 - This is what we do: ``` employeeNames DS2(DS1); ``` - **A constructor with exactly one argument** - The data type of the argument is the same as the constructor's class - Or equivalently ``` employeeNames DS2 = DS1; ``` ### C++ Copy Constructor - C++ provides an **automatic copy constructor** - The automatic copy constructor initializes a new object by **merely copying all the member variables from the existing object** - When you implement a class, **the documentation should include a comment indicating that the value semantics is safe to use** ``` //VALUE SEMANTICS for the XYZ class: //Assignments and the copy constructor may be used with XYZ objects ```