Introduction about Class in C++ with example



Class – It is a group of objects having some common attributes and behaviors.
Object - It is an identifiable entity which has some characteristics and behaviors.
Every Class has three parts
Name of the class – It is a unique identity for representing an object of a class.
Attributes - It is a representation of attributes by internal data structure.
Behaviors - It is a set of allowed operations, functions and methods.
Example:
class user_define_name
                              {
                                             Data members
                                             Member function
};


The keyword private, protected, and public are used to specify the three levels of access protection for hiding data and function member internal to class.
A)     Private – A member data can only be accessed by the member function and friends of this class.
B)     Protected - A member data can only be accessed by the member function and friends of this class. And also, these function can be accessed by the member function and friends derived from the class. 
C)     Public – Can be accessed by any function out of the class.

Example:
#include<iostream.h>
# include<conio.h>
class student
               {
                              private :
int id;
char name[20];
public:
void get();
void put();           
};
void student :: get()
{
cout<<”Enter the Student Info=”;
cin>>id>>name;    
}
void student :: put()
{
               cout<<”Student Info”;
               cout<<id<<name;
}
void main()
{
clrscr();
student st;
st.get();
st.put();
getch();
}
 

No comments:

Post a Comment