Introduction to Object-Oriented Programming
Programming:
Program -- A list of instructions for a computer to execute.
Evolution of languages:
-
Machine languages -- this is what is necessary for the computer, but hard
for people to read
-
Assembly languages -- these are symbolic translations of machine code,
easier for people to read
-
High-level procedural languages (like Pascal, Fortran, C)
-
Object Oriented languages (C++, Java, Smalltalk)
Computers need machine code instructions, but programmers would
like to read and write programs on a higher level. High-level languages
like C and Pascal are also known as procedural languages. They allow
the writing of procedures and functions for code modularity, often dividing
the work into separate actions. High level languages are much
more readable for programmers. Since the computer still needs machine
code instructions, high-level languages are usually run through a compiler,
which translates high-level code into the machine's own instructions.
Object-oriented languages, like C++ and Java, take this a step further
and encapsulate their data and procedures together in units called objects,
which contain more than just functions (actions, often representable by
verbs). These languages make items modular as well (objects, or things
representable by nouns). Object-oriented languages are also high-level
languages, more readable for people, and needing translation for the machine
(by a compiler or interpreter).
C is a high-level procedural programming language.
C++ is an object-oriented programming language based on C.
Classes and Objects:
-
Object -- an encapsulation of data and functions that act upon
that data. The three aspects of an object:
- Name -- the variable name we give it
- Attributes (member data) -- the data that describes the what the
object is
- Behavior (member functions) -- behavior aspects of the object
(functions that describe what the object does)
- Class -- a blueprint for objects. A class is a user-defined
type that describes what a certain type of object will look like. A class
description consists of a declaration and a definition (usually split into
separate files).
- An object is a single instance of a class.
You can create many
objects from the same class type, in much the same way that you can create
many houses from the same blueprint.
What's so special about objects?
The C language has a similar concept called a struct. What's
the difference? A struct consists of:
-
Name -- the variable name we give it
-
Attributes -- data stored inside the struct
So, what's the difference?
-
In C, data and functions are separate
-
In C++, you can build obects that encasulate data and functions together
C++ libraries can contain useful classes of objects that store data
in useful and reusable ways. Classes become building blocks
for use in other programs.