Sometimes you get this question on job interviews. Here it is the correct answer: Object Oriented Programming is a programming paradigm designed to improve code reusability. It has four principles, called the pillars of OOP programming. C++ implements these principles:
A class is a named block of code declared using keyword "class". Class name usualy start with uppercase letter and can contain any letters numbers and underscore. The class has a block of code that contains declarations inside. Everithing declared in the class block is called "member" of the class.
Next we define a class called Vechicle that has 3 members. All members are public properties of a Vechicle. We will use this class in next examples to demonstrate how to use a class. Let's study the definition:
//define a class
#include <iostream>
#include <string>
class Vechicle {
public:
string maker;
int year;
int price;
};
A class can serve for two purposes. First is to encapsulate functionality like a library of functions. This use-case is more rarely used. More frequent is to serve as a blueprint to enable creation of objects.
Since the class is basicly a data type it is only logical we can declare variables of this type. These are called "objects" and reprezent data structures with members defined by the Class. Each "object" is also called "instance" of the class.
Next we show how to create two Vechicles that use the class defined in previous example.
//code fragment that use a class
int main() {
Vechicle v1; //first vechicle
Vechicle v2; //second vechicle
// set properties of first vechicle
v1.maker = "Ford";
v1.year = 2001;
v1.price = 500;
// set properties of second vechicle
v2.maker = "Toyota";
v2.year = 2015;
v2.price = 8000;
// output some of the properties
cout << "First vechicle is a " << v1.maker << " from " << v1.year << "\n";
cout << "Second vechicle is a " << v2.maker << " from " << v2.year << "\n";
}
Homework: Open this example on repl.it and run it: cpp-class
Read next: Exceptions