Ques.  : A C++ program to represent simple class-object concept ?
#include<iostream>
using namespace std;

class employee
{
    public:
    int sal1;
    int sal2;
};

int main()
{
    employee A;
    employee B;    
    
    A.sal1=450;
    A.sal2=210;    
   
    B.sal1=85;
    B.sal2=23;

    cout <<" First salary is : "<< A.sal2 << " and " << A.sal1<<endl;
    cout <<" Second salary is : "<< B.sal2 << " and " << B.sal1;
    
   return 0;
}
Output : (This program is executed and tested in Dev C++ application)
First salary is : 210 and 450
Second salary is : 23 and 85

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

#include<iostream>
using namespace std;

class employee
{
    public:
    int sal1=5600;
    int sal2=6300;
};

int main()
{
    employee A;
    employee B;     

    cout <<" First salary is : "<< A.sal2 << " and " << A.sal1<<endl;
    cout <<" Second salary is : "<< B.sal2 << " and " << B.sal1;
    
   return 0;
}
Output : 
 First salary is : 6300 and 5600
 Second salary is : 6300 and 5600

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

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

/* -------------------  OR  -------------------- */
#include <iostream>
using namespace std;

class A 
{		
      public:
  	int num1 ;
  	char name[20];
};

int main() 
{
        A a={10,"Codershelpline"};
  	cout<<a.num1<<"  "<<a.name; 
	return 0;
}
Output :
10  Codershelpline
Ques. : A basic C++ program to show class-object concept .
#include <iostream>
using namespace std;
class student
{
	private:		
		char  sname[50];
		int   srollno;		
		float tfee;		
	public:		
		void input()
		{
			cout << "Enter student name: " ;
			cin >> sname;
			cout << "Enter student roll number: ";
			cin >> srollno;
			cout << "Enter student tution fee : ";
			cin >> tfee;	
		}
		void output()
		{
			cout<<endl<< "Student stored 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;
}
Ques. : A basic C++ program to show class-object concept with private methods [Nesting                      Methods].
#include <iostream>
using namespace std;
class student
{
	private:			
		int x,y,z;				
	private:			
		void input()
		{
			cout << "Enter first Number : " ;
			cin >> x;
			cout << "Enter second number: ";
			cin >> y;				
		}		
		void process()
		{
			z=x+y;
		}
		
	public:		
		void output()
		{
			input();
			process();
			
			cout<<"The addition result is = "<<z;
		}	
};
int main()
{
	student stu;	
	stu.output();	
	return 0;
}
Ques. : A basic C++ program to show class-object concept having private methods with                          return type.
#include <iostream>
using namespace std;
class student
{
	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 = "<<z;
		}	
};
int main()
{
	student stu;
	stu.input();
	stu.process();	
	stu.output();	
	return 0;
}

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

class number 
{
	public:
   	int num = 210;
   	char ch='X';
   	
   void display(number obj2)
   {
      cout<<obj2.num<<endl;
      cout<<obj2.ch<<endl;
   }
};
int main() 
{
   number obj1;
   obj1.display(obj1);
   return 0;
}
Ques. : A C++ program to return an object  through a function.
#include <iostream>
using namespace std;

class Student 
{
public:
	
   int rollno;
   int age;
   string sname;
  
   Student input(int num1, int num2, string str)
   {
      Student stu1;
      
      stu1.rollno = num1;
      stu1.age = num2;
      stu1.sname = str;
      
      return stu1;    //retrun object
   }
   
   void output(Student stu2)    //object as parameter
   {
      cout<<"Student Name: "<<stu2.sname<<endl;
      cout<<"Roll No : "<<stu2.rollno<<endl;
      cout<<"Student Age : "<<stu2.age<<endl;
   }
};
int main() 
{
   Student stu3;
   
   stu3 = stu3.input(10130, 37, "Mr.Ashok");
   stu3.output(stu3);   
   return 0;
}
Output :
Student Name: Mr.Ashok 
Roll No : 10130
Student Age :37
Ques. : 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 tfee;		
	public:		
		void input()
		{
			cout << "Enter student name: " ;
			cin >> sname;
			cout << "Enter student roll number: ";
			cin >> srollno;
			cout << "Enter student tution fee : ";
			cin >> tfee;	
		}
		void output()
		{
			cout<<endl<< "Student stored 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[3];	
	int i;	
	for(i=0;i<3;i++)
	{
	   stu[i].input();		
	}	
	for(i=0;i<3;i++)
	{
	   stu[i].output();		
	}	
	return 0;
}
Ques. : 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 stored 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;
}
Ques. : A C++ program to show class-object with array of 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 stored 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[3];	
	int i;	
	for(i=0;i<3;i++)
	{
	   stu[i].input();		
	}
	
	for(i=0;i<3;i++)
	{
	   stu[i].output();		
	}	
	return 0;
}
Ques. : 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;
}
Ques. : A c++ program to represent friend class concept.
#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
Ques. : 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 : 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;
	
	obj1.count();
	obj2.count();
	obj1.count();
	obj2.count();
	
	return 0;
}

Output:
8    1
9    1
10   2
11   2
Example : An example of C++ program 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
Example : An example of C++ program to show friend class.
#include <iostream>  
using namespace std;  
class A  
{  
    int x =15;  
    friend class B;           // friend class declaration.  
};  
class B  
{  
  public:  
    void display(A &a)  
    {  
        cout<<"value of x is = "<<a.x;  
    }  
};  
int main()  
{  
    A a;  
    B b;  
    b.display(a);  
    return 0;  
} 

Output :
 value of x is = 15

 445 total views,  1 views today

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.