0% found this document useful (0 votes)
5 views6 pages

Classesobjects

The document explains how to define member functions in C++ classes, including inline definitions and definitions using the scope resolution operator. It provides several program examples demonstrating public and private member functions, multiple object creation, and nesting of member functions. The examples illustrate how to manage student details using class structures and member functions effectively.

Uploaded by

gopikha.ilakkiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Classesobjects

The document explains how to define member functions in C++ classes, including inline definitions and definitions using the scope resolution operator. It provides several program examples demonstrating public and private member functions, multiple object creation, and nesting of member functions. The examples illustrate how to manage student details using class structures and member functions effectively.

Uploaded by

gopikha.ilakkiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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;
s1.name="gopi";
s1.rollno=2340;
s1.dept="IT";
cout<<"Name "<<s1.name<<endl;
cout<<"Roll_NO:"<<s1.rollno<<endl;
cout<<"Dept:"<<s1.dept;
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;
s1.name="gopi";
s1.rollno=2340;
s1.dept="IT";
s1.details();
cout<<"Name "<<s1.name<<endl;
cout<<"Roll_NO:"<<s1.rollno<<endl;
cout<<"Dept:"<<s1.dept;
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;
s1.details("gopi",345,"IT");
s1.showdata();
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;
s1.details("gopi",345,"IT");
s1.showdata();
s2.details("Radha",3499985,"CSE");
s2.showdata();
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;
s1.details("Gopi",345,"IT");
s1.showdata();
s2.details("Radha",3499985,"CSE");
s2.showdata();
return 0;
}

You might also like