Function to Find Factorial in C++
#include
#include
using namespace std;
int factorial(int var)
{
int
fact=1;
for(int
i=1;i<=var;i++)
fact =
fact * i;
return
fact;
}
int main()
{
cout<<"5 Factorial Number :"<
getch();
return
0;
}
Output
5 Factorial Number: 120
Inline Function Using C++ Programming
#include
#include
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value
is:"<
cout<<"\n\nCube value is
:"<
getch();
}
Output
Enter two values: 5
7
Multiplication Value is: 35
Cube Value is: 25 and 343
Constructor in C++
#include
#include
using namespace std;
class example
{
int a,b;
public:
example()
{
a=10;
b=20;
cout<<"Im Constructor\n";
}
void display()
{
}
};
int main()
{
example object;
object.display();
getch();
return 0;
}
Output
Im Constructor
Values :10
20
Parameterized Constructor In C++
#include
#include
using namespace std;
class example
{
int a,b;
public:
example(int
x,int y)
{
a=x;
b=y;
cout<<"Im Constructor\n";
}
void display()
{
}
};
int main()
{
example object(10,20);
object.display();
getch();
return 0;
}
Output
Im Constructor
Values :10
20
Constructor Overloading In C++
#include
#include
using namespace std;
class example
{
int a,b;
public:
example()
{
a=50;
b=100;
cout<<"\nIm Constructor";
}
example(int
x,int y)
{
a=x;
b=y;
cout<<"\nIm Constructor";
}
void
display()
{
}
};
int main()
{
example object(10,20);
example object2;
object.display();
object2.display();
getch();
return 0;
}
Output
Im Constructor
Im Constructor
Values :10 20
Values :50 100
No comments:
Post a Comment