CLASS AND OBJECT
CLASS:- CLASS IS ABSTRACT USER DEFINED DATA TYPE
CLASS IS WAY
OF BINDING DATA AND METHOD TOGETHER
CLASS IS
GROUP OF SIMILAR KIND OF OBJECTS
Defining a
class in c++
Student
|
Roll
Name
Age
Marks
|
Void get() // to accept student details
Void show()// to display student details
|
If we have
to define class in C++ for the above class represented in diagram we can define
like this
#include<iostream.h>
#include<conio.h>
class
student
{
int
roll,age,marks; // by default data are private, private data
are not accessible out side class
char
name[20];
public: // functions are public
means it will be accessible outside the class using object of class
void get()
{
cout<<"\n
enter roll";
cin>>roll;
cout<<"\n
enter name";
cin>>name;
cout<<"\n
enter age";
cin>>age;
cout<<"\n
enter roll";
cin>>marks;
}
void
show()
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
void
main()
{
student s;
s.get();
s.show();
getch();
}
Here
function is defined inside the class. We can also define function outside the
class as give below
#include<iostream.h>
#include<conio.h>
class
student
{
int
roll,age,marks; // by default
data are private, private data are not accessible outside class
char
name[20];
public: // functions are
public means it will be accessible outside the class using object of class
void
get(); // function prototype
void
show();// function prototype
};
void
student::get() // function is define using scope resolution operator ,
student::get() mean get of student class
{
cout<<"\n
enter roll";
cin>>roll;
cout<<"\n
enter name";
cin>>name;
cout<<"\n
enter age";
cin>>age;
cout<<"\n
enter roll";
cin>>marks;
}
void
student::show() // function
is defined using scope resolution operator
, student::show() mean show of student class
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
void
main()
{
student s;
s.get();
s.show();
getch();
Access modifiers :- (or access specifiers)
are keywords in object-oriented
languages that set the accessibility of classes, methods,
and other members.
1. Public
:- accessible every where
2. Protected
:- accessible inside the class and in derived class
3. Private:-
accessible only by member function of class.
Using class in main Program
Class can be used in main program by declaring
object
Since private members are not accessible outside
object can access only public member
As shown in given example
void
main()
{
student s; // s is object of class student
s.get(); // accessing get function(using
.dot operator) of student class as it is public
s.show(); // accessing show function(using
.dot operator) of student class as it is public
getch();
}
Array of
object
Student s
can store details of one student. If we have to store details of more than one
student we can create array of student object like
Student s[5];
We can
call the function student[i].get()
Example
void
main()
{
student s[5]; //five object of class student
for(i=0;i<5;i++)
{
S[i].get();
}
for(i=0;i<5;i++)
{
S[i].show();
}
getch();
}
}
No comments:
Post a Comment