Skip to Content

C++: Intro to OOP Ideas

What is Object-Oriented?

Object Orientation is the combination of three main ideas:

  • Encapsulation: this means putting things together to make it look like a whole and to hide the internal things from external inspectors outside.
  • Inheritance: this means that some types can be subtypes of other things and inherit their features. For this, always think of the phrase “is-a”. For example, a bear is-a mammal. Bears inherit features of mammals (all mammals have hair; bears have hair).
  • Polymorphism: this means that things in your program can act differently based on the context. (Greek “poly” means many, “morph” means form; so the word means “many forms”)

It will take us most of the rest of the semester to introduce these three concepts.

Every “object-oriented” language has these three capabilities. If a language does not, it should not claim to be an OO language.

What is Good Object Oriented Programming?

When we get to a programming language, most OO programming languages use the keyword class – and we quickly start thinking about what classes our program should have.

This is bad! Object oriented programming is about objects!

When we are writing code, we are looking at a description of a computation that will happen when our program executes. Because objects exist at runtime, we sometimes forget about them when looking at our code, and only think about classes. This is bad!

A class is a description of a particular kind of object (but we might create many objects of that kind). Because of this, a class name should always be singular. It is a name of a kind of object; e.g., Customer is a good class name. Customers is not a good class name; if you need to keep track of a collection of customers, then you should call your class SetOfCustomers (set is singular), or better yet use a generic/template such as Set<Customer>.

What is an Object?

Think of an object is a living thing. In an executing program, objects do have a lifetime: they are created, they do things, and then they are destroyed. Living things can do things, they have capabilities; living things also have characteristics, they have attributes. To read more about this concept, see here and here.

In programming, the attributes of an object are the data members in the object. Data members must always be private; only the object itself should be allowed to modify its own attributes. This is the reason that setter methods are evil; they are just a way to allow the outside world to modify attributes. Getter methods are also somewhat evil; usually they indicate lazy programming and misplaced capability. If something needs done using an object’s attributes, the object itself should do it! To read about getters and setters being evil, look here, here, and here.

The capabilities of an object are the methods (or operations) of the object; Public methods are the interface that the object presents to the rest of the world; these are things that the object can do to or for the rest of the program. Private methods are generally helper methods that are used to help better organize our code (divide-and-conquer) so that the public methods are cleaner.

So what is a Class?

OO programming languages use the class idea to allow a programmer to create a new datatype, which is “this kind of object”. Just like a built-in type, such as int, once a class is defined, you can create variables that hold the kind of object that you defined with the class.

An object is an instance of a class. Just like each integer variable is an instance of the type int, each object is an instance of the user-defined data type (the class). It’s no different!

Always be thinking of a single object when writing the code for a class, even if you will create many objects of the class. E.g., when creating a Customer class, you are describing what one Customer object is and can do, even if you will create many of them.

Encapsulation: The First Object Oriented Concept

In OO languages, encapsulation takes on two forms:

One: Functions (now called methods) that are associated with the data of the object (in code, the class) are placed in the class. So both data and computation are encapsulated into the object (in code, the class). In non-OO language features, like C structs, only data fields exist in a structure. OO brings in functions (now called member functions or methods).

Two: new access modifier keywords public and private allow you to explicitly state which elements of an object (in code, the class) are visible from the outside (public) and which are hidden (private). Once we bring in inheritance, even more access modifiers like protected.

Non-OOP programming languages, like plain C, do have the ability to create data aggregates, often called records or tuples or structures, but these do not have functions in them, nor is anything private. In plain C, the struct keyword lets you declare data records.

C++ allows the keyword struct to still be used, but its has members that are by default public. In a C++ class, members are by default private. However, it is generally good practice to explicitly declare which members are public and which are private.

Inheritance and Polymorphism are Going to be on another page….

Inheritance

Polymorphism

OLD from here on down…still needs editing

Example C++ Class

See the handout for the complete code listing.

class Line
{
   public:
      // constructors (overloaded)
      Line();
      Line(int x1, int y1, int x2, int y2, int color=0);
      // destructor
      ~Line();
      // public methods
      void movePosition(int x1, int y1, int x2, int y2);
      void reColor(int color);
      void translateTo(int x, int y);
      double length(void);
      void draw(void);
   private: // private data
      int x1;
      int y1;
      int x2;
      int y2;
      int color;
};

Like the plain C struct, the general form of a class is class ClassName {...};

You do not need to use typedef in C++. The class name (e.g., Line) is automatically defined as a new type name.

Unlike Java, in a C++ class declaration, you only put the method prototypes, not the full method bodies.

As the listing shows, the method bodies are declared outside of the class declaration, but with ClassName:: attached to the front of the method name. Other than that, writing a method is just like writing a function.

Methods are called on an object. On the listing, you can see that calling a method is a lot like accessing a data member of a struct – you use the pointer to the object, an “->” operator, and then the method name and arguments.

The object that the method is called on has automatic access to the object’s data fields. It can refer to them directly, as “local” variables. If the method needs to refer to the object pointer directly, it uses the variable name this.

Two special types of methods: constructors and destructors.

A constructor is a special method in a class that has the same name as the class itself.

A class can have many constructors (this is an example of function/method overloading, because they all have the same name).

Each time an object is created, one and only one constructor is called. For the example program, the constructor with no arguments is called for the first object created, and the constructor with arguments is called for the second two objects created.

Constructors are responsible for initializing the object. Depending on the arguments and which constructor is called, the initialization state or level may be different.

A destructor is a special method in a class that is called when an object is destroyed. The destructor has the name ~ClassName – yes that is a tilde (~) in front of the class name.

A class can only have one destructor. It does not have to have one.

The destructor is responsible for releasing any resources that the object is holding outside of itself. The object itself will be deallocated by the memory allocation system. But if it is holding other allocated memory (e.g., a string), open file handles that need closed, or anything else that needs released, it must be done in the destructor.

Constructors and the destructor must be public.

Other methods in the class are declared by the programmer for whatever needs the class and program have. Other methods may be public or private. Most methods are public, but private methods are useful for subcomputations that the class needs but that should not be allowed to be called all by themselves from the rest of the program.

Data in a class should always be private. This is a major benefit of OO programming! The external program should only interact with the class through its public methods. It should not be able to access the data directly.

This concept is known as information hiding. The idea is that different pieces of the program should be provided information on a need to know basis.

Information hiding also means that one piece of a program hides the exact mechanisms it is using to store the information it is managing, and only allows other pieces of the program to access that information through methods or functions.

With information hiding, you can change how the local data is stored, without affecting any other parts of the program, since they don’t have access to it anyways.