C++ program to find the volume of a cube, cylinder, and sphere by function overloading

C++ program to find the volume of a cube, cylinder, and sphere by function overloading.

    #include<iostream>
    using namespace std;
    float T4Tutorials_Volume(int,int);
    float T4Tutorials_Volume(float);
    int T4Tutorials_Volume(int);
     
    int main()
    {
    	int r,h,a;
    	float r1;
        cout<<"Enter radius and height of a cylinder:"<<endl;
    	cin>>r>>h;
    	cout<<"Enter side of cube:"<<endl;
    	cin>>a;
        cout<<"Enter radius of sphere:"<<endl;
    	cin>>r1;
    	cout<<" Volume of cylinder is"<<T4Tutorials_Volume(r,h)<<endl;
    	cout<<" Volume of cube is"<<T4Tutorials_Volume(a)<<endl;
        cout<<"Volume of sphere is"<<T4Tutorials_Volume(r1)<<endl;
        return 0;
    }
    float T4Tutorials_Volume(int r,int h)
    {
        return(3.14*r*r*h);
    }
    float T4Tutorials_Volume(float r1)
    {
        return((4*3.14*r1*r1*r1)/3);
    }
    int T4Tutorials_Volume(int a)
    {
        return(a*a*a);
    }

Output

C++ program to find the volume of a sphere

    #include<iostream>
    using namespace std;
    float T4Tutorials_Volume(float); 
    int main()
    {
    	float r1;
        cout<<"Enter radius of sphere:"<<endl;
    	cin>>r1;
    
        cout<<"Volume of sphere is"<<T4Tutorials_Volume(r1)<<endl;
        return 0;
    }
    float T4Tutorials_Volume(float r1)
    {
        return((4*3.14*r1*r1*r1)/3);
    }

 

 

 

C++ Program to convert a decimal number into binary by inline function

Write a program in C++ to convert a decimal number into binary without using an array and with inline function.Decimal to binary C++

The inline function inline T4Tutorials_Decimal_Number(int n) helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as inline T4Tutorials_Decimal_Number(int n).

Making inline means that compiler can replace the function definitions of inline T4Tutorials_Decimal_Number(int n)with the place where this function is called T4Tutorials_Decimal_Number (n);.

The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.

#include <iostream>
using namespace std;
inline T4Tutorials_Decimal_Number(int n)
{
		int i=1,j=n , binaryn=0;
				for(j=n;j>0;j=j/2)
				{
					binaryn=binaryn+(n%2)*i;
					i=i*10;
					n=n/2;
				}
				cout<<"binary number ="<<binaryn<<endl;
		};
		int main()
		{ 
		    int n;
			cout<<" Please enter Decimal  number :";
		    cin>>n;
			T4Tutorials_Decimal_Number (n);
		}

Output

Please enter Decimal number :

9

binary number =

1001

 

C++ Program to convert a decimal number into binary with friend function

Write a program in C++ to convert a decimal number into binary without using an array with a friend function.Decimal to binary C++

If we declare a function friend int show(T4Tutorials_Decimal_Number);  as a friend in a class T4Tutorials_Decimal_Number then this function friend int show(T4Tutorials_Decimal_Number);  can access the private and protected members of the class T4Tutorials_Decimal_Number. You must know that a global function can also be declared as a  friend function of the class.

 

 

Syntax of friend function in C++

class class_name
{

friend return_type function_name(arguments);

}

#include <iostream>
using namespace std;
class T4Tutorials_Decimal_Number
{     
    protected:
    		int i=1,j=n,n,  binaryn=0;
    public:
    	T4Tutorials_Decimal_Number()
    	{
    		cout<<" enter  Decimal  Number :";
		    cin>>n;
		    for(j=n;j>0;j=j/2)
				{
					binaryn=binaryn+(n%2)*i;
					i=i*10;
					n=n/2;
				}
		}
		friend int show(T4Tutorials_Decimal_Number);
	};
	int show(T4Tutorials_Decimal_Number A)
	{
	       	cout<<"binary number ="<<A.binaryn<<endl;
		}
		int main()
		{ 
		  T4Tutorials_Decimal_Number A;
		  show(A);
	}

