Class & Object
In
side of class we define the member function definitions they are two type s to
declare the class &object.
· Inside of class
· Outside of class
Definition of member function
Member functions of a class can be defined either
outside the class definition or inside the class definition. In both the cases, the function body
remains the same.
1. Inside the Class:
A member function of a class can also be defined
inside the class. when a member function is defined inside the class, the class
name and the scope resolution operator are not specified in the function
header. The member functions defined inside a class definition are by default
inline functions.
Syntax:
class MCA
{
private:
int a,b;
public:
void getdata();
void display()
{
cout<<a<<b;
}
};
2. Outside the Class:
Defining a member function outside a
class requires the function declaration (function prototype) to be provided
inside the class definition. The member function is declared inside the class
like a normal function. This declaration informs the compiler that the function
is a member of the class and that it has been defined outside the class. After
a member function is declared inside the class, it must be defined (outside the
class) in the program.
Syntax:
class
MCA
{
private:
int
a,b;
void
getdata()
};
void
MCA :: getdata()
{
cout<<”input
two no”;
cin>>a>>b;
}
Program:
#include<iostream.h>
#include<conio.h>
class
MCA
{
private:
int
a,b,c,d;
public:
void
getdata();
void
display() //inside class
{
cout<<”d=”<<d;
}
};
void
MCA :: getdata() //outside class
cout<<”input
three value”;
cin>>a>>b>>c;
d=a*b*c;
}
void
main()
{
MCA x;
x.getdata();
x.getdisplay();
getch();
}
Output:
Input
three value:
4
4
5
d=80
No comments:
Post a Comment