I'll start by stating I don't believe this question's tags are not exactly related to the problem at hand. The compilation one is the closest one but, even though, I consider the question to be more related to specific environment file selection and string manipulation than anything else.
Nonetheless there are several ways of doing it. It mostly comes down to the system in which you want to compile these files and the compilation environment, if any.
Basic options will involve Makefiles, Shell Scripting, for Linux environments, Batch Files and PowerShell for Windows environments, and so on.
Given the way the question was formulated I'll assume you're on a Linux system using Bash. A simple solution for that would involve a few commonly-distributed tools (you can find out more about each of them on their man pages):
Assuming you're running the commands in the same folder the source files are located, the following line should do the trick for you:
for f in *.cpp; do g++ -o $(echo $f | awk -F.cpp '{printf "%s", $1}') $f; done
What the line above is doing is:
for f in *.cpp - iterates over all files in the current directory that match the provided wildcard. On each iteration the file name is stored at the $f variable
echo $f | awk -F.cpp '{printf "%s", $1}' - this snippet removes the .cpp extension from the file name we've got
Edit note: the proposed solution was improved by removing the parsing over ls -l's result because of @clcto's reference in this answer's comments.
main()is not a method, it is a free (i.e., non-member) function.make -k a b c d e f g h i j. Or write a short makefile that depends on$(patsubst *.cpp,%,$(wildcard *.cpp)).