learncpponline

Why use Constructor and Destructor in C++

We have seen, so far, a few examples of classes being implemented. In all the cases, we have used member functions such as getdata() and setvalue() to provide initial values to the private member variables. For example, the following statement: X.input(); invokes the member function input(), which assigns initial values to the data items of the object X. Similarly, the statement: A.getdata(100,299.95); passes the initial values as arguments to the function getdata(), where these values are assigned to the private variables of object A. All these ‘function call’ statements are used with the appropriate objects that have already been created. These functions cannot be used to initialise the member variables at the time of creation of their objects. This means that we are not able to initialize a class type variable (object) when it is declared, much the same way as initialization of an ordinary variable. For example: int p...

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 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 75 objects (workers). Since, an array of objects behave like any other array, we can use the usual array-accessing methods to access individual elements...

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 have access to the private data of these classes. Such a function need not be a member of any of these classes. To make an outside class “friendly” to a class, we have to simply declare this function as a friend of the class as illustrated below: class ABC {...

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 object used in the call. This means that nay changes made to the object inside the function will reflect in the actual object. The pass-by-reference method is more efficient since it requires to pass only the address of the object and not the entire object. Program given below illustrates the...

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 << “Object number: ” << code << endl; } static void showcount() //static member function { cout << “Count: ” << count << endl; } }; int test :: count; void main() { test t1, t2; t1.setcode(); t2.setcode(); test :: showcount(); //accessing static function test t3; t3.setcode(); test :: showcount(); //accessing...

Static Data Members in C++

A data member of a class can be qualified as static (storage-class). The properties of a static member variable are similar to that of a C static variable. Read more about storage classes in C (Click here) In addition, a static member variable (of a class) has certain special characteristics which are detailed as follows: It is initialized to zero when the first object of its class is created. No other initialization is permitted Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created It is visible only within the class, but its lifetime is the entire program Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurrences of all the objects....