Output

enter Decimal Number: 9

1001

C++ Program to convert a decimal number into binary with constructor and destructor

Write a program in C++ to convert a decimal number into binary without using an array and using the constructor and destructor.

Decimal to binary C++

Constructor

#include <iostream>
using namespace std;
class T4Tutorials_Decimal_Number
{
	public:
		T4Tutorials_Decimal_Number(int n)
		{
		int i=1,j=n,T4Tutorials_Binary_Number=0;
	     		cout<<"Please enter Decimal  number : ";
				cin>>n;
				for(j=n;j>0;j=j/2)
				{
					T4Tutorials_Binary_Number=T4Tutorials_Binary_Number+(n%2)*i;
					i=i*10;
					n=n/2;
				}
				cout<<"binary number ="<<T4Tutorials_Binary_Number;
			}
		};
		int main()
		{
			T4Tutorials_Decimal_Number obj(4);
		
		}
			

Output

Please enter Decimal number: 8

binary number = 1000

Destructor

#include <iostream>
using namespace std;
class T4Tutorials_Decimal_Number
{
	public:
		T4Tutorials_Decimal_Number(int n)
		{
		int i=1,j=n, T4Tutorials_Binary_Number=0;
	     		cout<<" Please enter Decimal  number :";
				cin>>n;
				for(j=n;j>0;j=j/2)
				{
					T4Tutorials_Binary_Number=T4Tutorials_Binary_Number+(n%2)*i;
					i=i*10;
					n=n/2;
				}
				cout<<"binary number ="<<T4Tutorials_Binary_Number<<endl;
			}
			~T4Tutorials_Decimal_Number()
			{
				cout<<"Destructor run , FREE MEMORY "<<endl;
			}
		};
		int main()
		{
			T4Tutorials_Decimal_Number obj(4);
		}

Output

Please enter Decimal number: 4

binary number = 100

C++ Program to convert a decimal number into binary with constructor overloading.

Write a program in C++ to convert a decimal number into binary without using an array by using the constructor overloading.

Decimal to binary C++

#include<iostream>
using namespace std;
class T4Tutorials_Decimal_Constructor
{
	protected :
		int i,j;
		int decimal, binary;
		int decimal1, binary1;
		int decimal2, binary2;
	public :
		T4Tutorials_Decimal_Constructor(int n)
		{
			decimal=n;
			i=1;j=n,binary=0;
		  for(j=n;j>0;j=j/2)
				{
					binary=binary+(n%2)*i;
					i=i*10;
					n=n/2;
				}
				  	cout<<"binary number = "<<binary<<endl;
			}
		
		T4Tutorials_Decimal_Constructor(int n1,int n2)
		{
			decimal1=n1;
			decimal2=n2;
			i=1;
		for(j=n1;j>0;j=j/2)
				{
					binary1=binary1+(n1%2)*i;
					i=i*10;
					n1=n1/2;
				}
				  	cout<<"binary number = "<<binary1<<endl;
			i=1;
		for(j=n2;j>0;j=j/2)
				{
					binary2=binary2+(n2%2)*i;
					i=i*10;
					n2=n2/2;
				}
				  	cout<<"binary number = "<<binary2<<endl;
	}
};
int main()
{

	int option;
	cout<<"Enter 1 FOR Single parameter Constructor"<<endl;
	cout<<"Enter 2 FOR Multiple Paramter construcor "<<endl;
	cin>>option;
	
	if(option ==1)
	{
		cout<<"You Have Slected Single Paramater  Constructor."<<endl<<endl;
		int n;
		cout<<"Enter a Decimal Number to convert into BINARY Number : "<<endl;
		cin>>n;
		T4Tutorials_Decimal_Constructor a(n);
	}
	else if(option==2)
	{
		cout<<"You Have slected Multiple Paramater Constructor."<<endl<<endl;
		
		int n1,n2;
		cout<<"Enter First Decimal no. to convert into BINARY no. : ";
		cin>>n1;
		cout<<"Enter Second Decimal no. to convert into BINARY no. : ";
		cin>>n2;
		T4Tutorials_Decimal_Constructor a(n1, n2);
	}
	else
	cout<<"invalid Input ";
}

