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