Using Classes in C++

This C++ demonstration is derived from the code of GamePlayer.pas on our Pascal page Classes and Objects. You should not find it difficult to transfer your Pascal knowledge. In the definition of methods outside of the class declaration C++ uses :: instead of . to separate the class identifier from the method identifier.

#include <iostream>
#include <strings.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

class Player{
  int points = 0;
  char name[20];
public:
  void addPoints(int newPoints);
  void showPoints();
  void setName(char playerName[20]);
  char * getName();
  int getPoints();
};

void Player::addPoints(int newPoints)
{
  points += newPoints;
}

void Player::showPoints()
{
  printf("\nPoints for %s: %d", name, points);
}

void Player::setName(char playerName[])
{
  strlwr(playerName);
  playerName[0] = toupper(playerName[0]);
  strcpy(name, playerName);
}

char * Player::getName()
{
  return name;
}

int Player::getPoints()
{
  return points;
}

int main()
{
  char forename[20];
  Player player1, player2;
  printf("Please enter the forename of the first player: ");
  scanf("%s", &forename);
  player1.setName(forename);
  printf("Please enter the forename of the second player: ");
  scanf("%s", &forename);
  player2.setName(forename);
  srand(time(NULL));
  int i, r1, r2;
  for (i = 1; i < 6; i++)
  {
    r1 = rand() % 4;
    printf("%s draws number %d\n", player1.getName(), r1);
    r2 = rand() % 4;
    printf("%s draws number %d\n", player2.getName(), r2);
    if (r1 > r2)
      player1.addPoints(1);
    if (r2 > r1)
      player2.addPoints(1);
  }
  player1.showPoints();
  player2.showPoints();
  if (player2.getPoints() > player1.getPoints())
    printf ("\n%s wins.\n", player2.getName());
  else
  {
   if (player1.getPoints() > player2.getPoints())
   {
     printf ("\n%s wins.\n", player1.getName());
   }
   else
     printf("\nDraw\n");
  }
  return 0;
}    

Inheritance

This demonstration shows the syntax for creating from the base class Player a derived class ConfidentPlayer. Note the public access specifier, which ensures that all fields and methods in the base class are accessible in the derived class. We have a new function pick() in the base class, which will be overridden in an object of type ConfidentPlayer by its own (more favourable) function with the same identifier. The field msg, the function get_msg() and the procedure introduce() in the derived class are not present in the base class.

ConfidentPlayer has a constructor to initialise msg. In C++ the constructor has the same identifier as the class and does not have a return type such as void.

#include <iostream>
#include <strings.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

using namespace std;

class Player{
  int points = 0;
  char name[20];
public:
  void addPoints(int newPoints);
  void showPoints();
  void setName(char playerName[20]);
  char * getName();
  int getPoints();
  int pick();
};

class ConfidentPlayer : public Player{
  char msg[15];
public:
  ConfidentPlayer(char inputMsg[20]);
  void introduce();
  char * getMsg();
  int pick();
};

void Player::addPoints(int newPoints)
{
  points += newPoints;
}

void Player::showPoints()
{
  printf("\nPoints for %s: %d", name, points);
}

void Player::setName(char playerName[])
{
  strlwr(playerName);
  playerName[0] = toupper(playerName[0]);
  strcpy(name, playerName);
}

char * Player::getName()
{
  return name;
}

int Player::getPoints()
{
  return points;
}

int Player::pick()
{
  return (rand() % 4);
}

ConfidentPlayer::ConfidentPlayer(char inputMsg[20])
{
  strcpy(msg, inputMsg);
}

void ConfidentPlayer::introduce()
{
  printf("Hi! My name is %s. %s\n", getName(), getMsg());
}

int ConfidentPlayer::pick()
{
  return (rand() % 3 + 1);
}

char * ConfidentPlayer::getMsg()
{
  return msg;
}

int main()
{
  char forename[20];
  Player player1;
  ConfidentPlayer player2("I feel lucky.");
  printf("Please enter the forename of the first player: ");
  scanf("%s", &forename);
  player1.setName(forename);
  printf("Please enter the forename of the second player: ");
  scanf("%s", &forename);
  player2.setName(forename);
  player2.introduce();
  srand(time(NULL));
  int i, r1, r2;
  for (i = 1; i < 6; i++)
  {
    r1 = player1.pick();
    printf("%s draws number %d\n", player1.getName(), r1);
    r2 = player2.pick();
    printf("%s draws number %d\n", player2.getName(), r2);
    if (r1 > r2)
      player1.addPoints(1);
    if (r2 > r1)
      player2.addPoints(1);
  }
  player1.showPoints();
  player2.showPoints();
  if (player2.getPoints() > player1.getPoints())
    printf ("\n%s wins.\n", player2.getName());
  else
  {
   if (player1.getPoints() > player2.getPoints())
   {
     printf ("\n%s wins.\n", player1.getName());
   }
   else
     printf("\nDraw\n");
  }
  return 0;
}
    

Input and output:

Please enter the forename of the first player: JUSTIN
Please enter the forename of the second player: JORDAN
Hi! My name is Jordan. I feel lucky.
Justin draws number 1
Jordan draws number 1
Justin draws number 0
Jordan draws number 3
Justin draws number 2
Jordan draws number 2
Justin draws number 2
Jordan draws number 3
Justin draws number 2
Jordan draws number 1

Points for Justin: 1
Points for Jordan: 2
Jordan wins.
Programming - a skill for life!

How to apply your knowledge of Pascal when learning C/C++