Output

Enter 1 FOR Single parameter Constructor.

Enter 1 FOR Single parameter Constructor.

2

You Have Selected Single Parameter Constructor.

Enter First Decimal no. to convert into BINARY no: 4

Enter Second Decimal no. to convert into BINARY no: 8

binary number=100

binary number=1000

 

Write a c++ program to find out the sum of an A.P. series by using the inline function.

Write a c++ program to find out the sum of an A.P. series by using the inline function.

The inline function inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function() helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function().

Making inline means that compiler can replace the function definitions of inline T4Tutorials_AP_Series_Function()with the place where this function is called object.T4Tutorials_AP_Series_Function();.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.

#include<iostream>
using namespace std;
class T4Tutorials_AP_Series
{
	protected :
		int n1, T4Tutorials_Difference, n2, i, ln, s1;
	public :
		T4Tutorials_AP_Series_Function();
};
inline T4Tutorials_AP_Series::T4Tutorials_AP_Series_Function()
	{
		    s1 = 0;
		    cout << "Input  the starting number of the A.P. series: "<<endl;
		    cin >> n1;
		    cout << "Input the number of items for  the A.P. series: "<<endl;
		    cin >> n2;
		    cout << "Input  the common difference of A.P. series: "<<endl;
		    cin >> T4Tutorials_Difference;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2;
		    ln = n1 + (n2 - 1) * T4Tutorials_Difference;
		    cout << "The Sum of the  A.P. series are : "<<endl;
		    for (i = n1; i <= ln; i = i + T4Tutorials_Difference) 
		    {
		        if (i != ln)
		            cout << i << " + ";
		        else
		            cout << i << " = " << s1 << endl;
		    }
		}
int main()
{
	T4Tutorials_AP_Series object;
	object.T4Tutorials_AP_Series_Function();
}

 

Write a c++ program to find out the sum of an A.P. series using constructor and destructor.

Write a c++ program to find out the sum of an A.P. series using constructor and destructor.

Constructor

The constructor T4Tutorials_AP_Series() is a member function of the class T4Tutorials_AP_Series. The constructor T4Tutorials_AP_Series()has the same name as the name of its class T4Tutorials_AP_Series.

  1. When a new object T4Tutorials_AP_Series a; of the class T4Tutorials_AP_Seriesis executed, the constructor T4Tutorials_AP_Series() also executed automatically.
  2. The constructor T4Tutorials_AP_Series()has no data type. Even we can’t use void also.
  3. The constructor T4Tutorials_AP_Series()can have arguments.
  4. The constructor T4Tutorials_AP_Series()can be only public.
  5. There is no inheritance of the constructor T4Tutorials_AP_Series().
#include<iostream>
using namespace std;
class T4Tutorials_AP_Series
{
	protected :
		int n1, df, n2, i, ln, s1;
	public :
		T4Tutorials_AP_Series()
		{
		    s1 = 0;
		    cout << "Please input  the starting number of the A.P. series: "<<endl;
		    cin >> n1;
		    cout << "Please input the number of items for  the A.P. series: "<<endl;
		    cin >> n2;
		    cout << "Please input  the common difference of A.P. series: "<<endl;
		    cin >> df;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * df)) / 2;
		    ln = n1 + (n2 - 1) * df;
		    cout << "The Sum of the  A.P. series are : "<<endl;
		    for (i = n1; i <= ln; i = i + df) 
		    {
		        if (i != ln)
		            cout << i << " + ";
		        else
		            cout << i << " = " << s1 << endl;
		    }
		}

};
int main()
{
	T4Tutorials_AP_Series a;
}

Destructor

