this Pointer in C++

C++ uses a unique keyword called “this” to represent an object that invokes a member function. ‘this’ is a pointer that points to the object for which this function was called. This unique pointer is called and it passes to the member function automatically. The pointer ‘this’ acts as an implicit argument to all the member function. Example: The private variable ‘a’ can be used directly inside a member function, like We can also use the following statement to do the same job. Example: Here this pointer is used to…

Read More

Pointer in C++

When an object is created from its class, the member variables and member functions are allocated memory spaces. The memory spaces have unique addresses. Pointer is a mechanism to access these memory locations using their address rather than the name assigned to them. You will study the implications and applications of this mechanism in detail in this chapter. Pointer is a variable which can hold the address of a memory location rather than the value at the location. Consider the following statement. This statement instructs the compiler to reserve a…

Read More

Virtual Function in C++

Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but different forms. An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes. Virtual functions, one of advanced features of OOP is one that does not really exist but it appears real in some parts of a program. This section deals with the polymorphic features which are incorporated using the virtual functions. Rules For Virtual Functions: When virtual functions are created…

Read More

Polymorphism in C++

What is Polymorphism ? Polymorphism comes from the Greek words “poly” and “morphism”. “poly” means many and “morphism” means form i.e.. many forms. Polymorphism means the ability to take more than one form. Polymorphism allows a single name to be reused for several related but different purposes. The purpose of polymorphism is to allow one name to be used for a general class. Depending on the type of data, a specific instance of the general case is executed. Polymorphism is subdivided in two concepts Compile Time( static) polymorphism and Run…

Read More

Ambiguity in Inheritance

When both the class, a base class and a derived class contains same name method, it creates a problem for the object of derived class to access method by name or parameters. Object will have multiple definitions to call for the same method by name. This problem could be seen in any type of inheritance. It is called ambiguity in inheritance. Look at the example: Here, x1 and x2 will call method of derived class by default. What if you want to access the base class method? C++ provided you a solution…

Read More

Constructor in Derived Class

Base class constructors are always called using the derived class constructors.  Whenever you create derived class object, first the base class default constructor is executed and then the derived class’s constructor finishes execution. In inheritance constructor of base class is inherited like other member functions. Object of derived class, access the constructor of base class like normal functions. Example: Output: Note: Using derived class constructor you can supply the argument to the base class constructor. It follows the deriving order in which they are mentioned. Method of Inheritance Order of Execution Class…

Read More

Abstract Base Class in C++

What is Abstract Base Class ? (ABC) ? Abstract Class is a conceptual class, which contains at least one Pure Virtual function in it. Abstract classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class. Syntax: Abstract Base Class What is Pure Virtual Function in c++ ? Pure virtual Functions are virtual functions with no definition or no body part. They start with virtual keyword and ends with =…

Read More

Virtual Base Class in C++

Virtual Base class is used to resolve the ambiguity in inheritance. While inheriting the base class data there may be a some situation where it may generate an error to access the base class data. Lets check a situation first and after we will discuss the answer for, what is base class ? and why it is used? Scenario 1: Consider a situation, where all the three kinds of inheritance, namely multi-level, multiple and hierarchical are involved. Let us say the ‘ClassD‘ has two direct base classes ‘ClassB’ and ‘ClassC’…

Read More

Hybrid Inheritance

Hybrid inheritance is combination of two or more inheritances such as single, multiple, multilevel or Hierarchical inheritances. There could be situations where we need to apply two or more types of  inheritance to design a program. Syntax: Here, Class Derived is inheriting the property of class BaseB and BaseC. Class BaseB inheriting the class BaseA.

Read More