Example : A basic C++ program to show class-object concepts?
#include<iostream>
using namespace std;

class employee
{
    public:
      int salary1;
      int salary2;
};

int main()
{
    employee emp1;
    employee emp2;    
    
    emp1.salary1=450;
    emp1.salary2=210;    
   
    emp2.salary1=85;
    emp2.salary2=23;

    cout <<" First Object salary is : "<< emp1.salary1 << " and " << emp1.salary2<<endl;
    cout <<" Second Object salary is : "<< emp2.salary1 << " and " << emp2.salary2;
    
   return 0;
}

Output : 
First Object salary is : 450 and 210
Second Object salary is : 85 and 23

NB: main() is always defined after class to declare object inside it that needs class name.

/* ---------------------------  OR  --------------------------- */

#include<iostream>
using namespace std;

class employee
{
    public:
    	int salary1=5600;
    	int salary2=6300;
};

int main()
{
    employee emp1;
    employee emp2;     

    cout <<" First object salary is : "<< emp1.salary1 << " and " << emp1.salary2<<endl;
    cout <<" Second object salary is : "<< emp2.salary1 << " and " << emp2.salary2;
    
   return 0;
}

Output : 
 First object salary is : 5600 and 6300
 Second object salary is : 5600 and 6300

/* ---------------------------  OR  --------------------------- */

#include<iostream>
using namespace std;

class A
{
    public:
      int x;
      int y;    
      int z;   
};

int main()
{
    A a;       
   
    a.x=10;
    a.y=20;
    
    a.z=a.x+a.y;
    cout<<"The addition result is = "<<a.z;    
    
   return 0;
}

/* ---------------------------  OR  --------------------------- */

#include<iostream>
using namespace std;

class A
{
    public:
      int x;
      int y;
    private:
      int z;
    public:
    	void process()
    	{
    	    z=x+y;
	}
	void output()
	{
	    cout<<"The addition result is = "<<z;	
	}
};

int main()
{
    A a;   
    a.x=10;
    a.y=20;

    a.process() ;
    a.output() ;    
    
   return 0;
}

Output:
The addition result is = 30

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std; 
class user
{
    public:
        void message()
        {
            cout <<"Welcome U All in Codershelpline";
        }
}; 
int main()
{
    user x;     
    x.message();     
    return 0;
}

Output:
Welcome U All in Codershelpline

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;

class student 
{		
    public:
  	int rollno ;
  	char sname[30];
};

int main() 
{
    student stu={34,"Robert"};
    
  	//cout<<stu.rollno<<"  "<<stu.sname;
	cout<<stu.rollno <<endl <<stu.sname;   
    return 0;
}

Output :
34
Robert
Example : A typical C++ program to show class-object concepts.
#include <iostream>
using namespace std;
class student
{
	private:		
		char  sname[50];
		int   srollno;		
		float cfee;	
			
	public:	
		
		void input()
		{
			cout << "Enter student name: " ;
			cin >> sname;
			cout << "Enter student roll number: ";
			cin >> srollno;
			cout << "Enter student course fee : ";
			cin >> cfee;	
		}
		
		void output()
		{
			cout<<endl<< "Student details are :\n";
			
			cout<< "Student Name is = "<< sname <<endl; 
			cout<< "Student Roll Number is = " << srollno <<endl; 
			cout<< "Student Course Fee is = " << cfee; 	
		}
};

int main()
{
	student stu;	
	
	stu.input();
	stu.output();
		
	return 0;
}

Output:
Enter student name: Saini
Enter student roll number: 123
Enter student course fee : 2500

Student details are :
Student Name is = Saini
Student Roll Number is = 123
Student Course Fee is = 2500

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class student
{
	public:		
		
		void output()
		{
			cout<<endl<< "Student details are :\n";
			
			cout<< "Student Name is = "<< sname <<endl; 
			cout<< "Student Roll Number is = " << srollno <<endl; 
			cout<< "Student Course Fee is = " << cfee; 	
		}
		
		void input()
		{
			cout << "Enter student name: " ;
			cin >> sname;
			cout << "Enter student roll number: ";
			cin >> srollno;
			cout << "Enter student course fee : ";
			cin >> cfee;	
		}
		
	private:		
		char  sname[50];
		int   srollno;		
		float cfee;
			
};

