Category: Concepts of Classes and Objects in C++

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: class employee { char name[30]; float age; public: void getdata(); void putdata(); }; 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: employee managers[3]; //array of manager employee foremen[15]; //array of foreman employee workers[75]; //array of worker In above declaration, the array managers 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: class-name :: function-name; Program below illustrates the implementation of these characteristics: #include <iostream.h> class test { private: int code; static int count; //static member variable public: void setcode() { code = ++count; } void showcode() { cout <<...