Sage-Code Laboratory
index<--

C++ Classes

The main difference between C and C++ is the programming paradigm. While C is structured, C++ is object oriented. That is you can define classes. A class is a derived data type that encapsulate private and public elements. In this article we will explain how to create and use classes and objects to create better applications.

What is OOP?

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:

  1. Abstraction: you can define abstract classes and interfaces,
  2. Encapsulation: you can define public and private properties and members,
  3. Inheritance: one class can inherit properties and methods from parent class,
  4. Polymorphism: one method from a parent class can be overwritten in a child class.

What is a Class?

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.

Example:

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;
};

Using a Class

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.

Example:

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";
}
Caution:  Maybe you are a Java programmer. You will notice we do not use keyword "new" like in Java to create objects. That is just in case you wander. In C++ the keyword "new" do exist but has a different ussage.

Homework: Open this example on repl.it and run it: cpp-class


Read next: Exceptions