int main()
{
	student stu;	
	
	stu.input();
	stu.output();
		
	return 0;
}

Output:
Enter student name: Navya
Enter student roll number: 34
Enter student course fee : 2400

Student details are :
Student Name is = Navya
Student Roll Number is = 34
Student Course Fee is = 2400

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;				
		}
				
		void process()
		{
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{	
			cout<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};
int main()
{
	calculation stu;
	
	stu.input();	
	stu.process();		
	stu.output();
		
	return 0;
}

Output:
Enter first Number : 100
Enter second number: 50
The addition result is = 150
The subtraction result is = 50
The multiplication result is = 5000
The division result is = 2
Example Using a class-object concept, a C++ program to show parameterized member functions/methods.
#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input(int n1, int n2)
		{
			num1=n1;
			num2=n2;				
		}
				
		void process()
		{
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{	
			cout<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};
int main()
{
	calculation stu;
	
	stu.input(100,10);	
	stu.process();		
	stu.output();
		
	return 0;
}

Output:
The addition result is = 110
The subtraction result is = 90
The multiplication result is = 1000
The division result is = 10

--------------  OR  ----------------

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input(int n1, int n2)
		{
			num1=n1;
			num2=n2;				
		}
				
		void process()
		{
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{	
			cout<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};

int main()
{
	calculation stu;
	int m,n;
	cout<<"Enter two values = "<<endl;
	cin>>m>>n;
	stu.input(m,n);	
	stu.process();		
	stu.output();
		
	return 0;
}

Output:
Enter two values =
20
10
The addition result is = 30
The subtraction result is = 10
The multiplication result is = 200
The division result is = 2
Example : A C++ program to show nesting of member functions/methods using a class-object concept.
#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;				
		}
				
		void process()
		{
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{	
			input();
			
			process();
			
			cout<<endl<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};
int main()
{
	calculation stu;
	
	//stu.input();	
	//stu.process();		
	stu.output();
		
	return 0;
}

Output:
Enter first Number : 500
Enter second number: 100

The addition result is = 600
The subtraction result is = 400
The multiplication result is = 50000
The division result is = 5

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;				
		}
				
		void process()
		{
			input();
			
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{	
			process();
			
			cout<<endl<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};
int main()
{
	calculation stu;
	
	//stu.input();	
	//stu.process();		
	stu.output();
		
	return 0;
}

Output:

Enter first Number : 500
Enter second number: 100

The addition result is = 600
The subtraction result is = 400
The multiplication result is = 50000
The division result is = 5

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;
			
			process();				
		}
				
		void process()
		{	
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
			
			output();
		}		
			
		void output()
		{			
			cout<<endl<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}	
};
int main()
{
	calculation stu;
	
	stu.input();	
	//stu.process();		
	//stu.output();
		
	return 0;
}

Output:

Enter first Number : 500
Enter second number: 100

The addition result is = 600
The subtraction result is = 400
The multiplication result is = 50000
The division result is = 5

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;						
		}
				
		void process()
		{	
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{			
			cout<<endl<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}
		
		void all()
		{
			input();
			process();
			output();	
		}	
};
int main()
{
	calculation stu;
	
	stu.all();	
		
	return 0;
}
Output:

Enter first Number : 500
Enter second number: 100

The addition result is = 600
The subtraction result is = 400
The multiplication result is = 50000
The division result is = 5

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class calculation
{
	private:			
		int num1,num2,sum,subtract,mult,div;
						
	public:			
		void all()
		{
			input();
			process();
			output();	
		}
		
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> num1;
			cout << "Enter second number: ";
			cin >> num2;						
		}
				
		void process()
		{	
			sum=num1+num2;
			subtract=num1-num2;
			mult=num1*num2;
			div=num1/num2;
		}		
			
		void output()
		{			
			cout<<endl<<"The addition result is = "<<sum<<endl;
			cout<<"The subtraction result is = "<<subtract<<endl;
			cout<<"The multiplication result is = "<<mult<<endl;
			cout<<"The division result is = "<<div;
		}		
			
};
int main()
{
	calculation stu;
	
	stu.all();	
		
	return 0;
}

Output:

