We know that runtime polymorphism is achieved when the objects of different classes in the class hierarchy respond to the same function call each in its way. To invoke same-named functions present both in the base and derived classes using a single interface. But we found that it still could not execute the derived class’s functions and executed the same-named function defined in the base class. Thus it could not achieve polymorphism. To implement polymorphism for invoking the exact version of the same-named member functions in the class hierarchy, we use virtual functions. [Read more…] about Virtual Function in C++
Virtual functions play an important role in object oriented programming. Ordinary functions use the concept of early binding. However virtual functions help in late binding. The concept of pointers playa vital role in Virtual functions. [Read more…] about Pointer to C++ classes
Every object of a class created in a program is stored somewhere in the memory. This memory is automatically deallocated when the object is destroyed. A constructor allocates resources dynamic memory) to an object during its creation in addition to its property of initialization. Similarly, we should have a member function that deallocates the resources (dynamic memory) allocated to an object. Such counterpart (complement) to the constructor is called a Destructor in C++. [Read more…] about What is Destructor in C++
The member functions are created and placed in the memory space only once when they are defined as a part of a class specification. Since· all the objects belonging to that class use the same member functions, no separate space is allocated for member functions when the objects are created. For each object, memory is allocated only for member data. Separate memory locations for the objects are essential since the member variables hold different data values for different objects. [Read more…] about Write a C++ Program for Memory allocation for a class
Member of a class can be accessed only through the object of a class. The members are accessed as follows: [Read more…] about How to Accessing members of a class in C++
The difference between a structure and a class is that, in a class, the member data or functions are private by default whereas, in a structure, they are public by default. The following segment [Read more…] about Difference between Structures and Classes in C++
The const qualifier is used with the variables of basic data types to prevent them from being modified by the function. In a similar way, const qualifier can also be applied to member functions, member function arguments and the objects of a class. [Read more…] about const Member Functions c++
A function can also return objects either by value or by reference. When an object is returned by value from a function, a temporary object is created within the function, which holds the return value. This value is further assigned to another object in the calling function. [Read more…] about c++ – Returning object from function
The objects of a class can be passed as arguments to member functions as well as nonmember functions either by value or by reference. When an object is passed by value, a copy of the actual object is created inside the function. This copy is destroyed when the function terminates. Moreover, any changes made to the copy of the object inside the function are not reflected in the actual object. On the other hand, in pass by reference, only a reference to that object (not the entire object) is passed to the function. Thus, the changes made to the object within the function are also reflected in the actual object. [Read more…] about Objects as Function Arguments in c++
Like array of other user-defined data types, an array of type class can also be created. The array of type class contains the objects of the class as its individual elements. Thus, an array of a class type is also known as an array of objects. An array of objects is declared in the same way as an array of any built-in data type. [Read more…] about Array of Objects in c++
Arrays can be declared as the members of a class. The arrays can be declared as private, public or protected members of the class. [Read more…] about Arrays as Class Members in C++
The members of a class can be directly accessed inside the class using their names. However, accessing a member outside the class depends on its access specifier. The access specifier not only determines the part of the program where the member is accessible, but also how it is accessible in the program. [Read more…] about Accessing Public and private members of C++ classes
Once a class is defined, it can be used to create variables of its type known as objects. The relation between an object and a class is the same as that of a variable and its data type. [Read more…] about How to Creating Objects and Allocate Memory for Objects in c++
Member functions of a class can be defined either outside the class definition or inside the class definition. In both the cases, the function body remains the same, however, the function header is different. [Read more…] about c++ – Defining member functions inside or outside the class definition
A class in C++ is a user-defined data type that binds data and the functions that operate on the data together in a single unit. Like other user-defined data types, it also needs to be defined before using its objects in the program. A class definition specifies a new data type that can be treated as a built-in data type. [Read more…] about What is Class in C++
Polymorphism (a Greek word meaning having multiple forms) is the ability of an entity such as a function or a message to be processed in more than one form. It can also be defined as the property of an object belonging to a same or different class to respond to the same message or function in a different way. For example, if a message change_gear is passed to all the vehicles then the automobiles will respond to the message appropriately however, the pulled vehicles will not respond. The concept of polymorphism plays an important role in OOP as it allows an entity to be represented in various forms. [Read more…] about What is Polymorphism?
Abstraction is a mechanism to’ hide irrelevant details and represent only the essential features so that one can focus on important things at a time; It allows managing complex systems by concentrating on the essential features only. For example, while driving a car, a driver only knows the essential features to drive a car such as how to use clutch, brake, accelerator, gears, steering, etc., and least bothers about the internal details of the car like motor, engine, wiring, etc. [Read more…] about What is Abstraction? Explain Type of Abstraction.
A feature of some programming languages in which the same 0PERATORmay be used on different data types with different, but analogous, results. For example most languages permit the same operator + to add either INTEGER or FLOATING-POINT numbers, and many further allow it to be used to CONCATENATE strings, so that ‘rag’ + ‘mop’ produces ‘ragmop’. A few languages, including C++, allow the programmer to create new operator overloading. [Read more…] about Write a C++ program to Operator Overloading Using a Friend Function.