NB : These programs are compiled & executed in Dev C++ 5.11 application.

Example : A basic C++ program to show class-object concept ?
#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 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 concept .
#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 : A C++ program to show parameterized member functions/methods using class-object concept.
#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
Example : A C++ program to show nesting of member functions/methods using 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 function/methods with return type using 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;			
		}
		
		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 concept using array of object .
#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];	
	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 (::) ?
#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
Example : A C++ program to show class-object having array of object with 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 object 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 friend function concept.
#include<iostream>
using namespace std;

class frifun
{
	int a,b;
	public:
	void read()
	{
		a=90;
		b=70;
	}	
	friend void display(frifun obj2);
};
	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 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 C++ program to show inline function.
#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
Example : An example of C++ program to show static data member.
#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  -------------------------

#include<iostream>
using namespace std;

class stacon
{
	static int c;
	int x=0;

	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 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 C++ program to show Function Overloading.
#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;
  	}

        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  ----------------------------
#include <iostream>
using namespace std;

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

  	void display(int p, int q)
  	{
           x1= p;
	   x2= q;
	   cout<<x1<<endl<<x2<<endl<<endl;
  	}
  	
  	void display(double m, double n)
  	{
           y1= m;
	   y2= n;
	   cout<<y1<<endl<<y2;
  	}
};

int main() 
{
  A obj;  
  obj.display(40);
  
  obj.display(10,20);
  
  obj.display(41.20,32.23);
  return 0;
}
Output :
40

10
20

41.2
32.23

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.