Enter first Number : 500
Enter second number: 100

The addition result is = 600
The subtraction result is = 400
The multiplication result is = 50000
The division result is = 5
Example : A C++ program to show member functions/methods with return type using a class-object concept.
#include <iostream>
using namespace std;
class addition
{
	private:			
		int x,y,z;				
	public:				
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> x;
			cout << "Enter second number: ";
			cin >> y;				
		}
						
		int process()
		{
			z=x+y;
			return z;			
		}

};
int main()
{
	addition stu;
	int p;

	stu.input();
	p=stu.process();
	cout<<"The addition result is = "<<p;	
			
	return 0;
}

Output :
Enter first Number : 10
Enter second number: 20
The addition result is = 30

----------------  OR  ----------------

#include <iostream>
using namespace std;
class addition
{
	private:			
		int x,y,z;				
	public:				
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> x;
			cout << "Enter second number: ";
			cin >> y;				
		}
						
		int process()
		{
			z=x+y;
			return z;			
		}
};

int main()
{
	addition stu;
	
	stu.input();	
	cout<<"The addition result is = "<<stu.process();	
			
	return 0;
}

Output:
Enter first Number : 100
Enter second number: 50
The addition result is = 150

/* ---------------------------  OR  --------------------------- */

#include <iostream>
using namespace std;
class addition
{
	private:			
		int x,y,z;				
	public:				
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> x;
			cout << "Enter second number: ";
			cin >> y;				
		}
						
		int process()
		{
			z=x+y;
			return z;			
		}
		
		void output()
		{
			cout<<"The addition result is = "<<process();		
		}		
		
};
int main()
{
	addition stu;
	
	stu.input();
	stu.process();	
	stu.output();
			
	return 0;
}

Output :
Enter first Number : 10
Enter second number: 20
The addition result is = 30
Example : A C++ program to pass an object as arguments through a member function.
#include <iostream>
using namespace std;

class number 
{
	public:
   		int num = 210;
   		float val=23.12;
   		char ch='X';
   	
   void display(number obj2)
   {
      cout<<obj2.num<<endl;      
      cout<<obj2.val<<endl;
      cout<<obj2.ch;
   }
};

int main() 
{
   number obj1;
   obj1.display(obj1);
   
   return 0;
}

Output:
210
23.12
X
Example : A C++ program to return an object and print/display through a member function.
#include <iostream>
using namespace std;

class student 
{
   public:
	
     string sname;
     int srollno;
     float cfee;
     
  
   student input(int num1, float num2, string str)
   {
      student stu1;
      
      stu1.sname = str;
      stu1.srollno = num1;
      stu1.cfee = num2;      
      
      return stu1;    //retrun object
   }
   
   void output(student stu2)    //object as parameter
   {
      cout<<"Student Name: "<<stu2.sname<<endl;
      cout<<"Roll No. : "<<stu2.srollno<<endl;
      cout<<"Student Course Fee : "<<stu2.cfee<<endl;
   }
};

int main() 
{
   student stu3,stu4;
   
   stu4 = stu3.input(10130, 3500, "Mr.Ashok");
   
   stu3.output(stu4);
      
   return 0;
}

Output :
Student Name: Mr.Ashok
Roll No. : 10130
Student Course Fee : 3500

---------------------  OR  -----------------------

#include <iostream>
using namespace std;

class number 
{
   public:
 	int num = 210;
 	float val=23.12;
 	char ch='X';
   	
   number display(number obj2)
   {
      return obj2;
   }
};

int main() 
{
   number obj1, obj3;
   obj3=obj1.display(obj1);
   cout<<obj3.num<<" "<<obj3.val<<" "<<obj3.ch;
   return 0;
}

Output:

210 23.12 X
Example : A C++ program to show class-object concepts using an array of objects.
#include <iostream>
using namespace std;
class student
{
	private:		
		char  sname[50];
		int   srollno;		
		float cfee;	
			
	public:		
		void input()
		{
			cout << "Enter student name: " ;
			cin >> sname;
			cout << "Enter student roll number: ";
			cin >> srollno;
			cout << "Enter student course fee : ";
			cin >> cfee;
			
			cout<<endl;	
		}
		
