Skip to content

Classes and Objects

Terry Griffin edited this page Nov 12, 2018 · 5 revisions

Classes and Objects

Languages that support object-oriented programming typically use inheritance for code reuse and extensibility in the form of either classes or prototypes. Those that use classes support two main concepts:

1) Classes: Define a structure for the data and available procedures for a given type or class of object. This means that the class definition contains elements representing data packaged with the procedures (known as class methods) that manipulate / access the data. Simply put we package data and methods together.

// Class definition
class Person{
   string first_name;
   string last_name;
   double height;     // in inches
   int weight;        // in pounds
public:
   Person();
   Person(string,string,double,int);
   string getFirstName();
   string getLastName();
   int getWeight();
   double getHeight();
};

A "Class" is the definition portion of an Abstract Data Type (ADT) informing a user how it should be used. It does not become an "object" until it is used (an instance is created).

2) Objects: – instances of classes Objects sometimes correspond to things found in the real world. For example, a graphics program may have objects such as "circle", "square", "menu". An online shopping system might have objects such as "shopping cart", "customer", and "product". Sometimes objects represent more abstract entities, like an object that represents an open file, or an object that provides the service of translating measurements from U.S. customary to metric.

// Creating an instance of a class
Person P;   // Now it's an object

Object-oriented programming is more than just classes and objects. It is a whole programming paradigm based around objects (data structures) that contain data fields and methods which are logically related. Putting un-related data and / or methods together in a class does not fit the paradigm.

Terms

  • Class variables – belong to the class as a whole; there is only one copy of each one (See Static Keyword).
  • Instance variables or attributes – data that belongs to individual objects; every object has its own copy of each one.
  • Member variables – refers to both the class and instance variables that are defined by a particular class.
  • Class methods – belong to the class as a whole and have access only to class variables and inputs from the procedure call (See Static Keyword).
  • Instance methods – belong to individual objects, and have access to instance variables for the specific object they are called on, inputs, and class variables.

Objects are accessed somewhat like variables with complex internal structure, and in many languages are effectively pointers, serving as actual references to a single instance of said object in memory within a heap or stack.

They provide a layer of abstraction which can be used to separate internal from external code. External code can use an object by calling a specific instance method with a certain set of input parameters, read an instance variable, or write to an instance variable. Objects are created by calling a special type of method in the class known as a constructor. A program may create many instances of the same class as it runs, which operate independently. This is an easy way for the same procedures to be used on different sets of data.

Object-oriented programming that uses classes is sometimes called class-based programming, while prototype-based programming does not typically use classes. As a result, a significantly different yet analogous terminology is used to define the concepts of object and instance.

Sources:

  1. https://en.wikipedia.org/wiki/Object-oriented_programming
Clone this wiki locally