C++ Files

C++ Files

The fstream library allows us to work with files.

To use the fstream library, include both the standard <iostream> AND the <fstream> header file: Continue reading C++ Files

C++ Polymorphism

Polymorphism

Polymorphism means “many forms”, and it occurs when we have many classes that are related to each other by inheritance.

Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways. Continue reading C++ Polymorphism

C++ Inheritance Access

Access Specifiers

You learned from the Access Specifiers chapter that there are three specifiers available in C++. Until now, we have only used public (members of a class are accessible from outside the class) and private (members can only be accessed within the class). The third specifier, protected, is similar to private, but it can also be accessed in the inherited class: Continue reading C++ Inheritance Access

C++ Multiple Inheritance

Multiple Inheritance

A class can also be derived from more than one base class, using a comma-separated list: Continue reading C++ Multiple Inheritance

C++ Multilevel Inheritance

Multilevel Inheritance

A class can also be derived from one class, which is already derived from another class.

In the following example, MyGrandChild is derived from class MyChild (which is derived from MyClass). Continue reading C++ Multilevel Inheritance

C++ Inheritance

Inheritance

In C++, it is possible to inherit attributes and methods from one class to another. We group the “inheritance concept” into two categories:

  • derived class (child) – the class that inherits from another class
  • base class (parent) – the class being inherited from

To inherit from a class, use the : symbol.

In the example below, the Car class (child) inherits the attributes and methods from the Vehicle class (parent): Continue reading C++ Inheritance

C++ Encapsulation

Encapsulation

The meaning of Encapsulation, is to make sure that “sensitive” data is hidden from users. To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class). If you want others to read or modify the value of a private member, you can provide public get and set methods.

Continue reading C++ Encapsulation

C++ Access Specifiers

Access Specifiers

By now, you are quite familiar with the public keyword that appears in all of our class examples:

Example

class MyClass {  // The class
  public:        // Access specifier
    // class members goes here
};

The public keyword is an access specifier. Access specifiers define how the members (attributes and methods) of a class can be accessed. In the example above, the members are public – which means that they can be accessed and modified from outside the code. Continue reading C++ Access Specifiers

C++ Constructors

Constructors

A constructor in C++ is a special method that is automatically called when an object of a class is created.

To create a constructor, use the same name as the class, followed by parentheses () : Continue reading C++ Constructors

C++ Class Methods

Class Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

  • Inside class definition
  • Outside class definition

In the following example, we define a function inside the class, and we name it “myMethod“.

Note: You access methods just like you access attributes; by creating an object of the class and using the dot syntax (.): Continue reading C++ Class Methods