C++ hello world class program
#include
#include
class message
{
public:
void
display()
{
cout << "hello world\n";
}
};
int main()
{
message
c;
c.display();
return 0;
}
C++ addition program using class
#include
#include
class mathematics
{
int x, y;
public:
void input()
{
cout << "input two inetegers\n";
cin >> x >> y;
}
void add()
{
cout << "result = " << x + y;
}
};
int main()
{
mathematics m;
m.input();
m.add();
return 0;
}
C++ program to reverse a number
#include
#include
class operations
{
long c;
public:
void inputnumber()
{
cout << "input a number\n";
cin >> c;
}
long reversenumber()
{
long invert = 0;
while (c != 0)
{
invert = invert * 10;
invert = invert + c%10;
c = c/10;
}
return invert;
}
};
int main()
{
long result;
operations t;
t.inputnumber();
result = t.reversenumber();
cout << "number obtained on reversal = " << result;
return 0;
}
C++ programming code
#include
#include
int main()
{
int n, status = 1, num = 3, count, c;
cout << "enter the number of prime numbers to print\n";
cin >> n;
if ( n >= 1 )
{
cout << "first " << n <<" prime numbers are :-" << endl;
cout << 2 << endl;
}
for ( count = 2 ; count <=n ; )
{
for ( c = 2 ; c <= (int)sqrt(num) ; c++ )
{
if ( num%c == 0 )
{
status = 0;
break;
}
}
if ( status != 0 )
{
cout << num << endl;
count++;
}
status = 1;
num++;
}
return 0;
}
C++ programming code
#include
#include
main()
{
int n, c, first = 0, second = 1, next;
cout << "enter the number of terms of fibonacci series you want" << endl;
cin >> n;
cout << "first " << n << " terms of fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}
C++ programming code
#include
#include
main()
{
int first[20], second[20], c, n;
cout << "enter the number of elements in the array ";
cin >> n;
cout << "enter elements of first array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> first[c];
cout << "enter elements of second array " << endl;
for ( c = 0 ; c < n ; c++ )
cin >> second[c];
cout << "sum of elements of two arrays " << endl;
for ( c = 0 ; c < n ; c++ )
cout << first[c] + second[c] << endl;
return 0;
}
C++programming code
#include
#include
main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "enter the number of rows and columns of matrix ";
cin >> m >> n;
cout << "enter the elements of first matrix\n";
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
cin >> first[c][d];
cout << "enter the elements of second matrix\n";
for ( c = 0 ; c < m ;c++ )
for ( d = 0 ; d < n ; d++ )
cin >> second[c][d];
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
cout << "sum of entered matrices:-\n";
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
cout << sum[c][d] << "\t";
cout << endl;
}
return 0;
}
C++ programming code
#include
#include
main()
{
int n, max, num, c;
cout << "enter the number of random numbers you want ";
cin >> n;
cout << "enter the maximum value of random number ";
cin >> max;
cout << "random numbers from 0 to " << max << " are :-" << endl;
for ( c = 1 ; c <= n ; c++ )
{
num = random(max);
cout << num << endl;
}
return 0;
}
C++ programming code
#include
#include
class programming
{
private:
int variable;
public:
void input_value()
{
cout << "in function input_value, enter an integer\n";
cin >> variable;
}
void output_value()
{
cout << "variable entered is ";
cout << variable << "\n";
}
};
main()
{
programming object;
object.input_value();
object.output_value();
return 0;
}
C++ constructor program
#include
#include
class game
{
private:
int goals;
public:
game()
{
goals = 0;
}
int getgoals()
{
return goals;
}
void incrementgoal()
{
goals++;
}
};
int main()
{
game football;
cout << "number of goals when game is started = " << football.getgoals() <
football.incrementgoal();
football.incrementgoal();
cout << "number of goals a little later = " << football.getgoals() << endl;
return 0;
}
C++ programming code
#include
#include
long add(long, long);
float add(float, float);
int main()
{
long a, b, x;
float c, d, y;
cout << "enter two integers\n";
cin >> a >> b;
x = add(a, b);
cout << "sum of integers: " << x << endl;
cout << "enter two floating point numbers\n";
cin >> c >> d;
y = add(c, d);
cout << "sum of floats: " << y << endl;
return 0;
}
long add(long x, long y)
{
long sum;
sum = x + y;
return sum;
}
float add(float x, float y)
{
float sum;
sum = x + y;
return sum;
}
C++ new operator example
#include
#include
int main()
{
int n, *pointer, c;
cout << "enter an integer\n";
cin >> n;
pointer = new int[n];
cout << "enter " << n << " integers\n";
for ( c = 0 ; c < n ; c++ )
cin >> pointer[c];
cout << "elements entered by you are\n";
for ( c = 0 ; c < n ; c++ )
cout << pointer[c] << endl;
delete[] pointer;
return 0;
}
Scope resolution operator in class
#include
#include
class programming
{
public:
void output();
};
void programming::output()
{
cout << "function defined outside the class.\n";
}
int main()
{
programming x;
x.output();
return 0;
}
No comments:
Post a Comment