Class DeclarationObjected – oriented programming constructs modeled out of data types called classes. Defining variables of a class data type is known as a class instantiation and such variables are called objects. A cla ss encloses both the data and functions that operate on the data, into a single unit. A class is a way to bind the data and its associated functions together. It allows the data to be hidden, if necessary from external use. When 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 has two parts
? Class declaration.
? Class function definitions.
The general form of a class declaration is
The functions and variables which declared inside the class are collectively called members. They are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public . The keywords private and public are known as visibility labels.
The members that have been declared as private can be accessed only from within the class. On the other hand, public members can be accessed from outside the class also.
The data hiding is the key feature of object-oriented programming. The use of the keyword private is optional. By default, the members of a class are private . If both the labels are missing, then, by default, all members are private. Such a class is completely hidden from the outside world and does not serve any purpose.
The variables declared inside the class are known as data members and the functions are known as member functions. Only the member functions can have access to the private data members and private functions. However, the public members can be accessed from outside the class.
The binding of data and functions together into a single class -type variable is referred to as Encapsulation.
Class ObjectsThe idea of creating a class is to serve as blueprint for creating objects. A class defines a new data type. This new data type can be used to create objects of that type. The class is a template and the object is a physical entity. The necessary resources are allocated only when a class is instantiated. The syntax for defining
objects of a class is shown below.
[class] Class_name Object_name1,…..
Example
Stud S1,S2,S3;
Here the s1,s2 and s3 are objects and the ‘stud’ is the class name The definition of an object is similar to that of a variable of any primitive data type. Objects can also be created by placing their names immediately after the closing brace like in the creation of the structure variables. Thus the definition is
Class student
{
…………….
…………….
}s1,s2,s3,s4;
An object is a conceptual entity possessing the following properties:? It is identifiable
? It has features that span a local state space.
? It has operations that can change the status of the system locally, while also
inducing operations in peer objects.
? It refers to a thing, either a tangible or a mental construct, which is
indentifiable by the users of the target system.
Accessing Class MembersOnce an object of a class has been created, there must be provision to access its members. This is achieved by using the member access operator, called dot (.). The syntax for accessing members of a class is shown as
Object_Name.Data_Member
(a) Syntax for accessing data members of a class
Object_Name.Function_Name(Actua l_argument)
(b) Syntax for accessing member function of a class
Sample Program#include <iostream.h>
Class stud
{
private:
int rno;
float avg;
public:
void setdata(int r, float a)
{
rno = r;
avg = a;
}
void showdata(void)
{
cout<<rno<<endl;
cout<<avg<<endl;
}
};
void main()
{
stud A;
A.setdata();
A.showdata();
}
Defining Member FunctionsThe data members of a class must be declared within the body of the class, whereas the member functions of the class can be defined in any one of the following steps:
? Outside the class specification
? Inside the class specification
Outside the class specification
Member functions that are declared 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.
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 ‘label’ tells the compiler which class the function belongs to. The general form
Return_type class-name : : function_name(arguments)
{
function body;
………..
};
The label class-name:: informs the compiler that the function_name is the member of the class class-name. The scope of the function is restricted to only the objects and other members of the class.
The member functions have some special characteristics that are often used in the program development.
? Several different classes can use the same function name. The ‘membership
label’ will resolve their scope.
? Member functions can access the private and of the class. A non-member
function cannot do so.
? However this is exception for friend function.
? A member function can call another member function directly, without using
the dot operator.
? Member functions defined as public act as an interface between the service
provider and the service seeker.
? A class can have multiple member functions with the same name as long as
they differ in terms of argument specification.
Sample program
#include <iostream.h>
class bank
{
int cno;
float amt;
public:
void setdata(int x, float y);
void display(void);
};
void bank::setdata(int x, float y)
{
cno = x;
amt = y;
}
void bank::display(void)
{
cout<<”Number is “<<cno;
cout<<”Amount is “<<amt;
}
void main()
{
bank B;
B.setdata(10,4350);
B.display();
}
Inside the Class Definition
Another method of defining a member function is to replace the function declaration by the actual function definition inside the class. The example is Sample Program
#include <iostream.h>
class sample
{
int a,b;
public:
void setab(void)
{
cout<<”Enter the value for a”;
cin>>a;
cout<<”Enter the value for b”;
cin>>b;
}
void showab(void)
{
cout<<”a value is”<<a<<”b value is “<<b<<endl;
}
};
void main()
{
sample s;
s.setab();
s.showab();
}