The destructor ~T4Tutorials_AP_Series() is a member function of the class T4Tutorials_AP_Series. Destructor ~T4Tutorials_AP_Series() has the same name as the name of its class T4Tutorials_AP_Series. The tild sign ∼ is used before the name of the destructor ~T4Tutorials_AP_Series().

  • When the object T4Tutorials_AP_Series a; of the class T4Tutorials_AP_Seriesdestroyed, then the destructor ~T4Tutorials_AP_Series() also destroyed automatically.
  • One class T4Tutorials_AP_Series can have only one destructor ~T4Tutorials_AP_Series(). However, one class can have many constructors.
  • Destructor ~T4Tutorials_AP_Series()overloading is impossible.
  • The Destructor ~T4Tutorials_AP_Series()can’t have any arguments(parameters).
  • The destructor ~T4Tutorials_AP_Series()has no data type.
#include<iostream>
using namespace std;
class T4Tutorials_AP_Series
{
	protected :
		int n1, df, n2, i, ln, s1;
	public :
		~T4Tutorials_AP_Series()
		{
		    s1 = 0;
		    cout << "Please input  the starting number of the A.P. series: "<<endl;
		    cin >> n1;
		    cout << "Please input the number of items for  the A.P. series: "<<endl;
		    cin >> n2;
		    cout << "Please input  the common difference of A.P. series: "<<endl;
		    cin >> df;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * df)) / 2;
		    ln = n1 + (n2 - 1) * df;
		    cout << "The Sum of the  A.P. series are : "<<endl;
		    for (i = n1; i <= ln; i = i + df) 
		    {
		        if (i != ln)
		            cout << i << " + ";
		        else
		            cout << i << " = " << s1 << endl;
		    }
		}

};
int main()
{
	T4Tutorials_AP_Series a;
}

 

Write a c++ program to find out the sum of an A.P. series by using the constructor overloadin.

Write a c++ program to find out the sum of an A.P. series by using the constructor overloading.

The concept of using more than one constructor with the same name is called constructor overloading.

In this program, the constructor must obey one or both of the following rules.

  1. All constructors with the same name and having a different number of parameters.
    • T4Tutorials_Constructor() and another constructor as T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference).
  2. All constructors with the same name and have the same number of parameters but of different data types.
    • T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference) and another constructor as 4Tutorials_Constructor(double n1, double n2, double T4Tutorials_Difference).

Note: In this example, we are overloading the constructor with the following rule;

One constructor as T4Tutorials_Constructor() and another constructor as T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference).

#include<iostream>
using namespace std;
class T4Tutorials_Constructor
{
	protected :
		int n1, T4Tutorials_Difference, n2, i, ln, s1;
	public :
		T4Tutorials_Constructor()
		{
		    s1 = 0;
		    cout << "Input  the starting number of the A.P. series: "<<endl;
			cin >> n1;
			cout << "Input the number of items for  the A.P. series: "<<endl;
			cin >> n2;
			cout << "Input  the common difference of A.P. series: "<<endl;
			cin>>T4Tutorials_Difference;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2;
		    ln = n1 + (n2 - 1) * T4Tutorials_Difference;
		    cout << "The Sum of the  A.P. series are : "<<endl;
		    for (i = n1; i <= ln; i = i + T4Tutorials_Difference) 
		    {
		        if (i != ln)
		            cout << i << " + ";
		        else
		            cout << i << " = " << s1 << endl;
		    }
		}
		T4Tutorials_Constructor(int n1, int n2,int T4Tutorials_Difference)
		{
		    s1 = 0;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2;
		    ln = n1 + (n2 - 1) * T4Tutorials_Difference;
		    cout << "The Sum of the  A.P. series are : "<<endl;
		    for (i = n1; i <= ln; i = i + T4Tutorials_Difference) 
		    {
		        if (i != ln)
		            cout << i << " + ";
		        else
		            cout << i << " = " << s1 << endl;
		    }
		}

};
int main()
{
	int option;
	cout<<"Please Enter the  1 for T4Tutorials_Constructor with No paranter :"<<endl;
	cout<<"Please Enter the  2 for T4Tutorials_Constructor with Paramter : \n";
	cout<<"Please Enter the  1 or 2  here : ";
	cin>>option;
	system("cls");
	if(option==1)
	{
		cout<<"You Have Slected No Paramter T4Tutorials_Constructoructor.... "<<endl;
		T4Tutorials_Constructor a;
		
	}
	else if(option==2)
	{
		int n1,n2,T4Tutorials_Difference;
		cout << "Input  the starting number of the A.P. series: "<<endl;
		cin >> n1;
		cout << "Input the number of items for  the A.P. series: "<<endl;
		cin >> n2;
		cout << "Input  the common difference of A.P. series: "<<endl;
		cin>>T4Tutorials_Difference;
		T4Tutorials_Constructor (n1,n2,T4Tutorials_Difference);
	}
	else
	{
		cout<<"Your Input is Wrong : Try Again."<<endl;
	}
}

 

