Microsoft Store
 

C plus plus


 

C++ (pronounced "see plus plus", IPA: /si? pl?s pl?s/) is a general-purpose computer programming language. It is a statically typed free-form multi-paradigm language supporting procedural programming, data abstraction, object-oriented programming, and generic programming. Since the 1990s, C++ has been one of the most popular commercial programming languages.

Object-oriented features of C++

C++ introduces some object-oriented (OO) features to C. It offers classes, which provide the four features commonly present in OO (and some non-OO) languages: abstraction, encapsulation, polymorphism, and inheritance.

Related Topics:
Object-oriented - Class - Abstraction - Encapsulation - Polymorphism - Inheritance

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Encapsulation

C++ implements encapsulation by allowing all members of a class to be declared as either public, private, or protected. A public member of the class will be accessible to any function. A private member will only be accessible to functions that are members of that class and to functions and classes explicitly granted access permission by the class ("friends"). A protected member will be accessible to members of classes that inherit from the class in addition to the class itself and any friends.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

The OO principle is that all and only the functions that can access the internal representation of a type should be encapsulated within the type definition. C++ supports this (via member functions and friend functions), but does not enforce it: the programmer can declare parts or all of the representation of a type to be public, and is also allowed to make public entities that are not part of the representation of the type. Because of this C++ supports not just OO programming but other weaker decomposition paradigms, like modular programming.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

It is generally considered good practice to make all data private, or at least protected, and to make public only those functions that are part of a minimal interface for users of the class that hides implementation details.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Polymorphism

Polymorphism is a widely used and abused term that is not well defined.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

In the case of C++ it is often used in connection with member function names, where the function name corresponds to several implementations, and which implementation gets invoked depends either on the type of the arguments (static polymorphism) or on the type of the class instance value (dynamic polymorphism) used on which the virtual member function is invoked.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

For example, a C++ program may contain something like this:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

/* Static polymorphism */

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

extern void SendJobToDevice(PrintJobText *, DeviceLaser *);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

extern void SendJobToDevice(PrintJobText *, DeviceJet *);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

extern void SendJobToDevice(PrintJobHTML *, DeviceLaser *);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

extern void SendJobToDevice(PrintJobHTML *, DeviceJet *);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

...

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

SendJobToDevice(printJob, device);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

/* Dynamic polymorphism */

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

class Device {

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

public:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

virtual void print(PrintJob*);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

...

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

};

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

PrintJob *printJob;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Device *device;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

...

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

device->print(printJob);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

// Note that since C++ does not have multiple dispatch, the above

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

// function call is polymorphic based only on the device's type.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

In C, (dynamic) polymorphism of a sort can be achieved using the switch statement or function pointers.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

C++ provides two more sophisticated features for polymorphism: function overloading and virtual member functions. Both features allow a program to define several different implementations of a function for use with different types of objects.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Function overloading allows programs to declare multiple functions with the same name. The functions are distinguished by the number and types of their formal parameters. For example, a program might contain the following three function declarations:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

void pageUser(int userid);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

void pageUser(int userid, string message);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

void pageUser(string username);

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Three different pageUser() functions are declared. When the compiler afterwards encounters a call to pageUser(), it determines which function to call based on the number and type of the arguments provided. (The compiler considers only the parameters, not the return type.) Because the compiler determines which function to call at compile time, this is called static polymorphism. (The word static is used here in the sense of "not moving". It denotes that the determination is made based solely on static analysis of the source code: by reading it, not by running it. By the time the program executes, the decision has been made.)

Related Topics:
Static polymorphism - Static analysis

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Operator overloading is a form of function overloading. It is one of C++'s most controversial features. Many consider operator overloading to be widely misused, while others think it is a great tool for increasing expressiveness. An operator is one of the symbols defined in the C++ language, such as +, !=, <, or &. Much as function overloading allows the programmer to define different versions of a function for use with different argument types, operator overloading lets the programmer define different versions of an operator for use with different operand types. For example, if the class Integer contains a declaration like this:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Integer& operator++();

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

then the program can use the ++ operator with objects of type Integer. For example, the code

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Integer a = 2;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

++a;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

behaves exactly like this:

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Integer a = 2;

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

a.operator++();

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

In most cases, this would then increment the value of the variable a to 3. However, the programmer who created the Integer class can define the Integer::operator++() member function to do whatever he wants. Because operators are commonly used implicitly, it is considered bad style to declare an operator except when its meaning is obvious and unambiguous. Curiously, it can be argued that the standard libraries do not follow this convention. For example, the object cout, used for outputting text to the console, has an overloaded << operator for outputting the text. Critics argue that this use is non-obvious because << is the operator for a bit shift, which is clearly meaningless in this context. Nevertheless, most people consider this use to be acceptable, and this particular example is certainly in C++ to stay.

Related Topics:
Console - Bit shift

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

C++ templates make heavy use of static polymorphism, including overloaded operators.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Virtual member functions provide a different type of polymorphism. In this case, different objects that share a common base class may all support an operation in different ways. For example, a PrintJob base class might contain a member function

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

virtual int getPageCount(double pageWidth, double pageHeight)

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Each different type of print job, such as DoubleSpacedPrintJob, may then override the method with a function that can calculate the appropriate number of pages for that type of job. In contrast with function overloading, the parameters for a given member function are always exactly the same number and type. Only the type of the object for which this method is called varies.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

When a virtual member function of an object is called, the compiler sometimes doesn't know the type of the object at compile time and therefore can't determine which function to call. The decision is therefore put off until runtime. The compiler generates code to examine the object's type at runtime and determine which function to call. Because this determination is made on the fly, this is called dynamic polymorphism.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

The run-time determination and execution of a function is called dynamic dispatch. In C++, this is commonly done using virtual tables.

Related Topics:
Dynamic dispatch - Virtual table

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Inheritance

Inheritance from a base class may be declared as public, protected, or private. This access specifier determines whether unrelated and derived classes can access the inherited public and protected members of the base class. Only public inheritance corresponds to what is usually meant by "inheritance". The other two forms are much less frequently used. If the access specifier is omitted, inheritance is assumed to be private for a class base and public for a struct base. Base classes may be declared as virtual; this is called virtual inheritance. Virtual inheritance ensures that only one instance of a base class exists in the inheritance graph, avoiding some of the ambiguity problems of multiple inheritance.

Related Topics:
Virtual inheritance - Multiple inheritance

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Multiple inheritance is another controversial C++ feature. Multiple inheritance allows a class to derive from more than one base class; this can result in a complicated graph of inheritance relationships. For example, a "Flying Cat" class can inherit from both "Cat" and "Flying Mammal". Some other languages, such as Java, accomplish something similar by allowing inheritance of multiple interfaces while restricting the number of base classes to one (interfaces, unlike classes, provide no implementation).

Related Topics:
Multiple inheritance - Java - Interfaces

~ ~ ~ ~ ~ ~ ~ ~ ~ ~