Classesobjects
Classesobjects
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.
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;
}