After learning the OOPS concepts, let's now learn how to declare a class. Declaring a class is somewhat similar to declaring a structure in C. The difference is that in a structure all the members are "public" by default whereas in a class all the members are by default "private". Another difference is that a structure can only contain member variables but a class can not only contain variables but also functions or methods.
Syntax
class classname
{
members...
};
Example
class vehicle
{
int vehno;
char vehtype[12];
void getveh()
{}
void showveh()
{}
};
Description :
"vehicle" is the name of the class, which can be any logical name within the rules of declaration.Inside the class there are four members - int vehno, char vehtype,void getveh() and void showveh(). A class can have as many members as required. Generally a class should have at least two functions - one for initialising the member variables and another for printing the output. Declaration of other functions depends on the need of the program. The class body must end with a ";" which is called the "class terminator".
Example 1 -- The student class
class student
{
private:
int admno;
char sname[15];
char grade;
public:
void get_data()
{
cout<<"Adm. No. ";
cin>>admno;
cout<<"Student Name ";
cin>>sname;
cout<<"Grade ";
cin>>grade;
}
void show_data()
{
cout<<"Adm. No. ";
cout<<"Name of the student ";
cout<<"Grade ";
}
};
Description
The student class has 3 member variables -- int admno,char sname[15] and char grade. The class also has 2 methods "get_data()" and "show_data()". The member variables have the "private" access specifier while the methods have a "public" visibility. Why ? The reason for this is a concept called "data hiding". A general user of this program will either need to intialise the data variables or print their values. For both the purposes we have already defined the functions "get_data()" and "show_data()". If the user wants to initialise the variable he can call the "get_data()" method and to print the same the second method can be called. Now, when both the tasks are being performed by the methods why should we allow the variable to come out of the class. This is why we have specified them as "private". The variables can be accessed from within the class but to access them from outside - either of the two methods can be used.
Why not public ?
It can be questioned here that what is the harm in specifing the variables as public also. The answer is "prevention of accidental modification". If the variables are also specified as "public" there are chances that their values are accidently modified. For example before making a call to the method "show_data()", if the user writes a statement like "admno=0", the second method will print 0 and not the value entered by the user. By creating public methods we have created a proper channel through which the variables can be initialised and printed. The "private" specifier helps us in specifying the accessiblility of the variables making them inaccessible from outside.
Note : "endl" denotes "end line" and is used to print different outputs on different lines as "\n" in C.
Using the "student" class
After declaring a class, we now need to call or access its members. To access a class member we must first create an object. As mentioned earlier an object is an instance of class. Once we declare an object we can call the members of the class using "objectname.membername" syntax. Take a look at the following example which is in continuation with the last example.
Example 1 -- Using the student class
class student
{
private:
int admno;
char sname[15];
char grade;
public:
void get_data()
{
cout<<"Adm. No. ";
cin>>admno;
cout<<"Student Name ";
cin>>sname;
cout<<"Grade ";
cin>>grade;
}
void show_data()
{
cout<<"Adm. No. ";
cout<<"Name of the student ";cout<<"Grade ";
}
};
void main()
{
student objstud;
objstud.get_data()
objstud.show_data();
}
Description
In the main(), we have created an object name "objstud". Remeber, that a class is an user defined dataytype therefore to use it a variable of the class should be defined. After declaring the variable (object, to be precise), we can access the class members using the syntax like objstud.get_data(). The above program will first ask the user to enter the student data and then will print the same.
0 comments:
Post a Comment