		void output()
		{
			cout<<endl;	
			cout<< "Student Name is = "<< sname <<endl; 
			cout<< "Student Roll Number is = " << srollno <<endl; 
			cout<< "Student Course Fee is = " << cfee;
			
			cout<<endl;	 	
		}
};

int main()
{
	student stu[3];	   // Array of object
	int i;
		
	for(i=0;i<3;i++)
	{
	   stu[i].input();		
	}
	
	cout<<endl<< "Student details are :\n";	
	for(i=0;i<3;i++)
	{
	   stu[i].output();		
	}
		
	return 0;
}
Output:
Enter student name: Reyansh
Enter student roll number: 52
Enter student course fee : 1200

Enter student name: Shreyansh
Enter student roll number: 23
Enter student course fee : 1300

Enter student name: Sonaya
Enter student roll number: 67
Enter student course fee : 1400


Student details are :

Student Name is = Reyansh
Student Roll Number is = 52
Student Course Fee is = 1200

Student Name is = Shreyansh
Student Roll Number is = 23
Student Course Fee is = 1300

Student Name is = Sonaya
Student Roll Number is = 67
Student Course Fee is = 1400
Example : A C++ program to show class-object concept using scope resolution operator (::)?
// Accessing Global Variables

#include <iostream>
using namespace std;

int x = 10; // Global variable

int main() 
{
    int x = 20; // Local variable
    cout << "Local variable value is : " << x << endl;
    cout << "Global variable value is : " << ::x << endl; // Using scope resolution to access the global variable
    return 0;
}

Output:
Local variable value is : 20
Global variable value is : 10

--------------  OR  -------------

// Defining Class Members Outside the Class

#include <iostream>
using namespace std;
class student
{
	private:		
		char  sname[50];
		int   srollno;		
		float tfee;		
	public:		
		void input();		
		void output();		
};
void student::input()
{
	cout << "Enter student name: " ;
	cin >> sname;
	cout << "Enter student roll number: ";
	cin >> srollno;
	cout << "Enter student tution fee : ";
	cin >> tfee;	
}
void student::output()
{
	cout<<endl<< "Student details are :\n";
			
	cout<< "Student Name is = "<< sname <<endl; 
	cout<< "Student Roll Number is = " << srollno <<endl; 
	cout<< "Student Course Fee is = " << tfee;
}

int main()
{
	student stu;	
	stu.input();
	stu.output();	
	return 0;
}

Output:
Enter student name: Reyansh
Enter student roll number: 84
Enter student course fee : 1100

Student details are :
Student Name is = Reyansh
Student Roll Number is = 84
Student Course Fee is = 1100

--------------  OR  -------------

// Accessing Namespace Members

#include <iostream>
namespace MyNamespace   //creation of namespace
{
    int value = 500;
    void display() 
    {
        std::cout << "Namespace value is : " << value << std::endl;
    }
}

int main() 
{
    MyNamespace::display();  // Accessing namespace member
    return 0;
}

Output:
Namespace value is : 500

--------------  OR  -------------

//Accessing Static Members of a Class

#include <iostream>
using namespace std;

class Example 
{
  public:
    static int count;    // Static member declaration
    static void increment() 
    {
        count=count+5;
    }
};

// Define the static member outside the class
int Example::count = 0;

int main() 
{
    cout << "Original value count: " << Example::count << endl;
    
    Example::increment();
    cout << "First Value after increment: " << Example::count << endl;
    
    Example::increment();
    cout << "Second Value after increment: " << Example::count << endl;
    return 0;
}

Output:
Original value count: 0
First Value after increment: 5
Second Value after increment: 10

--------------  OR  -------------

//Resolving Ambiguities in Inheritance
#include <iostream>
using namespace std;

class Parent1 
{
    public:
    void show() 
	{ 
	  cout << "Parent1 Class Mehtod Executed" << endl; 
	}
};

class Parent2 
{
    public:
    void show() 
	{ 
	  cout << "Parent2 Class Method Executed" << endl; 
	}
};

class Derived : public Parent1, public Parent2 
{
    public:
    void show() 
    {
        Parent1::show();    // Call Parent1's show()
        Parent2::show();    // Call Parent2's show()5
    }
};

int main() 
{
    Derived obj;
    obj.show();
    return 0;
}

