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);
}
Write a program in C++ to convert a decimal number into binary without using an array and with inline function.
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);
}
Write a program in C++ to convert a decimal number into binary without using an array with a friend function.
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);
}
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.
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.
When a new object T4Tutorials_AP_Series a; of the class T4Tutorials_AP_Seriesis executed, the constructor T4Tutorials_AP_Series() also executed automatically.
The constructor T4Tutorials_AP_Series()has no data type. Even we can’t use void also.
The constructor T4Tutorials_AP_Series()can have arguments.
The constructor T4Tutorials_AP_Series()can be only public.
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 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.
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).
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).
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.
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.
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();
}
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.
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.
When a new object T4Tutorials_Floyds_Triangle object; of the class T4Tutorials_Floyds_Triangle is executed, the constructor T4Tutorials_Floyds_Triangle() also executed automatically.
The constructor T4Tutorials_Floyds_Triangle() has no data type. Even we can’t use void also.
The constructor T4Tutorials_Floyds_Triangle()can have arguments.
The constructor T4Tutorials_Floyds_Triangle()can be only public.
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;
}