S1: A player written in cpp
We will do a very brief introduction to cpp, with the goal of allowing you to understand the ROS code we will supply.
However, a deep study of cpp is out of the scope of this workshop. You can check several cpp courses online, e.g.:
http://www.cplusplus.com/doc/tutorial/
https://class.coursera.org/cplusplus4c-002/lecture
We will use c++ functionalities to create code for our game. Later on we will plug in the ROS stuff to this code skeleton.
First lets create a file:
cd ~/catkin_ws/src/rws2018/rws2018_moliveira/player_moliveira/src
touch player_moliveira_node.cpp
and paste the following inside the file:
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
return 1;
}
Editing CMakeLists.txt, compiling with catkin_make and executing with rosrun
NOTE: you may configure vscode to compile and run ros projects. It is better than gedit (and much worse than vim).
Create a class Player, with a constructor which receives as argument an std::string.
#include <iostream>
class Player
{
public:
Player(std::string name)
{
this->name = name;
}
std::string name;
private:
};
int main()
{
std::string player_name = "moliveira";
//Creating an instance of class Player
Player player(player_name);
std::cout << "Created an instance of class player with public name " << player.name << std::endl;
}
#include <iostream>
class Player
{
public:
//Constructor with the same name as the class
Player(std::string name) { this->name = name;}
//Set team name, if given a correct team name (accessor)
int setTeamName(std::string team)
{
if (team=="red" || team=="green" || team=="blue")
{
this->team = team;
return 1;
}
else
{
std::cout << "cannot set team name to " << team << std::endl;
return 0;
}
}
//Gets team name (accessor)
std::string getTeam(void) {return team;}
std::string name; //A public atribute
private:
std::string team;
};
int main()
{
//Creating an instance of class Player
Player player("moliveira");
player.setTeamName("red");
std::cout << "player.name is " << player.name << std::endl;
std::cout << "team is " << player.getTeam() << std::endl;
}
#include <iostream>
class Player
{
public:
//Constructor with the same name as the class
Player(std::string name) { this->name = name;}
int setTeamName(int team_index = 0 /*default value*/)
{
switch (team_index)
{
case 0:
return setTeamName("red"); break;
case 1:
return setTeamName("green"); break;
case 2:
return setTeamName("blue"); break;
default:
std::cout << "wrong team index given. Cannot set team" << std::endl; break;
}
}
//Set team name, if given a correct team name (accessor)
int setTeamName(std::string team)
{
if (team=="red" || team=="green" || team=="blue")
{
this->team = team;
return 1;
}
else
{
std::cout << "cannot set team name to " << team << std::endl;
return 0;
}
}
//Gets team name (accessor)
std::string getTeamName(void) {return team;}
std::string name; //A public atribute
private:
std::string team;
};
int main()
{
//Creating an instance of class Player
Player player("moliveira");
player.setTeamName("red");
player.setTeamName(2);
std::cout << "player.name is " << player.name << std::endl;
std::cout << "team is " << player.getTeamName() << std::endl;
}
class Player
// ... (hidden for better visualization)
//Class myPlayer extends class Player
class myPlayer: public Player
{
public:
myPlayer(std::string name, std::string team): Player(name)
{
setTeamName(team);
}
};
int main()
{
//Creating an instance of class Player
myPlayer my_player("moliveira","green");
std::cout << "my_player.name is " << my_player.name << std::endl;
std::cout << "team is " << my_player.getTeamName() << std::endl;
}
We can use namespaces to group functions, classes, variables, etc under a common name.
For example, all opencv stuff is under the namespace cv::
In the following code, we created our own namespace. In addition, we are using a shortcut to the namespace std
#include <iostream>
using namespace std;
namespace rws_moliveira
{
class Player **//hidden for better visualization**
//Class myPlayer extends class Player
class myPlayer: public Player **//hidden for better visualization**
} //end of namespace rws_moliveira
int main()
{
//Creating an instance of class Player
rws_moliveira::myPlayer my_player("moliveira","green");
cout << "my_player.name is " << my_player.name << endl;
cout << "team is " << my_player.getTeamName() << endl;
}
A class team should contain a list of players.
//System includes
#include <iostream>
#include <vector>
//Boost includes
#include <boost/shared_ptr.hpp>
using namespace std;
/**
* @brief The namespace of this lib
*/
namespace rws2018_moliveira
{
class Player **//hidden for better visualization**
//Class myPlayer extends class Player
class myPlayer: public Player **//hidden for better visualization**
/**
* @brief Contains a description of a team
*/
class Team
{
public:
/**
* @brief Constructor
* @param team_name the team name
*/
Team(string team_name)
{
name = team_name;
}
/**
* @brief Prints the name of the team and the names of all its players
*/
void printTeamInfo(void)
{
//Write code here ...
}
/**
* @brief Checks if a player belongs to the team
* @param player_name the name of the player to check
* @return true or false, yes or no
*/
bool playerBelongsToTeam(string player_name)
{
//write code here ...
}
/**
* @brief The team name
*/
string name;
/**
* @brief A list of the team's player names
*/
vector<string> players;
};//end of class Team
} //end of namespace rws_moliveira
int main()
{
//Creating an instance of class Team
rws_moliveira::Team team("green");
team.players.push_back("moliveira");
team.players.push_back("vsantos");
//team.printTeamInfo();
}