***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

Monday, April 26, 2021

Defining member functions

The class Member functions can be defined in two ways.

1. Inside the class

2. Outside the class.

1. Inside the class:

In this method, a class member functions are defined inside the class definition, that is, function declaration or prototype is replaced with function definition inside the class. 

Syntax:-

class classname

{

private:

datatype var1;

datatype var2;

-----------

public:

return_type fun_name([args])

{

-------------

}

};

Example:

#include<iostream.h>

#include<conio.h>

class emp

{

private:

int eno;

char name[15];

float sal;

public:

void getdata()

{

cout<<"Enter Number : ";

cin>>eno;

cout<<"Enter Name: ";

cin.ignore();

cin.getline(name,15);

cout<<"Enter Salary : ";

cin>>sal;

}

void show()

{

cout<<eno<<"   "<<name<<"    "<<sal<<endl;

}

 

2. Outside the class:

In C++, a class member functions are also defined in outside the class. In this method, the class member functions are declared inside the class and the member functions are defined in outside the class.

For this, C++ provides the scope resolution operator ( : : ).

Syntax:- 

class classname                                                                                                                                    

{                                                                                                                                                          

public:                                                             

return_type fun_name( args_list);                                                                                                       

-----------                                                                                                                                               

};                                                                                                                                                

return_type classname : : fun_name( args_list)                                                                          

{                                                                                                                                                 

---------                                                                                                                                        

statements;                                                                                                                                 

}                                                                                                                                                 

                                                                                                                           

Example:

#include<iostream.h>

#include<conio.h>

class emp

{

private:

int eno;

char name[15];

float sal;

public:

void getdata();

void show();

};

void emp::getdata()

{

cout<<"Enter Number : ";

cin>>eno;

cout<<"Enter Name  : ";

cin.ignore();

cin.getline(name,15);

cout<<"Enter Salary : ";

cin>>sal;

}

void emp::show()

{

cout<<eno<<"  "<<name<<"  "<<sal<<endl;

}

void main()

{

clrscr();

emp e;

e.getdata();

e.show();

getch();

}

No comments:

Post a Comment