CLASSES
Consider a Student. His characteristics will be Roll Number, Name, Class(I mean Standard), etc. He will have some functions like Reading Writing and Playing. Combing all the characteristics and functions into a single Unit will form a 'CLASS'.
First starts with a keyword 'class'. with a class_name and the Braces will Contain the entities and the associated Functions with it. Semicolon at the End.
Example:
class Student
{
int roll_no;
char name[20];
void show()
{
cout<<"Roll No: "<< roll_no;
cout<<"\nName: "<<name;
}
};
The above example contains variables roll_no, char string name and function 'show'. All are combined in a Single Unit. This implements Abstraction and Encapsulation.
Class has three members: private, public and protected.
Private: which we don't want anyone to inherit and don't want to show to the Outside World. Variables and Functions declared in this section are protected from the Direct Outside Access. If no Label is specified then by Default, members are Private.
Public: it's Members can be Accessed form Outside the Class.
Protected: It's members are accessed inside the Class and also from the class who inherit them.
NOTE: Friend class can access the private and protected members of the other Class.
No comments:
Post a Comment