Concepts of Classes and Objects in C++

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...