Classes and Objects....cont(1)

An object is a region of storage with associated semantics. After the declaration int i;

we say that " i is an object of type int ". In C++, "object" usually menas "an instance of a class". Thus, a class defines the behaviour of many objects(instances). For example:

class Myclass

{

int a;

public;

void getval(int);

void display();

};

In a class declaration, the object-list is optional. In the above example, the declaration of Myclass did not define any object of type Myclass. It only defines the type of object. To declare an object, the class name is used as a type specifier. For example:

Myclass ob1, ob2; // declaring objects of Myclass

Referencing Class Memebers

The private data of a class can be accessed only though the member functions of that class. The function main() cannot access private members of a class directly. The following is the syntax for accessing a public member function of a class:

object_name.function_name(actual arguments);

We can refer to any of the public members of an object as if they were normal functions or variables, by simply writing the object's name followed by the dot operator and then the class member (like we did it in C++ structures). for example:

ted.set_value(3,4); // ted is the object of class Newclass

sum=ted.add();

But we will not be able to refer to x nor y since they are private members of the class. They can be reffered from other members of the same class.

Class Method DefinitionWhen designing a class, we design a new abstract data type (ADT) that can be treated like any other built-n type.

The class method (function) definition provides the code for the member functions. there are two ways of defining a member function.

1. Outside the class definition.

2. Inside the class definition.

In both the cases, the code for the fnction body is identical. However there is a difference in the way the function header is defined.

Outside the Class Definition

A member function definition outside the class definition is the same as a normal function definition. The only difference s that the name of the function is the qualified name(full name) of the function. The qualified full name can be written as

class-name::function-name;

whaere,

class-name indicates that the function specified by function-name is a member of the class specified by class-name. the scope operator:: included in the definition is used to declare a member of a class outside it.

The scope operator(::) allows to specify the class to which the member declared belongs, granting exactly the same scope properties as if it were defiend directly within the class.

Inside the Class Definition

When a member function is defined inside a class, the function definition is similar to the normal function definition. A membership labelis not required here with the function name. A function declared inside the class definition is automatically considered inline

The inline property

The inline property is a hint for the compiler to insert the code for the body of the function(already declared inline) at the place where it is called. It saves the overheads (the time spent in loading and unloading the called function) involved in the function call.

HOME LEARN C++

PREVIOUS NEXT