Advert

Constructors in Derived Class

Last edited by . Total of 2 comments in the discussion.

A constructor plays a vital role in initializing an object. An important note, while using constructors during inheritance, is that, as long as a base class constructor does not take any arguments, the derived class need not have a constructor function. However, if a base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor. Remember, while applying inheritance, we usually create objects using derived class. Thus, it makes sense Read More

Share on:

Static Class Members

Last edited by . Total of no comments in the discussion.

The members (data members and member functions) of a class may be qualified as static by preceding member declaration with the keyword static. There may be static data members and static member functions in a class. Static Data Member A static data member of a class is just like a global variable for its class. That is, this data member is globally available for all the objects of that class type. The static data members are usually maintained to store values common to the entire class. For instance, a class Read More

Share on:

Pointers

Last edited by . Total of no comments in the discussion.

A pointer is a variable that holds the memory address, usually a location of another variable in memory. The use of pointers is critical for the successful programming in C++. The three reasons that support this are, Pointers provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in any way. Pointers support dynamic allocation routines. Pointers can improve the efficiency of certain routines. Since pointers hold memory location of another variable and also facilitate dynamic memory allocation, we must Read More

Share on:

Scope Resolution Operator

Last edited by . Total of no comments in the discussion.

Occasionally, it is useful to distinguish between class member names and other names. The scope resolution operator (::) can be used here. For example   class A { private:     int x;     float y; public:     void readm() const;     /*     ———     ———     */ };   In this class the full name (qualified name) of variable x is A::x. Similarly, the qualified name of y is A::y and the qualified name of readm() is A::readm(). Global Variables and Class Member Variables with the Same Name What Read More

Share on:

Standard Library Functions

Last edited by . Total of no comments in the discussion.

A library is a collection of programs and functions which can be used by other programs. Like all other high-level languages, C++ provides function libraries containing built-In functions. These standard functions are extremely useful to the programmers. The advantages of library functions are listed below: Reusability of code : The program development activity requires three major activities: coding, testing and debugging. If a function is developed previously, then it is available as a standard library function and the effort of redevelopment can be avoided. Faster development : Since many useful Read More

Share on:

Virtual Base Classes

Last edited by . Total of no comments in the discussion.

A situation may arise where all the three kinds of inheritance, namely, multilevel, multiple and hierarchical inheritance are involved. This is illustrated in the figure below; the child has two direct base classes 'parent1' and 'parent2' which themselves have a common base class 'grandparent'. The 'child' inherits the traits of grandparent class via two separate paths. It can also inherit directly, as shown by the dotted lines. The grandparent class is sometimes referred to as indirect base class. Such inheritance by the child class may create some problems. All the Read More

Share on:

Operator Overloading – Introduction

Last edited by . Total of no comments in the discussion.

Operators like + (plus), – (minus), * (multiply), / (divide) work very well with data types such as int, float, etc. But when it comes to user defined data types these operators do not work on their own. To make them work, we need to define special functions that tell the operators how to handle the operands of the user defined class. These special functions are known as operator functions. The general form of an operator function is: return_type classname :: operator op(arg) { ——— function body ——— } Where Read More

Share on:

Function Overloading

Last edited by . Total of no comments in the discussion.

Function overloading is a concept where several function declarations are specified with a single and a same function name within the same scope. Such functions are said to be overloaded. C++ allows functions to have the same name. Such functions can only be distinguished by their number and type of arguments. Example: float divide(int a, int b) ; float divide(float a, float b) ; The function divide(), which takes two integer inputs, is different from the function divide() which takes two float inputs. What is the need for function overloading? Read More

Share on:

Selection Sort

Last edited by . Total of 1 comment in the discussion.

Selection sort requires (n-1) passes to sort an array. In the first pass, we find the smallest element from a[0], a[1], a[2], … a[n-1] and swap it with the first element, i.e. a[0]. In the second pass, we find the smallest element from a[1], a[2], a[3]….a[n-1] and swap it with a[1] and so on. Pass1. Find the location loc of the smallest element in the entire array, i.e. a[0],[1],a[2]…a[n-1]. Interchange a[0] & a[loc]. Then, a[0] is trivially sorted. Pass2. Find the location loc of the smallest element in the entire Read More

Share on:

Constructors that Allocate Memory Dynamically

Last edited by . Total of no comments in the discussion.

Constructors can be used to initialize member objects as well as allocate memory. This can allow an object to use only that amount of memory that is required immediately. This memory allocation at run-time is also known as dynamic memory allocation. The new operator is used for this purpose. Sample Program The program below shows the use of new in constructors of a simple String class.   /* * Program to illustrate use of dynamic memory allocation * in constructors * http://www.byteguide.com */ #include #include class String { private: char Read More

Share on: