CLASS:
In oop,
class is a user defined data type that can hold data and their associated
functions into single unit.
The class members are divided into two types:
1. Data Member
2. Function Member
In class
there are certain privilege for accessing both data and function members. They
are said to be access specifiers.
It is divided into three categories. They are,
1.Public
2.private
3.Protected
By
default the data members are private. So the private members of a class are not
accessible from outside the class. If the data members are specified as public
then the members of a class can be accessible from inside and outside of the
class.
Syntax of a class
class class_name
{
Access Specifier:
Data Members
Access Specifier:
Function Members
};
Example:
Consider
an example of class class student
{
public:
int
rollno,age; Data Members
char *name;
void
getdetail()
{
Cin>>rollno>>age;
Cin>>name;
}
Function Members
void
printdetail()
{
Cout<<rollno<<age;
Cout<<name;
}
};
In this
example a class named student with data members such rollno, age and name and
with function members getdetail() and printdetail() is defined within the
class.The access specifiers of this class is public.
Function Definition outside the Class
The
function members which are declared inside the class can be defined outside of
the class using Scope Resolution
Operator ::
Syntax for Defining the Function outside the Class:
Return Type class_name::function_name()
{
Function Body
}
Return
type specifies the type of a function. Class name specifies the name of a class
in which the function belongs. The operator:: denotes scope resolution
operator. Function name specifies the name of a function.
Example:
class
student
{
public:
int
rollno,age; char *name;
void
getdetail();
void
printdetail();
};
void
student::getdetail()
{
Cin>>rollno>>age;
Cin>>name;
}
void
student::printdetail()
{
Cout<<rollno<<age;
Cout<<name;
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.