Classes and Objects in C++
C++ is a programming language designed to support either object-based programming (using classes and objects in your code) or object-oriented programming (using inheritance and polymorphism as well as classes and objects in your code). The language is said to be extensible because in addition to providing its own built-in simple and structured data types, it allows the programmer to define new data types via the class mechanism. These data types include data, as well as operations on that data. In other words, classes in C++ are used for implementing abstract data types (ADTs).
Every C++ program must have a main function and may have zero or more additional functions, and a typical C++ program will also define and use one or more classes. The usual operative rule declare-before-use applies for both functions and classes.
Programmer-defined types in C++ can be, and should be, first class. The term first class type is used to refer to a type that exhibits expected behaviors in the same sense as the built-in numeric types exhibit those behaviors.
For example, a programmer-defined first class type would be expected to support all of the familiar type operations in a natural way, using overloaded versions of the built-in operators when possible. Such operations would include assignment, testing for equality, and any "arithmetic" operations that make sense, as well as input and output operations.
A class is a set of objects. Or, an object is a single instance of a class. If a class is viewed as a cookie cutter, then objects of that class are the corresponding cookies. In the real-world modeling context, classes are to objects (or class instances) as categories are to individual items. In a programming language, a class is to an object as a type is to a variable. When classes are used to represent abstract data types, an instance of this abstract data type is an object of the class.
Defining classes correctly is just a mechanical skill, whereas designing good classes to use for a particular problem is more akin to an art form.
A class description consists of the following two parts, which often live in physically separate files:
A member function is also called a method. That is, a method is a function that provides an object with some particular behavior. On the other hand, the data members of an object give that object its individual attributes, and collectively determine the state of the object at any given time.
An imaginary member variable is a "variable" (or more correctly, in this context, a "data value") that is provided via a member function via calculation. That is, the value supplied by the function (which appears to be simply an accessor function) is not actually a value stored within the object as a data member, but a value that is calculated from the data values that are stored, but still "appears" to be an actual data member.
Any object has three fundamental aspects:
During its use in a program, we may also be interested in the lifetime of an object and/or its scope, both of which are related to the storage class of the object.
Member data and member functions of a class (and its object instances) can be
Note that protected is a kind of compromise between public and private. Note too that private is the default. Although the order is not prescribed, a common convention is to put the public section of a class declaration first.
A class may declare a function or another class to be a friend. A friend of a class has access to all the private member data and functions of the class but is not itself a member of the class. Function and class friends, just like human friends, should be chosen wisely.
The scope resolution operator (::) designates to which class a function belongs, and appears between the name of a class and the name of the member function in the function header when the function is defined, as in
void ClassName::FunctionName(formal_parameter_list)
A message (to an object) is a call to a member function of the class to which the object belongs. Message sending and message passing are key concepts in object-oriented programming, in which things are accomplished by sending messages. So, to get an object to do something, send it a message. The object may be able to do the thing itself, or it may delegate by passing the message along to yet another object. The syntax for sending a message to an object by calling a function in the interface of the class to which the object belongs is
objectName.FunctionName(actual_parameter_list)
A function declaration that is not a full definition (i.e., is just the function header followed by a semicolon) is called a function prototype, or just a prototype. A class declaration will usually contain just function prototypes rather, than full function definitions, unless some functions have been inlined.
A function function name, together with its formal parameter list of parameter types but no parameter names, is called the function's signature. Note that the return type of a function is not part of the signature, and cannot used to distinguish two functions with the same signature from each other.
Every constructor function (or, more simply, every constructor) must have
The (unique) default constructor for a class is the one with no parameters. A class may have more than one constructor function, and often does. When a class has several constructors, the use of default parameter values is often a convenient way of obtaining several constructors with a single definition.
A copy constructor is a constructor that creates a new object from another object of the same type, and the newly created object is identical to the original.
A conversion constructor is a constructor that can construct a new object from an object of a different type, and will be a constructor with a single parameter whose type is the type which is being used to construct the new value.
A destructor function, of which a class can have only one (or possibly none at all) must have
The term memberwise assignment refers to the default assignment for classes, in which each data member of the left-hand operand is assigned the corresponding right-hand member. This is what will be used by any default versions of both the copy constructor and the assignment operator of any class. The result of memberwise copying of a pointer that copies just the pointer and not the thing pointed at is called a shallow copy. A shallow copy of an object creates an alias for that object. A deep copy, on the other hand, is a copy which also copies the pointer's referent (the thing the pointer points to). A deep copy of an object does not create an alias for the object copied.
The above discussion gives the reason why, when a class involves dynamic data storage allocated from the heap, the class author should always write, in addition to a destructor, both a copy constructor and an overloaded version of the assignment operator for the class. These three member functions are sometimes collectively referred to as The Big Three.