In C++, a member function is a function that is declared within a class and operates on the objects of that class. Member functions are associated with objects of the class and have access to the class's private and protected members. They define the behavior of the class and allow objects to perform specific tasks or manipulate their internal state.
Member functions can be defined within the class declaration itself or outside the class declaration using the scope resolution operator (::) to specify the class to which they belong. They can have access specifiers such as public, private, or protected, determining their accessibility from outside the class hierarchy.
#include <iostream> using namespace std; class MyClass { private: int data; // Private member variable public: // Member function to set the value of data void setData(int value) { data = value; } // Member function to get the value of data int getData() { return data; } // Member function to display data void displayData() { cout << "Data: " << data << endl; } }; int main() { MyClass obj; // Creating an object of class MyClass obj.setData(10); // Setting the value of data cout << "Data retrieved using getData(): " << obj.getData() << endl; // Retrieving the value of data obj.displayData(); // Displaying data using displayData() return 0; }
#include <iostream> using namespace std; class MyClass { private: int data; // Private member variable public: // Declaration of member functions void setData(int value); int getData(); void displayData(); }; // Definition of member function setData() outside the class void MyClass::setData(int value) { data = value; } // Definition of member function getData() outside the class int MyClass::getData() { return data; } // Definition of member function displayData() outside the class void MyClass::displayData() { cout << "Data: " << data << endl; } int main() { MyClass obj; // Creating an object of class MyClass obj.setData(10); // Setting the value of data cout << "Data retrieved using getData(): " << obj.getData() << endl; // Retrieving the value of data // Accessing the member function defined outside the class obj.displayData(); // Displaying data using displayData() return 0; }
Note : The :: symbole is know as scope resolution operator.
Each member function is defined using the ClassName:: scope resolution operator before the function name, followed by the function's definition.
Nesting member functions refers to the practice of defining one member function within another member function within a class. This can be useful for encapsulating functionality that is closely related or for breaking down complex tasks into smaller, more manageable parts.
#include<iostream> using namespace std; class sample { int a,b; public: void getData() { cout<<"Enter the value of a and b:"<<endl; cin>>a>>b; return; } int sum() { return (a+b); // it will return int value from where it call. } int diff() { return (a-b); } void display(void); // Member Function Declaration. }; void sample::display(void) // Definitionqq { cout<<"The first Number :"<<a<<endl; cout<<"The second Number :"<<b<<endl; cout<<"The sum of two numbers are :"<<sum()<<endl; cout<<"The differences between two numbers are:"<<diff()<<endl; return; } int main() { sample obj; obj.getData(); obj.display(); }
Enter the value of a and b: 12 15 The first Number :12 The second Number :15 The sum of two numbers are :27 The differences between two numbers are:-3
In C++, you can define an array within a class just like any other member variable. Here's an example demonstrating how to define an array within a class:
#include <iostream> using namespace std; class A { int n, i, a[45]; public: void getData(); int biggest(); void show(); }; void A::getData() { cout << "Enter the size of an array :" << endl; cin >> n; cout << "Enter the " << n << " elements\n"; for (i = 0; i < n; i++) { cin >> a[i]; } } int A::biggest() { int i, big = a[0]; for (i = 1; i < n; i++) { if (a[i] > big) { big = a[i]; } } return big; } void A::show() { cout << "The big one is :" << biggest(); } int main() { A obj; obj.getData(); obj.show(); }
Enter the size of an array : 3 Enter the 3 elements 5 6 8 The big one is :8
In C++, you can create an array of objects within a class just like you would create any other array. Here's an example to read the record for more than one student to define an array of objects within a class:
#include <iostream> using namespace std; class A { // private: int roll; // default scope is private char name[45]; public: void getValue(); // Function prototype void showValue(); }; void A::getValue() // function defination { cout << "Enter the roll number:" << endl; cin >> roll; cout << "Enter the name :" << endl; cin >> name; } void A::showValue() // function defination { cout << "The Roll : " << roll << endl; cout << "The name : " << name << endl; } int main() { A obj[50]; // Array of an object int size, i; cout << "Enter the size of an array :" << endl; cin >> size; cout << "Enter the record of a student :" << endl; for (i = 0; i < size; i++) { obj[i].getValue(); } cout << "All Records are :" << endl; for (int i = 0; i < size; i++) { obj[i].showValue(); // function calling } }
Enter the size of an array : 2 Enter the record of a student : Enter the roll number: 101 Enter the name : Sachin Enter the roll number: 102 Enter the name : Anil All Records are : The Roll : 101 The name : Sachin The Roll : 102 The name : Anil
Passing objects as function arguments in C++ allows you to operate on those objects within the function, modifying their state or using their properties to perform some operation.
#include <iostream> using namespace std; class Abc { int a, b; // private public: void setValue() { a = 20; b = 45; } // Parameterized function geting an object as parameter int sum(Abc x); // Declaration of a function }; int Abc::sum(Abc x) { return (x.a + x.b); } int main() { Abc s; // Object created s.setValue(); cout << s.sum(s); }
#include <iostream> using namespace std; class Student { int roll; char name[45]; public: void getData(); void display(); }; void Student ::getData() { cout << "Enter the Roll no : " << endl; cin >> roll; cout << "Enter the name :" << endl; cin >> name; } void Student ::display() { cout << "Roll No : " << roll << endl; cout << "Name : " << name << endl; } void swap(Student &s1, Student &s2) { Student temp = s1; s1 = s2; s2 = temp; } int main() { Student stu1, stu2; cout << "Enter the Record for student 1 :" << endl; stu1.getData(); cout << "Enter the Record for student 2 :" << endl; stu2.getData(); cout << "Record before swaping :" << endl << endl; cout << "The record of Student 1 :" << endl; stu1.display(); cout << "The record of Student 2 :" << endl; stu2.display(); swap(stu1, stu2); // Normal function cout << "Record After swaping :" << endl << endl; cout << "The record of Student 1 :" << endl; stu1.display(); cout << "The record of Student 2 :" << endl; stu2.display(); }
Enter the Record for student 1 : Enter the Roll no : 101 Enter the name : Sachin Enter the Record for student 2 : Enter the Roll no : 102 Enter the name : Anil Record before swaping : The record of Student 1 : Roll No : 101 Name : Sachin The record of Student 2 : Roll No : 102 Name : Anil Record After swaping : The record of Student 1 : Roll No : 102 Name : Anil The record of Student 2 : Roll No : 101 Name : Sachin
Returning objects in C++ refers to the practice of returning an instance of a class from a function. This allows functions to generate and provide objects to the caller for further use. Use the return statement to return the object from the function
#include <iostream> using namespace std; class sample { int x; public: void getdata(int a) { x = a; } void display() { cout << x; // c.x= 30 } sample sum(sample b) { sample c; // x space alocated for c object c.x = x + b.x; return (c); } }; int main() { sample obj1, obj2, obj3; obj1.getdata(10); // obj1.x=10; obj2.getdata(20); // obj2.x=20; obj3 = obj1.sum(obj2); cout << "Sum ="; obj3.display(); return 0; }
Sum =30
#include <iostream> using namespace std; class Sample { private: int x; public: void setData(int a) { x = a; } void display() const { cout << x; } // Return by reference Sample& sum(const Sample& b) { x += b.x; return *this; } }; int main() { Sample obj1, obj2, obj3; obj1.setData(10); obj2.setData(20); obj3 = obj1.sum(obj2); // Assign the result of sum operation directly to obj3 cout << "Sum = "; obj3.display(); return 0; }
Sum = 30
#include <iostream> using namespace std; class A { // private: int roll; // default scope is private char name[45]; public: void getValue(); // Function prototype void showValue() const; }; void A::getValue() // function defination { cout << "Enter the roll number:" << endl; cin >> roll; cout << "Enter the name :" << endl; cin >> name; } void A::showValue() const // Constant function { // roll=67; // its just read only // can't change in value cout << "The Roll : " << roll << endl; cout << "The name : " << name; } int main() { A obj; obj.getValue(); // function Calling cout << "Output :" << endl; obj.showValue(); // function calling }