Output:
Parent1 Class Mehtod Executed
Parent2 Class Method Executed

--------------  OR  -------------

//Accessing Global Functions
#include <iostream>
using namespace std;

void display()   // Global function declaration
{
    cout << "Global Function Executed" << endl;
}

int main() 
{
    void display();  // Local function declaration
    {
    	cout<<"Local Function Executed";
    }
    cout<<"\n";
	
    ::display();    // Calls the global function    
    
    return 0;
}

Output:
Local Function Executed
Global Function Executed
Example : A C++ program to show a class object having an array of objects with a scope resolution operator (::).
#include <iostream>
using namespace std;

class student
{
	private:		
		char  sname[50];
		int   srollno;		
		float cfee;	
			
	public:		
		void input();		
		void output();		
};

void student::input()
{
	cout << "Enter student name: " ;
	cin >> sname;
	cout << "Enter student roll number: ";
	cin >> srollno;
	cout << "Enter student course fee : ";
	cin >> cfee;
	
	cout<<endl;	
}

void student::output()
{
	cout<<endl;
	
	cout<< "Student Name is = "<< sname <<endl; 
	cout<< "Student Roll Number is = " << srollno <<endl; 
	cout<< "Student Course Fee is = " << cfee;

	cout<<endl;
}

int main()
{
	student stu[3];	
	int i;
		
	for(i=0;i<3;i++)
	{
	   stu[i].input();		
	}
	
	cout<<endl<< "Student details are :\n";
	for(i=0;i<3;i++)
	{
	   stu[i].output();		
	}	
	return 0;
}

Output:
Enter student name: Shreyansh
Enter student roll number: 45
Enter student course fee : 3100

Enter student name: Reyansh
Enter student roll number: 34
Enter student course fee : 1200

Enter student name: Romana
Enter student roll number: 89
Enter student course fee : 1400


Student details are :

Student Name is = Shreyansh
Student Roll Number is = 45
Student Course Fee is = 3100

Student Name is = Reyansh
Student Roll Number is = 34
Student Course Fee is = 1200

Student Name is = Romana
Student Roll Number is = 89
Student Course Fee is = 1400
Example : A C++ program to create & use objects from another class.
#include <iostream>
using namespace std;
class student1
{
	private:			
		int x1,y1,z1;				
	public:			
		void input1()
		{
			cout <<endl<<"Enter first Number in class 1 : " ;
			cin >> x1;
			cout << "Enter second number in class 1 : ";
			cin >> y1;				
		}		
		void process1()
		{
			z1=x1+y1;
		}		
		void output1()
		{
			cout<<"The addition result of class 1 is = "<<z1<<endl;
		}	
};

class student2
{
	private:		
		student1 stu1;			
		int x2,y2,z2;
				
	public:			
		void input2()
		{
			cout << "Enter first Number in class 2 : " ;
			cin >> x2;
			cout << "Enter second number in class 2 : ";
			cin >> y2;
			
			stu1.input1();				
		}
		
		void process2()
		{
			z2=x2+y2;

			
			stu1.process1();
		}	
		
		void output2()
		{
			cout<<endl<<"The addition result of class 2 is = "<<z2<<endl;			
			stu1.output1();
		}	
};

int main()
{
	student2 stu2;
	
	stu2.input2();
	stu2.process2();
	stu2.output2();		
	return 0;
}
Example : A C++ program to represent the friend function concept.
#include<iostream>
using namespace std;

class frifun
{
	int a,b;
	public:
	void read()
	{
		a=90;
		b=70;
	}	
	friend void display(frifun obj5);
};
	void display(frifun obj2)
	{
		cout<<obj2.a<<"\n"<<obj2.b;
	}

int main ()
{
	//clrscr();
	frifun obj1;
	obj1.read();
	display(obj1);
	return 0;
}
Example : A C++ program in class-object to represent friend class concept.
#include <iostream>  
using namespace std;  
class A  
{  
    int x =15;  
    friend class B;           // friend class declaration.  
};  
class B  
{  
  public:  
    void display(A &m)  
    {  
        cout<<"value of x is = "<<m.x;  
    }  
};  
int main()  
{  
    A a;  
    B b;  
    b.display(a);  
    return 0;  
} 

