Class and object
Class
A class is
use define datatype which bind the data and member function together .
It allows
the data and member functions to be hidden if necessary from external use.
The internal
data of class is called as data member and functions are called as member
function. public member can access outside the class .
The variable
or instance of class is called as objects.
The only difference
between structur and class is that by
default member of class is private while by default member of structure is
public.
Syntax:
Class
classname
{
Private:
Datatype declaration
;
Function declaration
;
Public:
data-type declaration
;
Function declaration
;
};
Eg :
class committee
{
int roll no;
char name[20];
public:
void accept(int
a, char b);
void display();
};
Access specifiers
:
1] Public : -
The member which can be declare in public section can access from outside the class the public member is an interface between the outside word and private
The member which can be declare in public section can access from outside the class the public member is an interface between the outside word and private
member, using object
of class we can access the public member.
2]Private :
-
The member which can be declare in private section we can access that within the class . the private member can not access from outside the class.
The member which can be declare in private section we can access that within the class . the private member can not access from outside the class.
The member function
and friend of this class can access this member.
3]Protected :-
The protected member is same like private member only benefit is that it provided one additional benefit that they can be accessed in child classes which are called derived classes.
The protected member is same like private member only benefit is that it provided one additional benefit that they can be accessed in child classes which are called derived classes.
Object of
class :-
The access member of class we
required on object by using that we can access data member as well as member
function.
When we create an object of class memory is allocated ,constructor is called implicitly and memory get initialize.
This process of object creation is
called as instantiation .
Syntax:
Int main()
{
Class name
object name;
}
Or we can
also create object after class end
Class class
name {
} object;
Inside the
class:
Lets see how
to define function inside the class.
class student{
int roll no;
char
name[20];
public:
void
getdata()
{
Cout<<”enter
roll no and name ”;
Cin>>rollno>>name;
}
void putdata()
{
cout<<rollno<<name;
}
};
Outside the
class :
The function
can be define after the class declaration however the prototype of function
should be appear
inside the class.
Syntax :
return type class name :: function name
{
- - - -
- - - -
}
#include
<cstdlib>
#include<iostream>
using
namespace std;
class
student
{
int roll ;
char name[20];
public:
void getdata()
{
roll=23;
cout<<"enter roll num and
name"<<endl;
cin>>roll>>name;
}
void putdata()
{
cout<<roll<<name;
}
};
int main()
{
student ob;
ob.getdata();
ob.putdata();
return 0;
}
*****************************************************
Output
enter roll
num and name
1 rohan
1 rohan
Array of object
The array of
variable of class data type is called array of object .
Example :
If class is
student in a class so we define array of
student with 10 objects.
class Student
{
Int roll no;
char name[20];
public:
void
getdata();
void
putdata();
};
The array of
object is created as follows s[10];
We can
access array of object
student ob[10];
ob[i].getdata;
ob[i].pudata;
Program for
accept and display student info using
array of object.
#include
<cstdlib>
#include<iostream>
using
namespace std;
class
student
{
int roll;
char name[100];
public:
void getdata();
void putdata();
};
void
student::getdata()
{
cout<<"enter roll number::
";
cin>>roll;
cout<<"enter the name:: ";
cin>>name;
}
void
student::putdata()
{
cout<<"roll no ::
"<<roll<<"\t";
cout<<"name is ::
"<<"\t"<<name<<"\n";
}
int main(int
argc, char** argv) {
int i,n;
student s[10];
cout<<"enter no of
student:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].getdata();
}
for (i=0;i<n;i++)
{
s[i].putdata();
}
return 0;
}
****************************************
Output
enter no of
student:2
enter roll
number:: 1
enter the
name:: A
enter roll
number:: 2
enter the
name:: B
roll no ::
1 name is :: A
roll no ::
2 name is :: B
So you might
think how it sored in array so below is image show how this information stored in array using
object.
Comments
Post a Comment