Classes and Ojects

The :: Scope Resolution Operator
The :: scope resolution operator specifies the scope to which a member belongs. It has this general form:
name::member-name
Here, name is the name of the class or namespace that contains the member specified by member-name. Put differently, name specifies the scope within which can be found the identifier specified by member-name.
To reference the global scope, you do not specify a scope name. For example, to refer to a global variable called count that is being hidden by a local variable called count, you can use this statement:
::count
The scope resolution operator is not supported by C.
#include< iostream.h >
#include< conio.h >
class student
{
char name[20];
int age;
public:
void getdata(void);
void show(void);
};
void student :: getdata(void)
{
cout << " enter name and age";
cin >> name >> age;
}
void student :: show(void)
{
cout<< "Name ="<< name<< endl;
cout<< "Age =" << age;
}
void main()
{
clrscr();
student rec;
rec.getdata();
rec.show();
getch();
}
Output : -
Name = anuj
Age = 38
The program defines student as a new data of type class. The class student includes two basic data type items and two functions to operate on that data. These functions are called member functions.
If we declare members of a class before including any permission label, the members are considered private, since it is the default permission that the members of a class declared with the class keyword acquire.
Example:-
#include< iostream.h >
#include< conio.h >
class Rectangle
{
int width, height;
public:
void values (int,int);
int area()
{
return width*height;
}
};
void Rectangle::values (int x, int y)
{
width = x;
height = y;
}
int main()
{
clrscr();
Rectangle rect,rectb;
rect.values(3,4);
rectb.values (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
cout << "area: " << rect.area();
getch();
return 0;
}
Output: -
area : 12
area : 30
Notice that the call to rect.area() does not give the same result as the call to rectb. area(). This is because each object of class CRectangle has its own variables x and y, and its own functions value() and area().
On that is based the concept of object and object-oriented programming. In that data and functions are properties of the object, instead of the usual view of objects as function parameters in structured programming. In this and the following sections we will discuss advantages of this methodology.
C++ has a rich set of operators. All C operators are valid in C++ also. In addition, C++ introduces some new operators. We have already seen two such operators, namely, the insertion operator <<, and the extraction operator >>. Other new operators are:
::    Scope resolution operator
::*     Pointer-to-member declaration
->*     pointer-to-member operation
.*     Pointer-to-member operation
delete     Memory release operator
endl     Line feed operator
new     Memory allocation operator
setw     Field width operator
Understanding Scopes and Variable Lifetimes
C and C++ define scope rules, which govern the visibility and lifetime of objects. Although there are several subtleties, in the most general sense, there are two scopes: global and local.
The global scope exists outside all other scopes. A name declared in the global scope is known throughout the program. For example, a global variable is available for use by all functions in the program. Global variables stay in existence the entire duration of the program.
A local scope is defined by a block. That is, a local scope is begun by an opening brace and ends with its closing brace. A name declared within a local scope is known only within that scope. Because blocks can be nested, local scopes, too, can be nested. Of course, the most common local scope is the one defined by a function. Local variables are created when their block is entered, and destroyed when their block is exited. This means that local variables do not hold their values between function calls. You can use the static modifier, however, to preserve values between calls.
#include< iostream.h >
#include< conio.h >
int m=10;
int main()
{
int m=20;
{
int k = m;
int m = 30;
clrscr();
cout << " We are in inner block \n";
cout << "K = " << k << "\n";
cout << "m = " << m << "\n";
cout << "::m = " << ::m << "\n";
}
cout << "\n We are in outer block \n";
cout << "m = "<< m <<"\n";
getch();
return 0;
}
We are in inner block
K = 20
m = 30
::m = 10
We are in outer block
m = 20
Member Dereferencing Operators
C++ permits us to define a class containing various types of data and functions as members. C++ also permits us to access the class members through pointers. C++ provides a set of three pointer to member operators.

Member Dereferencing Operators
Operator Function
::* To declare a pointer to a member of a class
* To access a member using object name and a pointer to that member
->* To access a member using a pointer to the object and a pointer to that member

Nesting of Member Functions
A member funciton can be called by using its name inside another member function of the same class. This is known as nesting of member fuctions.
#include < iostream.h >
#include < conio.h >
class set
{
int m,n;
public:
void input(void);
void display(void);
int largest(void);
};
int set :: largest(void)
{
if (m >= n)
return(m);
else
return(n);
}
void set :: input (void)
{
cout << "input values of M and N" << "\n";
cin >> m >> n;
}
void set :: display (void)
{
cout << "largest value" << largest() << "\n";
}
int main()
{
set a;
a.input();
a.display();
return 0;
}
input values of M and N 12 14 largest value 14


Free Web Hosting