Output :
 value of x is = 15

/* -------------------  OR  -------------------- */

#include <iostream>  
using namespace std;
  
class A  
{  
    int x,y,z; 
	
    friend class B;
    friend class C;  
    friend class D;         // friend class declaration.  
};
 
class B  
{  
  public:	
    void input(A &m)  
    {  
        m.x=100;
        m.y=200;
    } 	 
};

class C  
{  
  public:	
    void process(A &n)  
    {  
        n.z=n.x+n.y;
    }	 
};
 
class D  
{  
  public:	
     void display(A &p)  
    {  
        cout<<"value of x is = "<<p.z;  
    }	 
}; 
 
int main()  
{  
    A a;  
    B b;
    C c;
    D d;
	
    b.input(a);
    c.process(a); 
    d.display(a);  
    return 0;  
} 

Output:
value of x is = 300

/* -------------------  OR  -------------------- */

#include <iostream>  
using namespace std;  
  
class Master  
{  
    int val=40;
	  
    friend class Servant; // Class Servant is friend of Master class  
};
  
class Servant 
{  
    public:  
    void display(Master &m)  
    {  
        cout<<"The Value is : "<<m.val;  
    }  
};
 
int main()  
{  
    Master M;  
    Servant S;  
    S.display(M);
	  
    return 0;  
}
Output :
The Value is : 40 

/* -------------------  OR  -------------------- */

#include <iostream>
using namespace std;

class Master 
{
private:
   char name[10]="Owner";
   int amt = 4300;
   
public:  
   friend class Servant;
};

class Servant 
{
public:
   void output(Master m)
   {
      cout<<m.name<<endl;
      cout<<m.amt<<endl;
   }
};
int main() 
{
   Master M;
   Servant S;
   
   S.output(M);
   return 0;
} 

Output :
Owner
4300
Example : An example of a C++ program to show an inline function.
#include <iostream>
using namespace std;

class Calculator 
{
   public:
     inline int add(int a, int b) 
       {
         return a + b;
       }
};

int main() 
{
    Calculator calc;
    cout << "Sum: " << calc.add(5, 10) << endl;
    return 0;
}

-------------  OR  --------------  

#include<iostream>
using namespace std;
class Inlin
{
    public:
	inline int info(int y)
	{
	   return y*y;
	}
};

int main ( )
{
   Inlin obj;
   int x;
   cout << "\n Enter the Input Value: ";
   cin>>x;
   cout << "\n The Output is: " <<obj.info(x);
   return 0;
}

-------------  OR  --------------

#include<iostream>
using namespace std;

int info(int p);

int main ( )
{
   int x;
   cout << "\n Enter the Input Value: ";
   cin>>x;
   cout << "\n The Output is: " <<info(x);
   return 0;
}

inline int info(int y)
{
   return y*y;
}

Output : 
Enter the Input Value: 6
The Output is: 36

-------------  OR  --------------

#include<iostream>
using namespace std;

inline int info(int y)
{
   return y*y;
}

int main ( )
{
   int x;
   cout << "\n Enter the Input Value: ";
   cin>>x;
   cout << "\n The Output is: " <<info(x);
   return 0;
}
Example : An example of a C++ program to show static variables/data members.
//Local Static Variable

#include <iostream>
using namespace std;

void Example() 
{
    static int count = 0; // Local Static Variable
    count=count+1;
    cout << "The Count value is : " << count << endl;
}

int main() 
{
    Example();
    Example();
    Example();
    return 0;
}

Output:
The Count value is : 1
The Count value is : 2
The Count value is : 3

--------------  OR  ---------------

// Global Static variable
#include <iostream>
using namespace std;

static int count = 5; // Global Static variable

void Example() 
{
    
    count=count+1;
    cout << "The Count value is : " << count << endl;
}

int main() 
{
    Example();
    Example();
    Example();
    return 0;
}

Output:
The Count value is : 6
The Count value is : 7
The Count value is : 8

--------------  OR  ---------------

// Static Member Variables

#include<iostream>
using namespace std;

class stacon
{
	static int c;

	public:
	void count()
	{
	c=c+1;
	cout<<c<<" ";
	}
};
	int stacon::c=7;
	
