Category: Concepts of Classes and Objects in C++

Interested in learning C Programming? Click here...


Array of Objects in Cpp 0

Array of Objects in C++

We know that an array can be of any data type. Hence, we can also have arrays of variables that are of the type class. Such variables are called array of objects. Consider the following class definition:

The identifier employee is a user-defined data type and can be used to create objects that relate to different categories of the employee. For example, consider the following:

In above declaration, the array managers contains three objects (managers), namely, managers[0], managers[1] and managers[2], of the type employee class. Similarly, the foremen array contains 15 objects (foremen) and the workers array contains...

Friend Functions and Friend Classes in Cpp 0

Friend Functions and Friend Classes in C++

We have been emphasizing that the private members cannot be accessed from outside the class. That is, a non-member function cannot have an access to the private data of a class. However, there could be a situation where we would like two classes to share a particular function. For example, consider a case where two classes, manager and scientist, have been defined. We would like to use a function income() to operate on the objects of both these classes. In such situations, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to...


Class Objects as Function Arguments-Parameters 1

Class Objects as Function Arguments (Parameters)

Like any other data-type, an object may be used as a function argument. This can be done in two ways: A copy of the entire object is passed to the function Only the address of the object is transferred to the function The first method is called pass-by-value. Since a copy of the object is passed to the function, any changes made to the object inside the function do not affect he object used to call the function. The second method is called pass-by-reference. When an address of the object is passed, the called function works directly on the actual...

Static Member Functions of Class in Cpp 0

Static Member Functions of Class in C++

Like static member variable, we can also have static member functions in a class. A member function that is declared static has the following properties: A static member function can have access to only other static members (functions or variables) declared in the same class A static member function can be called using the class name (instead of its objects) as follows:

Program below illustrates the implementation of these characteristics:

Output of the above program would be:

In the above program: The static member function showcount() displays the number of objects created till that moment A count...