Hello World,
Today we will be discussing how to declare variables,
constants, and basic arithmetic using C++. Fortunately for me, due to my prior
experience with Java, today’s post is a simple review. However, one thing that
is necessary for your code to execute properly is to make sure everything
prints correctly. What I mean by this, at the top of your compiler, verify that
this line of code exists: using namespace std;. This is the code that prints
any output. I had to add this line of code to my program because it wasn’t
already a default in my compiler.
Let’s start with declaring a variable. C++ has many of
the same features as Java when it comes to declaring variables. For instance if
you are making an instance variable in your main called volume, you just type:
int volume;. Now that the instance variable volume is defined you can assign it
to a number by saying volume = 1; on the line after. And you can then print
that by typing: cout << volume;. Please refer to the picture below to get
a better understanding of what I am talking about.
Constants are slightly different that how Java does
it. In Java, at the top of the page you can just say int constant; and that is
that. C++ has syntax that is slightly different. At the top of the page you must
type #define “(constant’s name)” and then add a number. Also semicolons are not
necessary in defining constants. Below is how to define a constant properly.
Finally arithmetic is exactly the same in Java as it
is for C++. Some of the commands you can do in arithmetic are: addition,
subtraction, multiplication, division, mod, incrementing by 1, decrementing by
1, divide and assign, multiply and assign, and mod and assign. Below is a
picture taken from wikiversity.org.
It shows how to write the arithmetic commands in C++. If you are familiar with
Java then this should be an easy review.
Now let’s take what we just learned and apply it to a
basic arithmetic function. I defined 3 constants, LENGTH, WIDTH, and HEIGHT, at
the top of the page. Then I made an instance variable called volume. I set
volume equal to LENGTH*WIDTH*HEIGHT and then I print out the result. Below is a
picture of this little program.
I hope you enjoyed this post and please let me know if
you have any questions pertaining to some of the information we covered.
Thanks!
Is the online compiler going to be sufficient when it comes to writing larger programs?
ReplyDeleteYes I actually was thinking about that myself. The other day I looked online for some sample programs to run. I found one on a forum that was 144 lines long. I ran it and the code compiled and produced the intended output. The code that I used was found on: http://www.cplusplus.com/forum/beginner/82385/
DeleteI feel confident this will be sufficient.
This appears to be straight forward enough despite being different from Java in a couple ways. Interesting how a semi-colon is not necessary in defining constants. If you were to end those declarations with a semi-colon, would the code still compile?
ReplyDeleteI was here for CS 270
ReplyDelete