int main ()
{
	//clrscr();
	stacon obj1,obj2;
	
	obj1.count();
	obj2.count();
	obj1.count();
	obj2.count();
	
	return 0;
}

Output :
8 9 10 11

---------------------------  OR  -------------------------

// Static Member Variables

#include<iostream>
using namespace std;

class stacon
{
	static int c;   //static member variable
	int x=0;        //non-static member variable

	public:
		
	void count()
	{
	  c=c+1;
	  x=x+1;
	  cout<<c<<"\t";
	  cout<<x<<" "<<"\n";
	}
};
	int stacon::c=7;
	
int main ()
{
	//clrscr();
	stacon obj1,obj2;
	
	cout<<"Static"<<"\t"<<"Non-static"<<endl;	
	obj1.count();
	obj2.count();
	obj1.count();
	obj2.count();
	
	return 0;
}

Output:
Static  Non-static
8       1
9       1
10      2
11      2
Example : A C++ program in class object to show static function/member function.
//Static Function

#include <iostream>
using namespace std;

static void Example() 
{
    cout << "Static Member Function Executed";
}
int main() 
{
    Example();
    return 0;
}

-----------  OR  ------------

//Static Member Function

#include<iostream>
using namespace std;

class stacon
{
	static int c;
	static int x;

	public:
	static void count()
	{
	  c=c+1;
	  x=x+1;
	}
	
	static void display()
	{
	  cout<<c<<"\t";
	  cout<<x<<" "<<"\n";	
	}
};
	int stacon::c=7;
	int stacon::x=0;
	
int main ()
{
	//clrscr();
	//No need to create object.
	stacon::count();
	stacon::display();

	stacon::count();
	stacon::display();
	return 0;
}

Output :
8   1
9   2
Example : An example of a C++ program showing function overloading.
// Different Number of Parameters

#include <iostream>
using namespace std;

class A 
{
	int x;
	int y;
        int z;	
	
	public:
  	void display(int a)
  	{
           x=a;
           cout<<x<<endl;
  	}

  	void display(int p, int q)
  	{
           x= p;
	   y= q;
	   cout<<x<<" "<<y;
	   cout<<endl;
  	}

    void display(int p, int q, int r)
  	{
           x= p;
	   y= q;
           z= r;
	   cout<<x<<" "<<y<<" "<<z;
  	}
};

int main() 
{
  A obj;
  
  obj.display(40);  
  obj.display(10,20);
  obj.display(100,200,300);

  return 0;
}

Output :
40
10 20
100 200 300

------------------  OR  ------------------

// Different type of parameters

#include <iostream>
using namespace std;

class A 
{
	int x1;
	float y1;
	double z1;	
	
	public:
  	void display(int a)
  	{
           x1=a;
           cout<<x1<<endl;
  	}

  	void display(float p)
  	{
           y1= p;	   	
	   cout<<y1<<endl;
  	}
  	
  	void display(double m)
  	{
           z1= m;	   	
	   cout<<z1<<endl;
  	}
};

int main() 
{
  A obj;  
  obj.display(40);
  
  obj.display(10.28);
  
  obj.display(41.01123);
  return 0;
}

Output:
40
10.28
41.0112

------------------  OR  ------------------

// Different Order of Parameters

#include <iostream>
using namespace std;

class A 
{
	int x1;
	float y1;
	char z1;	
	
	public:
  	void display(int a, float b, char c)
  	{
        x1=a;
        y1=b;
        z1=c;
        cout<<x1<<" "<<y1<<" "<<z1<<endl;
  	}

  	void display( float b, char c, int a)
  	{
        y1=b;
        z1=c;
        x1=a;
        cout<<y1<<" "<<z1<<" "<<x1<<endl;
  	}
  	
  	void display( char c, int a, float b)
  	{
        z1=c;
        x1=a;
        y1=b;
        cout<<z1<<" "<<x1<<" "<<y1<<endl;
  	}
};

int main() 
{
  A obj;  
  obj.display(40,10.28,'D');  
  obj.display(10.28,'D',40);  
  obj.display('D',40,10.28);
  return 0;
}
Output:
40 10.28 D
10.28 D 40
D 40 10.28

Loading

Categories: C++

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.