OOP
C++ OOP
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions.
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep the C++ code DRY “Don’t Repeat Yourself“, and makes the code easier to maintain, modify and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time
What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.

So, a class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the variables and functions from the class.
Attributes and Methods
Example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake.
Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as “class members”.
class: attributes, methods——>variables, functions——>class members
Create a class
Create a class called “MyClass“:
1 |
|
The
classkeyword is used to create a class calledMyClass.The
publickeyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class.Inside the class, there is an integer variable
myNumand a string variablemyString. When variables are declared within a class, they are called attributes.At last, end the class definition with a semicolon
;.
create an object
To create an object of MyClass, specify the class name, followed by the object name.
To access the class attributes (myNum and myString), use the dot syntax (.) on the object:
1 | int main() { |
create a method
There are two ways to define functions that belongs to a class:
- Inside class definition
- Outside class definition
Note: You access methods just like you access attributes; by creating an object of the class and using the dot syntax (.):
In the following example, we define a function inside the class, and we name it “myMethod“.
1 | class MyClass { // The class |
To define a function outside the class definition, you have to declare it inside the class and then define it outside of the class. This is done by specifiying the name of the class, followed the scope resolution :: operator, followed by the name of the function:
1 | class MyClass { // The class |
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 ():
1 |
|
Constructors can also take parameters (just like regular functions), which can be useful for setting initial values for attributes.
The following class have brand, model and year attributes, and a constructor with different parameters. Inside the constructor we set the attributes equal to the constructor parameters (brand=x, etc).
1 | class Car { // The class |
Access specifier
The public keyword is an access specifier, that defines how the members (attributes and methods) of a class can be accessed.
Access specifiers define how the members (attributes and methods) of a class can be accessed.
public- members are accessible from outside the classprivate- members cannot be accessed (or viewed) from outside the classprotected- members cannot be accessed from outside the class, however, they can be accessed in inherited classes.
Note: By default, all members of a class are private if you don’t specify an access specifier:
1 | class MyClass { |
In the following example, we demonstrate the differences between public and private members:
1 | class MyClass { |
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). To achieve this, you must declare class variables/attributes as private (cannot be accessed from outside the class).
Access Private Members
If you want others to read or modify the value of a private member, you can provide public get and set methods.
1 |
|
Why Encapsulation?
- It is considered good practice to declare your class attributes as private (as often as you can). Encapsulation ensures better control of your data, because you (or others) can change one part of the code without affecting other parts(when you are using multiple objects
- Increased security of data
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):
1 |
|
Why And When To Use “Inheritance”?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
Multilevel Inheritance
A class can also be derived from one class, which is already derived from another class.
1 | // Base class (parent) |
Multiple Inheritance
A class can also be derived from more than one base class, using a comma-separated list:
1 | // Base class |
Access Specifiers
The third specifier, protected, is similar to private, but it can also be accessed in the inherited class:
1 | // Base class |
Polymorphism
Polymorphism uses those methods that inherited from base class to perform different tasks. This allows us to perform a single action in different ways.
Just override the method from the base class in the derived class.
1 |
|
Why And When To Use “Inheritance” and “Polymorphism”?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
C++ Files
Expectations
When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).
Exception handling in C++ consist of three keywords:
try,throwandcatch:if a error is detected in the try{}, throw the exception and handle it in catch{}.
1 | try { |
1 | try { |