Write a c++ program to find out the sum of an A.P. series by using the friend function.

Write a c++ program to find out the sum of an A.P. series by using the friend function.

If we declare a function friend int show(T4Tutorials_AP_Series); as a friend in a class T4Tutorials_AP_Series then this function int show(T4Tutorials_AP_Series);  can access the private and protected members of the class T4Tutorials_AP_Series. You must know that a global function can also be declared as a  friend function of the class.

Syntax of friend function in C++

class class_name
{

friend return_type function_name(arguments);

}

#include<iostream>
using namespace std;
class T4Tutorials_AP_Series
{
	protected :
		int n1, T4Tutorials_Difference, n2, i, ln, s1;
	public :
		input()
		{
		    s1 = 0;
		    cout << "Input  the starting number of the A.P. series: "<<endl;
		    cin >> n1;
		    cout << "Input the number of items for  the A.P. series: "<<endl;
		    cin >> n2;
		    cout << "Input  the common difference of A.P. series: "<<endl;
		    cin >> T4Tutorials_Difference;
		    s1 = (n2 * (2 * n1 + (n2 - 1) * T4Tutorials_Difference)) / 2;
		    ln = n1 + (n2 - 1) * T4Tutorials_Difference;
		}
		friend int show(T4Tutorials_AP_Series);

};
int show (T4Tutorials_AP_Series a)
{
	cout << "The Sum of the  A.P. series are : ";
		    for (a.i = a.n1; a.i <= a.ln; a.i = a.i + a.T4Tutorials_Difference) 
		    {
		        if (a.i != a.ln)
		            cout << a.i << " + ";
		        else
		            cout << a.i << " = " << a.s1 << endl;
		    }
}
int main()
{
	T4Tutorials_AP_Series a;
	a.input();
	show(a);
}

 

Write a program in C++ to print Floyd’s Triangle by using the inline function.

Write a program in C++ to print Floyd’s Triangle by using the inline function.

The inline function inline T4Tutorials_Pattern_Function() helps to increase the execution time of a program. The programmer can make a request to the compiler to make the inline function as inlineinline T4Tutorials_Pattern_Function().

Making inline means that compiler can replace the function definitions of inline T4Tutorials_Pattern_Function()with the place where this function is called object.T4Tutorials_Pattern_Function();.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.

#include<iostream>
using namespace std;
class T4Tutorials_Floyds_Triangle
{
	protected:
		int i,j,p,q,n;
		public:
			int T4Tutorials_Pattern_Function();
};
inline T4Tutorials_Floyds_Triangle::T4Tutorials_Pattern_Function()
{
	cout<<"Pleae enter the no of rows";
	cin>>n;
	for(i=1;i<=n;i++)
	{
		if(i%2==0)
		{
			p=1;
			q=0;
		}
		else
		{
			p=0;
			q=1;
		}
		for(j=1;j<=i;j++)
		{
			if(j%2==0)
			{
				cout<<p;
			}
			else
			{
				cout<<q;
			}
		}
		cout<<endl;
	}
}
int main()
{
	T4Tutorials_Floyds_Triangle object;
	object.T4Tutorials_Pattern_Function();
}

 

Output

Write a program in C++ to print Floyd’s Triangle by using the friend function.

Write a program in C++ to print Floyd’s Triangle by using the friend function.

