learncpponline

Private Member Functions in C++

Although it is normal practice to place all the data items in a private section and all the functions in public, some situations may require certain functions to be hidden (like private data) from the outside calls. Tasks such as deleting an account in a customer file, or providing an increment to an employee are events of serious consequences and therefore the functions handling such tasks should have restricted access. We can place these functions in the private section of a class. A private member function can only be called by another function that is a member of its class. Even an object cannot invoke a private function using the dot operator. For example, consider a class as defined below: class sample { private: int m; void read(); //private member function public: void update(); void write(); }; If a1 is an object of class sample, then: a1.read(); is illegal. However,...

Accessing Class Members in C++

As pointed out earlier, the private data of a class can be accessed only through the member functions of that class. The main() cannot contain statements that access number and cost (of class item) directly. However, the member functions which are public, can be accessed from main(). The following is the format for calling a public member function: object-name.function-name(actual arguments); For example, x.getdata(100,75.6); The function call statement (in case of class item discussed above) is valid and assigns the value 100 to number and 75.6 to cost of the object x by implementing the getdata() function. The assignments occur in the actual function. Similarly, the statement: x.putdata() would display the values of data members. Remember, a member function can be invoked only by using an object of the same class. Hence the statement like: getdata(100, 78.9); has no meaning. Similarly, the statement: x.number = 100; is also illegal. This is...

Creating Objects of Classes in C++

Remember that the declaration of item class as shown before does not define any objects (i.e., variables) of item but only specifies what they will contain. Once a class has been declared, we can create an variable of that type by using the class name (like any other built-in type variable). For example: item x; creates a variable x of type item. In C++, the class variables are known as objects. Therefore, x is called an object of type item. We may also declare more than one object in one statement link: item x, y, z; The declaration of the object is similar to that of a variable of any basic type. Objects can also be created when a class is defined by placing their names immediately after the closing brace, as we do in the case of structures. That is, the definition: class item { … … }x,y,z; would...

Defining of Member Functions of Class in C++

Member functions can be defined at any one of the following two places: Outside the Class Inside the Class It is obvious that, irrespective of the place of definition, the function should perform the same task. Therefore, the code for the function body would be identical in both the cases. However, there is a subtle difference in the way the function header is defined. Both these approaches are discussed in detail as follows: Outside the Class Definition: Member functions that are declared (with the help of prototypes) inside a class have to be defined separately outside the class. Their definitions are very much like the normal functions. They should have a function header and a function body.However, an important difference between a member function and a normal function is that a member function incorporates a membership “Identity label” in the header. This labels tells the compiler which class the function...

Defining of Class in C++

Class in C++ is almost similar to structures. The only difference being that classes have functions too, as their members, apart from different types of variables. There is also one another difference between classes and structures. Such difference is that the members of a class are private. by default, while, the members of a structure are public, by default. The meanings of private members and public members will get clearer to you as move ahead with the tutorial. Defining or Declaration of Class: A class is a way to bind the data and its associated functions together. It allows the data (and functions) to be hidden, if necessary, from external use. While defining a class, we are creating a new abstract data type that can be treated like any other built-in data type. Generally, a class specification in C++ has two parts: Class declaration Class function definitions The class declaration...

Understanding the program in C++

In this article we will understand the program written in C++ programming language in step-by-step manner. Consider the below simple C++ program that performs multiplication of two integers and displays the result. /*Program that performs multiplication of two integers and displays the result*/ //Include header files #include <iostream> #include “conio.h” using namespace std; //main program starts from here void main() { //declare variables int result, variable1, variable2; //assign values to variable1 and variable2 variable1 = 10; variable2 = 2; /*Perform multiplication of variable1 and variable2. Store the result in the variable named result*/ result = variable1 * variable2; //Display the result in the output window cout << “The result is: ” << result << endl; } Result of the above program: The result is: 20 Let us understand the above program in detail: Line 1 thru 2: This is a multiline comment. We have used it to describe the purpose...