DEFINING MEMBER FUNCTIONS
Member functions of a class can be defined in two ways:
1. Inside the class (Inline Definition)
2. Outside the class (Using the Scope Resolution Operator ::)
Program Pattern1: ( FUNCTIONS INSIDE CLASS)
***************
#include <iostream>
using namespace std;
class student
{
public:
string name;
int rollno;
string dept;
void details()
{
cout<<"Welcome to IT";
}
};
int main()
{
student s1;
[Link]="gopi";
[Link]=2340;
[Link]="IT";
cout<<"Name "<<[Link]<<endl;
cout<<"Roll_NO:"<<[Link]<<endl;
cout<<"Dept:"<<[Link];
return 0;
}
OUTPUT
*******
Program pattern 2: (FUNCTIONS OUTSIDE CLASS)
****************
#include <iostream>
using namespace std;
class student
{
public:
string name;
int rollno;
string dept;
void details();
};
void student::details()
{
cout<<"Welcome to IT"<<endl;
}
int main()
{
student s1;
[Link]="gopi";
[Link]=2340;
[Link]="IT";
[Link]();
cout<<"Name "<<[Link]<<endl;
cout<<"Roll_NO:"<<[Link]<<endl;
cout<<"Dept:"<<[Link];
return 0;
}
OUTPUT:
Private Member Functions
Private member functions are functions that are declared within the private section of a
class.
These functions cannot be accessed from outside the class directly.
They can only be called by other member functions of the same class.
PROGRAM 3: (Private Access Modifiers)
#include <iostream>
using namespace std;
class student
{
private:
string name;
int rollno;
string dept;
public:
void details(string x,int y,string z)
{
name=x;
rollno=y;
dept=z;
}
void showdata()
{
cout<<"STUDENT DETAILS"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Rollno:"<<rollno<<endl;
cout<<"Dept:"<<dept;
}
};
int main()
{
student s1;
[Link]("gopi",345,"IT");
[Link]();
return 0;
}
OUTPUT:
MULTIPLE OBJECT CREATION:
#include <iostream>
using namespace std;
class student
{
private:
string name;
int rollno;
string dept;
public:
void details(string x,int y,string z)
{
name=x;
rollno=y;
dept=z;
}
void showdata()
{
cout<<"STUDENT DETAILS"<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Rollno:"<<rollno<<endl;
cout<<"Dept:"<<dept<<endl;
}
};
int main()
{
student s1,s2;
[Link]("gopi",345,"IT");
[Link]();
[Link]("Radha",3499985,"CSE");
[Link]();
return 0;
}
OUTPUT:
Nesting of Member Functions in C++
Nesting of member functions in C++ refers to calling one member function from
another member function within the same class.
A member function can call another member function directly inside the same class.
This technique helps in structuring the logic efficiently.
Private member functions are commonly used in nested calls, as they provide helper
functionality.
Program 5:
*********
#include <iostream>
using namespace std;
class student
{
private:
string name;
int rollno;
string dept;
void display()
{
cout<<"Welcome to IT"<<endl;
cout<<"***************"<<endl;
public:
void details(string x,int y,string z)
{
name=x;
rollno=y;
dept=z;
}
void showdata()
{
display();
cout<<"Name:"<<name<<endl;
cout<<"Rollno:"<<rollno<<endl;
cout<<"Dept:"<<dept<<endl;
}
};
int main()
{
student s1,s2;
[Link]("Gopi",345,"IT");
[Link]();
[Link]("Radha",3499985,"CSE");
[Link]();
return 0;
}