C++ Object-Oriented Programming Concepts Explained
Classified in Computers
Written on in
English with a size of 2.39 KB
Inheritance
Inheritance is the capability of a class to derive properties and characteristics from another class.
Deep Copy vs. Shallow Copy
Deep Copy
In a deep copy, an object is created by copying the data of all variables and allocating new memory resources with the same values. To perform a deep copy, you must explicitly define a copy constructor and assign dynamic memory if required. This also necessitates dynamic memory allocation in other constructors, requiring custom copy constructors and overloaded assignment operators.
box(box& sample) {
length = sample.length;
breadth = new int;
*breadth = *(sample.breadth);
height = sample.height;
}Shallow Copy
In a shallow copy, an object is created by simply copying the data of all variables from the original object. This works effectively only if none of the variables are defined in the heap section of memory.
Fraction(int numerator = 0, int denominator = 1) : m_numerator{ numerator }, m_denominator{ denominator }Binding in C++
Static Binding
Static binding occurs at compile-time, where the function call and definition are linked during compilation. It is also known as early binding.
Dynamic Binding
Dynamic binding takes place during runtime based on the object type. Because it is delayed until execution, it is called late binding or runtime binding. This is achieved using virtual functions and overridden methods in C++.
Abstract Classes
An abstract class is designed specifically to be used as a base class and contains at least one pure virtual function. Its purpose is to express broad concepts from which concrete classes are derived. While you cannot instantiate an object of an abstract class, you can use pointers or references to it.
Static vs. Non-Static Methods
- Static Method: Belongs to the class itself. Use this for information common throughout the program or functions called independently of specific objects.
- Non-Static (Instance) Method: Belongs to a specific object of a class. These can only be called after instantiating the class. Use these when data or behavior must be specific to each object instance.