views
Download C++ from the App Store. Be aware that Xcode can only be purchased through an Apple device only such as a MacBook, iPhone, iPad, etc. Non-apple users can set up C++ using other IDEs (Integrated Development Environments) such as Visual Studio or CodeBlocks.
Open Xcode and agree to the terms and conditions. This will then install a few components for your computer. It is crucial to accept these terms and conditions as you won't be able to use Xcode without them.
Select the icon that says Create a new Xcode project. This will help you make the C++ application that you've thought of making.
Choose a template for the Xcode project. Pick macOS and select Command Line Tool
Give the Xcode project a name, organization name, and organization identifier. This can be any identifier of your preference. Pick C++ as the Language for the Xcode project. It is very important that C++ is chosen as the main language. C++ Language selection.png
Select a location for the Xcode project. This can be anywhere from a folder, your desktop, or a device where you'd like to save the project. Creating a new folder just for C++ applications would be a useful idea.
Click main.cpp to get access to the code. Erase the entire code so you can rewrite the previous code on your own. This can be a good way to start off C++ for any person that is a beginner.
Add a header for the code to run at the very top. Use #include
Introduce the namespace under the header. This will prevent naming conflicts and help organize your code. This would be known as using namespace std; It is required to have this namespace, or else the code won't be successful. Using namespace std;.png
Write int main() right underneath. This is the main function of the C++ program. The int is the return value whereas main represents the main function for your code. There can only be one main function for a C++ program. Main function.png
Follow up by adding two curved brackets, {}, which will be the body of the function. This is where the code will be written.
Hit enter on your keyboard in the middle of the brackets to make one bracket be at the end of the code. Without these brackets, the code will continue to have errors. Spacing out the brackets gives more area for the user to type out their code and keep it organized. Curved Brackets.png
Add statements such as cout, which is the output of the function. Make sure it is inside the middle of the bracket to create a working code.
Placing cout outside the brackets will cause the program to fail.
Be aware that cout will work for our code because we included #include
Place the operator for the function,<<, right next to the cout. Operators help us make our code with cout. We are using a binary operator in this example. Operator for c++.png
Insert quotation marks and write a sentence. For this example, use “Hello World!” Any sentence of your preference can be used, but for this case it uses "Hello World!"
Put two more operators,<<, to wrap up your code. You are almost done with your code.
Finish the first line of code with endl. Endl wraps up the line and allows the user to start a new line of code. It informs our cout that there is no more code being added and that a new line needs to be integrated inside the function.
Add a semicolon right after endl to conclude the end of that specific line. This is the end for the code.
Run the code by clicking Run Program on the top left of Xcode. If the screen says Build Succeeded then the user has made a successful code. Since "Hello World!" was typed out from a few steps prior, it should be what comes out of the output.
Check the output on the bottom of the screen to see the code appear. Congratulations you've just created your own code!
Comments
0 comment