Introduction
Classes and Objects in C++ are the basic concepts of object-oriented programming - OOP. Object-oriented programming - is an extension of structured programming, in which the basic concepts are the concepts of classes and objects. The main difference between the programming languages C and C++ are classes in the latter, and hence the C language doesn’t support OOP.
The Beginning
A Class
So a class in C++ is an abstraction describing the methods and properties of objects that don’t exist yet. An object is a concrete representation of that abstraction and has its own properties and methods. Objects, created on the basis of a class, are called class instances. These objects may have different behaviors, properties, but will still be objects of the same class.
Lets take a look at the structure of class declaration:
{
private: // List of properties and methods for use within the class;
string name;
string position;
int yearBirth;
public: /* List of properties and methods available to other features and objects of the program */
Employee(string _name, string _position, int _yearBirth) //constructor
{
position=_position;
yearBirth=_yearBirth;
}
protected: // List of facilities available at inheritance;
}; // mind this semicolon. it must always finish the class;
Inheritance
Inheritance is a process that allows you to create a new subclass based on an existing, with all the characteristics of the parent class assigned to a child class. More correctly, the object can inherit the basic properties of another object, and complement features that are unique to this object. Inheritance is a vital element, which backs the concept of a class hierarchy. For example, consider the further description of a bicycle. Bikes can be for adults or kids, single or tandem, etc. So in this case class Bicycle is a superclass to the (its) subclass KidsBicycle. KidsBicycle inherits all the properties from Bicycle (color, owner’s name and so on) plus adds its own properties such as «wheelsQnty» and «MaxAge». However class Bicycle can also be a subclass to some class, say Vehicle, inheriting its features. In each case, all derived classes inherit all the associated info of the parent and adds its own defining characteristics. Inheritance plays a very important role in OOP.
{
protected:
int maker;
string color;
// some more code
};
{
private:
int maxAge; /* in the result, an object of this class will have the inherited properties (maker, color) and its own (maxAge) */
// some code
};
Abstraction
Abstraction is a process of separating ideas from specific instances of those ideas at work. Computational structures are defined by their meaning, while hiding away the details of how they work. Abstraction is widely used in implementing the interface by creating pure virtual methods and then overriding them in the derived classes.This allows you to work with objects, without going into specifics of their implementation.
Encapsulation
Encapsulation is a property of the programming language that allows the user not to think about the complexity of software components (what’s inside?), but interact with it through the provided interfaces (public methods and members), as well as integrate and protect vital component data. Encapsulation - one of the four major mechanisms of object-oriented programming (along with abstraction, polymorphism and inheritance). Hiding implementation is a very handy feature for further program editing.
{
private:
string name;
int age;
public:
// constructors and other methods
void setName (string _name) // a method to set an employee’s name
{
this->name=_name;
}
void setAge (int _age) // a method to set an employee’s age
{
this->age=_age;
}
string getName() // a method to get an employee’s name
{
return name;
}
int getAge() // a method to get an employee’s age
{
return age;
}
};
Polymorphism
Polymorphism is a property of a class that allows using class objects with the same interface without any information about the type and the internal structure of the object.
Templates
Templates are designed for the rapid generation of new classes or functions with the same functionality. Template parameters are the types themselves. Any pattern starts with the word template whether it’s a function or a class template. Then angle brackets < > list the parameters of the template. The keyword class or typename must precede each parameter. For example:
template <class T>; // or
template<typename T>; // or
template <typename T, typename S>;
Keyword typename indicates that the template will use a built-in data type such as: int, double, float, char, etc. A keyword class tells the compiler that a custom datatype will be used as a parameter. Make sure not to confuse a template parameter and a class template.
Container Class
Container class — is a template class used for storing elements of the specified type in contiguous memory locations. C++ already includes a large number of containers as part of the STL (Standard Template Library). A container can be imagined as a room with its structured internal rules of storing and managing the things you put into it. Different rooms have different shelves, wardrobes and thus have different methods of operating them. The container automatically arranges the memory space for the elements it holds and provides member functions to work with them. Accessing and operating the elements can be done through direct-manipulations or through the use of iterators.
C++ containers classes include commonly used data structures:vector, queue, stack, priority_queue, list, set, map etc.
The main criteria in choosing the right one correctly is the efficiency of using this very container (complexity), but not the functionality it offers. The reason for this is concealed in the characteristics and internal structure of the container. This aspect is particularly obvious in the use of sequences containers, which provide different complexity characteristics of such modifying operations as insert, delete and element access operations (at, front,back). Containers are divided into 3 groups:
- Sequence containers
- array
- vector
- deque
- forward_list
- list
- Container adaptors
- stack
- queue
- priority_queue
- Associative containers
- set
- multiset
- map
- multimap
You can see 3 container adaptors: stack, queue and priority_queue. They are called like that because of their not full «containerability", meaning that they only provide a certain interface, based on the object of the container classes. The base container is encapsulated so that its contents is managed by the members of the adaptor, but not the base container.
Advices from the Creator
A worldwide famous programmer and developer of C++ Bjarne Stroustrup published a set of rules-advices, which are to help beginners:
- If you can think of ‘‘it’’ as a separate idea, make it a class.
A class is a sort of a suite with properties and functions that generalizes the whole object type. For example, cars can be sedan, touring, coupe, SUV and so on, but all of them are still cars and have common basic characteristics (maker, model) and functions (accelerate, brake, turn).
2. If you can think of “it” as a separate entity, make it an object of some class.
Now, the object is not something general, but vice versa - a concrete thing, which has some exact properties. For example, «Car» class object is called «Car1» and has properties maker=«AUDI» and model=«A6 Avant». These characteristics are the features that make the object a separate entity.
3. If two classes have a common interface, make that interface an abstract class.
Using abstract classes may significantly make your code easier and more lightweight. An abstract class being a base class implement a general concept, which is overridden in derived classes.
4.If the implementations of two classes have something significant in common, make that commonality a base class.
The base class usually holds the general variables and functions, which are complemented with new more specific ones in the derived classes, therefore eliminates the need to write numerous classes with similar characteristics.
5. If a class is a container of objects, make it a template.
Since containers are used for storing objects of different type, creating a template container will help you develop a universal storage for different datatypes.
6. If a function implements an algorithm for a container, make it a template function implementing the algorithm for a family of containers.
The sense of creating a template function is similar to creating a template container - universal and easy coding helps you share the functionality with a number of similar containers.
7. If a set of classes, templates, etc., are logically related, place them in a common namespace.
A set of classes and functions can have a common namespace in order to group them according to their functional connectivity. Name conflicts are much less likely, because, in addition to a short or local name, classes have a fully qualified name that includes their namespace.
References
Allain, Alex. Lesson 12: Introduction to Classes in C++. Retrieved May 19, 2014, from http://www.cprogramming.com/tutorial/lesson12.html
Allain, Alex. Lesson 19: Inheritance in C++. Retrieved May 20, 2014, from http://www.cprogramming.com/tutorial/lesson19.html
Allain, Alex. Lesson 20: C++ Inheritance - Syntax. Retrieved May 20, 2014, from http://www.cprogramming.com/tutorial/lesson20.html
Oxford University Computing Services. Programming in C++. Retrieved May 21, 2014, from http://www.oucs.ox.ac.uk/documentation/userguides/c++/l931.pdf
Stroustrup, Bjarne. Notes to the Reader. Retrieved May 21, 2014, from http://www.stroustrup.com/3rd_notes.pdf
Classes (I). Retrieved May 18, 2014, from http://www.cplusplus.com/doc/tutorial/classes
Pitts, Robert. (2000). Introduction to Polymorphism in C++. Retrieved May 21, 2014, from https://www.cs.bu.edu/teaching/cpp/polymorphism/intro
C++ Inheritance. Retrieved May 19, 2014 from http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
Module 12. C++ Objects & Classes. Encapsulation Principles and Code Samples 1. Retrieved May 20, 2014, from http://www.tenouk.com/Module12.html
Alex. (2011, September 11). 16.2 STL Containers Overview. Retrieved March 22, 2014, from http://www.learncpp.com/cpp-programming/16-2-stl-containers-overview