If we declare a function friend int show(T4Tutorials_Floyds_Triangle);as a friend in a class T4Tutorials_Floyds_Trianglethen this function int show (T4Tutorials_Floyds_Triangle a)can access the private and protected members of the class T4Tutorials_Floyds_Triangle.

You must know that a global function can also be declared as a  friend function of the class.

Syntax of friend function in C++

class class_name
{

friend return_type function_name(arguments);

}

#include<iostream>
using namespace std;
class T4Tutorials_Floyds_Triangle
{
	public:
		int i,j,p,q,n;
		public:
			int Floyds_Pattern()
			{
				cout<<"enter no.of rows";
				cin>>n;
			}
			friend int show(T4Tutorials_Floyds_Triangle);
};
int show (T4Tutorials_Floyds_Triangle a)
{
	for(a.i=1;a.i<=a.n;a.i++)
	{
			if(a.i%2==0)
		{
			a.p=1;
			a.q=0;
		}
		else
		{
			a.p=0;
			a.q=1;
		}
		for(a.j=1;a.j<=a.i;a.j++)
		{
			if(a.j%2==0)
			{
				cout<<a.p;
			}
			else
			{
				cout<<a.q;
			}
		}
		cout<<endl;
	}
}
int main()
{
	T4Tutorials_Floyds_Triangle a;
	a.Floyds_Pattern();
	show(a);
}

 

 

Output

Write a program in C++ to print Floyd’s Triangle by using the constructor destructor.

Write a program in C++ to print Floyd’s Triangle by using the constructor destructor.

Constructor

The constructor T4Tutorials_Floyds_Triangle() is a member function of the class T4Tutorials_Floyds_Triangle. The constructor T4Tutorials_Floyds_Triangle() has the same name as the name of its class T4Tutorails_Series.

  1. When a new object T4Tutorials_Floyds_Triangle object; of the class T4Tutorials_Floyds_Triangle is executed, the constructor T4Tutorials_Floyds_Triangle() also executed automatically.
  2. The constructor T4Tutorials_Floyds_Triangle() has no data type. Even we can’t use void also.
  3. The constructor T4Tutorials_Floyds_Triangle()can have arguments.
  4. The constructor T4Tutorials_Floyds_Triangle()can be only public.
  5. There is no inheritance of the constructor T4Tutorials_Floyds_Triangle().

Destructor

The destructor ~T4Tutorials_Floyds_Triangle() is a member function of the class T4Tutorials_Floyds_Triangle. Destructor ~T4Tutorials_Floyds_Triangle() has the same name as the name of its class T4Tutorials_Series. The tild sign ∼ is used before the name of the destructor ~T4Tutorials_Floyds_Triangle().

  • When the object T4Tutorials_Floyds_Triangle object;of the classT4Tutorials_Floyds_Triangle destroyed, then the destructor ~T4Tutorials_Floyds_Triangle()also destroyed automatically.
  • One class T4Tutorials_Floyds_Triangle can have only one destructor ~T4Tutorials_Floyds_Triangle(). However, one class can have many constructors.
  • Destructor ~T4Tutorials_Floyds_Triangle()overloading is impossible.
  • The Destructor ~T4Tutorials_Floyds_Triangle()can’t have any arguments(parameters).
  • The destructor ~T4Tutorials_Floyds_Triangle()has no data type.
#include<iostream>
using namespace std;
class T4Tutorials_Floyds_Triangle
{
	public:
		int i,j,p,q,n;
		public:
//constructor
			T4Tutorials_Floyds_Triangle()         			{
             	cout<<"Please enter the no of rows";
	            cin>>n;
               	for(i=1;i<=n;i++)
             	{
		if(i%2==0)
		{
			p=1;
			q=0;
		}
		else
		{
			p=0;
			q=1;
		}
		for(j=1;j<=i;j++)
		{
			if(j%2==0)
			{
				cout<<p;
			}
			else
			{
				cout<<q;
			}
		}
		cout<<endl;
	}
}
	~T4Tutorials_Floyds_Triangle()       // destructor
	{
	}
};
int main()
{
	T4Tutorials_Floyds_Triangle object;
	return 0;
}

Output