Page 82 - DCAP404 _Object Oriented Programming
P. 82
Unit 4: Classes and Objects
second two; Notes
int sum()
//error, scope of the member function is not defined
{
return (x+y);
}
Both classes in the above case are defined with the same member function names. While accessing
these member function, it gives an error. The scope of the member function sum( ) is not defined.
When accessing the member function sum( ), control will be transferred to both classes one and
two. So the scope resolution operator (::) is absolutely necessary for defining the member
functions outside the class declaration.
int one:: sum( ) // correct
{
return (x+y);
}
Self Assessment
Fill in the blanks:
6. Member functions is only declared inside the class but …………………… outside the class.
7. The type of member function arguments must exactly match with the types declared in
the……………………. .
8. Only …………………….. identifies the function as a member of a particular class.
9. Without this scope operator, the function definition would create an ordinary function,
subject to the usual function rules to access an……………….. .
4.3 Creating Class Objects
Objects are the basic run-time entities in an object-oriented system. They may represent a person,
a place, a bank account, a table of data; they may also represent user-defined data such as vectors,
time and lists.
Figure 4.2: Example of an Object
LOVELY PROFESSIONAL UNIVERSITY 75