Essential C++ Concepts: Constructors, Inheritance, and Loops
Constructors in C++
Definition: A constructor is a special member function of a class that is automatically called when an object of the class is created. It is mainly used to initialize the data members of the class. The constructor has the same name as the class and it does not have any return type.
Characteristics
- Constructor name is the same as the class name.
- It has no return type (not even void).
- It is automatically executed when the object is created.
- It initializes the data members of the class.
Types of Constructors
- Default Constructor: Takes no arguments.
- Parameterized Constructor: Takes arguments to initialize variables.
- Copy Constructor: Creates a new object from an existing object.
Code Example
class Student
{
int roll;
public:
Student(int r)
{
roll = r;
}
};Inheritance in C++
Definition: Inheritance is a feature of Object-Oriented Programming in which one class acquires the properties and behavior of another class. It allows code reusability and reduces duplication. The class whose properties are inherited is called the base class, and the class that inherits is called the derived class.
Advantages
- Provides code reusability.
- Reduces duplication of code.
- Improves program structure.
- Establishes a relationship between classes.
Types of Inheritance
- Single Inheritance: A derived class inherits from exactly one base class. It is the most straightforward “is-a” relationship.
- Multiple Inheritance: A single derived class inherits from two or more base classes simultaneously. While powerful, it can lead to complexity (like the Diamond Problem).
- Multilevel Inheritance: A class is derived from another derived class, creating a chain (e.g., Class B inherits from A, and Class C inherits from B).
- Hierarchical Inheritance: Multiple derived classes all inherit from the same single base class. This is common for grouping similar objects under a general category.
- Hybrid Inheritance: A combination of two or more of the types above. For example, using both Multilevel and Multiple inheritance in the same structure.
Code Example
class Parent
{
};
class Child : public Parent
{
};Method Overloading
Definition: Method overloading is a feature in C++ where two or more functions have the same name but different parameters within the same class. It is an example of compile-time polymorphism.
Rules
- Function name must be the same.
- Number or type of parameters must be different.
- Return type alone cannot differentiate overloaded functions.
Advantages
- Increases flexibility of the program.
- Improves readability.
- Avoids the use of different function names.
Code Example
class Add
{
public:
int sum(int a, int b)
{
return a + b;
}
int sum(int a, int b, int c)
{
return a + b + c;
}
};The For Loop
Definition: A for loop is a control statement used to execute a block of code repeatedly when the number of iterations is known. It is an entry-controlled loop because the condition is checked before execution.
Syntax
for(initialization; condition; increment/decrement) { statements; }
Working
- Initialization is executed once.
- Condition is checked before each iteration.
- If the condition is true, the loop body executes.
- Increment or decrement updates the variable.
- The loop stops when the condition becomes false.
Code Example
for(int i = 1; i <= 5; i++)
{
cout << i; // The loop prints numbers 1 to 5
}