by Dinesh Thakur Category: Classes in C++

A virtual function is a function that is declared as virtual in a base class. A virtual function is always preceded by the keyword virtual. Virtual functions employ late binding by allocating memory space during execution time and not during compilation time. In this case when a pointer of the base class is defined in a main() function and derived class object's address is passed on to the base class pointer, then calling the overridden function will invoke the derived class member function and not the base class member function as mentioned earlier. Following is a program to illustrate the use of virtual functions.

 
by Dinesh Thakur Category: Classes 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.

 
by Dinesh Thakur Category: Classes in C++

Destructors are used for reinitializing the objects which are initialized by the constructors. With the help of destructors, the objects are destroyed. This special member function is called automatically when an object is destroyed. A Destructor function also has the same name as that of the class but it is preceded by a ~, symbol.

 
by Dinesh Thakur Category: Classes 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.

 
by Dinesh Thakur Category: Classes in C++

Member of a class can be accessed only through the object of a class. The members are accessed as follows:

 
by Dinesh Thakur Category: Classes 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

 
by Dinesh Thakur Category: 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.



 
by Dinesh Thakur Category: Classes in 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.

 
by Dinesh Thakur Category: Classes in C++

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.

 
by Dinesh Thakur Category: Classes 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.



 
by Dinesh Thakur Category: Classes 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.

 
by Dinesh Thakur Category: Classes 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.

 

Page 1 of 6



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles