HomeContact

animations
C++

An introductory tutorial




Language Elements

Object Oriented Concepts

Data Types

Key Words

Arrays

Pointers

Data Structures

Multithreading

Errors and Exception Handling

Interview Questions



Object Oriented Concepts in C++

C++ evolved from the C programming language with the intentional goal of supporting the development of large, complex, and maintainable software while preserving C's speed and low-level control. The most significant change was the addition of the Object-Oriented Programming (OOP) approach, where real-world entities are represented as classes and objects. The services that objects provide are implemented through functions (also called methods). This OOP methodology improves the reliability, adaptability, modifiability, maintainability, and reusability of code. Alongside OOP, C++ introduced other features such as function overloading, stronger type checking, templates, and exception handling, which further enhance safety and flexibility. Key foundational concepts in OOP include Class, Object, Abstraction, Encapsulation, Inheritance, and Polymorphism.

Class

A class in C++ is a definition or "blueprint" for creating objects. It can hold both data members (variables) and member functions (also called methods). Classes are declared using the keyword class followed by a class name (identifier).

In C++, access to class members is controlled by access specifiers:

  • public – accessible from anywhere the class is visible.
  • private – accessible only from within the same class.
  • protected – accessible from within the same class and its derived classes.

Besides access specifiers, there are other class modifiers that affect how classes or members behave:

  • final – prevents a class from being inherited (C++11+).
  • friend – allows another function or class to access private/protected members.
  • virtual – enables dynamic dispatch for member functions or marks a base class for polymorphism.
  • explicit – prevents implicit type conversions for constructors.
  • mutable – allows modification of a member even in a const object.
  • static – makes a member shared by all objects of the class.

Objects

An object is an instance (or example) of a class. A C++ program will typically contain a collection of objects that work together (cooperate) to perform the program's tasks. Each object belongs to some class, and multiple objects (instances) can be created from the same class. A class can define fields (also called properties or data members) to store data, and member functions (methods) to define behavior. Both fields and functions can have access specifiers (public, private, protected) that determine whether they are accessible from outside the class, only within the class itself, or within the class and its derived classes. For example, consider a class Dog with a field breed and methods like bark() or fetch(). A specific dog, such as a GermanShepherd, would be an instance (object) of the Dog class.

For example, a general Shape class could define common fields and methods like color or area(), and specific shapes like Triangle or Rectangle could inherit from it while adding shape-specific behaviors. Before exploring inheritance with multiple classes, here is a simple example showing how two triangles can be instantiated from a class and used to calculate their areas:

#include <iostream >
using namespace std;
// class declaration
class Triangle {
int x, y;// by default for a class, x and y have private access.
public: // two member functions, values and area have public access.
void values (int,int);
int area () {return (x*y)/2;}
};
void Triangle::values (int a, int b) { // :: is scope operator. It denotes the member of class from outside the class definition.
x = a;
y = b;
}
int main () {
Triangle trifirst, trisecond; // trifirst and trisecond are two instances of class Triangle.
trifirst.values (6,7);
trisecond.values (4,5);
cout << "area of first triangle trifirst: " << trifirst.area() << endl;
cout << "area of second triangle trisecond : " << trisecond.area() << endl;
return 0;
}

Abstraction

Abstraction is the process which only exposes details and allows to hide some of the implementation details, so the focus is on what is necessary. It gives a clear separation between properties of data type and the associated implementation details. There are no instances created with Abstract type of class.

Encapsulation

Encapsulation allows to hide the data, as necessary. Classes are created with data and functions (or methods) to provide encapsulation. Accessibility to data in a class is provided by declaring members as public, private or protected. Encapsultion keep data safe from unwanted external interference and reusability.

Inheritance

Inheritance is an object-oriented programming concept where a class (called the derived class) can acquire properties and behaviors (fields and methods) from another class (called the base class) without redefining them. This allows code to be reused and lets classes build upon each other in a parent-child or hierarchical relationship. Inheritance also supports concepts like polymorphism and encapsulation by letting derived classes extend or modify the functionality of base classes.

C++ allows multiple inheritance, where a class can be derived from more than one parent class. This lets a derived class combine features from multiple base classes.

Polymorphism

Polymorphism is the mechanism by mean of which same variable, function or object can take various forms, depending upon the type of data passed to it. It is achieved by using inheritance. There are two main types of polymorphism, one is compile time (static or early binding), and other is run time (dynamic or late binding). Two common kind of polymorphism are Overloading and Overriding.






Copyright © by Zafar Yasin. All rights reserved.