Page

Constructor Program in C++

#include <iostream>
#include <conio.h> 
using namespace std;
 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() << endl;

  football.incrementGoal();
  football.incrementGoal();

  cout << "Number of goals a little later = " << football.getGoals() << endl;

  return 0;
}

No comments:

Post a Comment