An operation is an action performed on one or more values either to
modify the value held by one or both of the variables or to produce a
new value by combining variables. Therefore, an operation is performed
using at least one symbol and one
value. The symbol used in an operation is called an operator. A variable
or a value involved in an operation is called an operand.
A unary operator is an operator that performs its
operation on only one operand.
An operator is referred to as binary if it operates on two operands.
Unary Operators: The Positive Operator +
|
|
Algebra uses a type of ruler to classify
numbers. This ruler has a middle position of zero. The numbers on the
left side of the 0 are
referred to as negative while the numbers on the right side of the
rulers are considered positive:
-∞ |
|
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
|
1 |
2 |
3 |
4 |
5 |
6 |
|
+∞ |
0 |
-∞ |
|
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
|
1 |
2 |
3 |
4 |
5 |
6 |
|
+∞ |
A value on the right side of 0 is considered
positive. To express that a number is positive, you can write a + sign
on its left. Examples are +4, +228, +90335. In this case the + symbol is
called a unary operator because it acts on only one operand.
The positive unary operator, when used, must be positioned on the left side of its
operand, never on the right side.
As a mathematical convention, when a value is
positive, you do not need to express it with the + operator. Just
writing the number without any symbol signifies that the number is
positive. Therefore, the numbers +4, +228, and +90335 can be, and are
better, expressed as 4, 228, 90335. Because the value does not display a
sign, it is referred as
unsigned as we learned in the previous lesson.
To express a variable as positive or unsigned, you can just type it. here is an example:
#include <iostream>
using namespace std;
int main()
{
int number = +802;
cout << "The value of the number is: " << number << "\n";
return 0;
}
Unary Operators: The Negative Operator -
|
|
As you can see on the above ruler, in order to express any number
on the left side of 0, it must be appended with a sign, namely the - symbol. Examples
are -12, -448, -32706. A value accompanied by - is referred to as negative.
The - sign must be typed on the left side of the number it is used to negate.
Remember that if a number does not have a sign, it is considered
positive. Therefore, whenever a number is negative, it MUST have a - sign. In the same
way, if you want to change a value from positive to negative, you can just add
a - sign to its left.
Here is an example that uses two variables. One has a
positive value while the other has a negative value:
#include <iostream>
using namespace std;
int main()
{
int number1 = 802;
int number2 = -62;
cout << "The value of the first number is: " << number1 << "\n";
cout << "The value of the second number is: " << number2 << "\n";
return 0;
}
Unary Operators: Increment and Decrement ++ and --
|
|
The increment operator is used to increase the value
of a number or variable by 1. The increment operator is performed with
the ++ operator. Because it involves a few more other issues, we will study it later on.
The decrement operator is used to decrease the value
of a number or variable by 1. The decrement operator is performed with
the -- operator. Because it involves a few more other issues, we will study it later on.
Unary Operators: The Address Of Operator &
|
|
In the previous lesson, we learned that, when
declaring a variable, the compiler reserves an amount of space in memory
for that variable. C++ provides an operator that can tell you where (the
space for) a variable is located. This is done using the
"address of" operator represented by the ampersand &.
To get the address of a variable, use the ampersand
operator on its left. The address is a hexadecimal number. Here is an example:
#include <iostream>
using namespace std;
int main()
{
int number = 46;
cout << "\n&Number = " << &number;
return 0;
}
|
This would produce:
&Number = 0012FED4
Unary Operators: The sizeof Operator
|
|
We have already seen that when declaring a variable,
the compiler reserves a portion of space in the computer memory to hold
that variable. We also illustrated how much space some data types need
to store their variable. C++ provides the unary sizeof operator
that allows you to find out how much space a data type or a certain
variable in your program is using.
There are four methods you can use with the
sizeof operator: using the variable or the data type. The general syntaxes of the
sizeof operator are:
sizeof VariableName;
sizeof DataType;
sizeof(VariableName);
sizeof(DataType);
Any of these four formulas is admissible. But it is
good to refrain from using the data type except in rare cases that you
can justify. The reason you should apply the sizeof operator on
the variable is because, if you change the variable (the word “variable”
means it can change throughout the program), that is, if you perform any
operation on it, the sizeof operator will acknowledge that and
render the right result. Some and most of the time, if you apply the
sizeof operator on the data type, you might end up with a bug;
if you are lucky, the program would simply not compile.
Here is an example:
#include <iostream>
using namespace std;
int main()
{
double period = 155.50;
int sizeOf = sizeof Period;
cout << "The size of Period is " << sizeOf << " bytes\n\n";
return 0;
}
Here is an example with various uses of the sizeof operator
// Use of the sizeof operator to find out how much
// space a variable or an identifier are using
#include <iostream>
using namespace std;
int main()
{
char iChar;
unsigned char uChar;
signed char sChar;
int iInt;
short int sInt;
unsigned int uInt;
unsigned short int uShortInt;
long int LInt;
unsigned long int uLongInt;
float iFloat;
double iDouble;
long double lDouble;
cout << "The sizeof operator used on a variable\n\n";
cout << "Identifier\t Memory Size\n"
<< "\t\t in Bytes\n";
cout << "----------------------------------";
cout << "\nchar\t\t\t" << sizeof(char);
cout << "\nunsigned char\t\t" << sizeof(unsigned char);
cout << "\nsigned char\t\t" << sizeof(signed char);
cout << "\nint\t\t\t" << sizeof(int);
cout << "\nshort int\t\t" << sizeof(short int);
cout << "\nunsigned int\t\t" << sizeof(unsigned int);
cout << "\nunsigned short int\t" << sizeof(unsigned short int);
cout << "\nlong int\t\t" << sizeof(long int);
cout << "\nunsigned long int\t" << sizeof(unsigned long int);
cout << "\nfloat\t\t\t" << sizeof(float);
cout << "\ndouble\t\t\t" << sizeof(double);
cout << "\nlong double\t\t" << sizeof(long double);
cout << "\n\n";
return 0;
}
This would produce:
The sizeof operator used on a variable
Identifier Memory Size
in Bytes
----------------------------------
char 1
unsigned char 1
signed char 1
int 4
short int 2
unsigned int 4
unsigned short int 2
long int 4
unsigned long int 4
float 4
double 8
long double 8
While the previous example uses the name of data
types, the following sizeof operators are used on the name of variables
// Use of the sizeof operator to find out how much
// space a variable or an identifier are using
#include <iostream>
using namespace std;
int main()
{
char iChar;
unsigned char uChar;
signed char sChar;
int iInt;
short int sInt;
unsigned int uInt;
unsigned short int uShortInt;
long int LInt;
unsigned long int uLongInt;
float iFloat;
double iDouble;
long double lDouble;
cout << "The sizeof operator used on an identifier\n\n";
cout << "Identifier\t Memory Size\n"
<< "\t\t in Bytes\n";
cout << "------------------------------";
cout << "\nchar\t\t\t" << sizeof(iChar);
cout << "\nunsigned char\t\t" << sizeof(uChar);
cout << "\nsigned char\t\t" << sizeof(sChar);
cout << "\nint\t\t\t" << sizeof(iInt);
cout << "\nshort int\t\t" << sizeof(sInt);
cout << "\nunsigned int\t\t" << sizeof(uInt);
cout << "\nunsigned short int\t" << sizeof(uShortInt);
cout << "\nlong int\t\t" << sizeof(LInt);
cout << "\nunsigned long int\t" << sizeof(uLongInt);
cout << "\nfloat\t\t\t" << sizeof(iFloat);
cout << "\ndouble\t\t\t" << sizeof(iDouble);
cout << "\nlong double\t\t" << sizeof(lDouble);
cout << "\n\n";
return 0;
}
In algebra, operations are performed on numeric values. Algebraic operators are
represented with the following symbols:
Practical
Learning: Introducing Operators
|
|
- Start your programming environment and create a new project named
GCS2.
Depending on your environment, if the file was not created already,
then create a source file and save it as, or name it, exercise.cpp
- Set the file's content as follows:
#include <iostream>
using namespace std;
int main()
{
char customerName[60], customerPhone[20];
unsigned short shirts;
unsigned short pants;
unsigned short dresses;
unsigned short ties;
double priceShirts = 1.25;
double pricePants = 2.75;
double priceDresses = 3.25;
double priceTies = 1.65;
int orderDay;
int orderMonth;
int orderYear;
cout << " -=- Georgetown Cleaning Services -=-\n";
cout << "Enter Customer Name: ";
cin >> ws;
cin.getline(customerName, 60);
cout << "Enter Customer Phone: ";
cin.getline(customerPhone, 20);
cout << "Enter the date this order was placed\n";
cout << "Order Day: ";
cin >> orderDay;
cout << "Order Month: ";
cin >> orderMonth;
cout << "Order Year: ";
cin >> orderYear;
cout << "Enter number of shirts: ";
cin >> shirts;
cout << "Enter number of pants: ";
cin >> pants;
cout << "Enter number of dresses: ";
cin >> dresses;
cout << "Enter number of ties: ";
cin >> ties;
cout << "\n====================================";
cout << "\n-=- Georgetown Cleaning Services -=-";
cout << "\n====================================";
cout << "\nCustomer Order";
cout << "\nCustomer Name: " << customerName;
cout << "\nCustomer Phone: " << customerPhone;
cout << "\nOrder Date: " << orderMonth
<< '/' << orderDay << '/' << orderYear;
cout << "\n------------------------------------"
<< "\nItem Type Unit Price Qty";
cout << "\n------------------------------------"
<< "\nShirts: " << priceShirts << " " << shirts
<< "\nPants: " << pricePants << " " << pants
<< "\nDresses: " << priceDresses << " " << dresses
<< "\nTies: " << priceTies << " " << ties
<< "\n\n";
return 0;
}
|
- Execute the application and perform the exercise
- Return to your programming environment
The addition is an operation used to add one number to another. For
example, if you have one pen and somebody gives you another pen, then
you have two pens. If you keep receiving pens, you will soon have more
than you had originally. In this manner, as you add pens, the number
grows. The addition gives you to ability to add things of the same
nature one to another, as many as necessary, until all items have been
added. Sometimes, the items are added one group to another. The concept
is still the same, except that this last example is faster. For example,
if you have a group of eleven students and you add 5 students to the
group, you can apply the same rule by adding one student at a time. But
to perform this operation faster, you should work to add groups of items
to one another.
The addition is performed in mathematics using the +
sign. The same sign is used in C++. The + sign is located on the left
side of the Backspace key. You get it by pressing Shift and =.
To get the addition of two values, you add the first
one to the other. After the addition of two values has been performed,
you get a new value. This means if you add Value1 to Value2, you would
write Value1 + Value2. The result is another value we could call Value3.
You can also add more than two values, like a + b + c.
The order you use to add two or more values doesn't
matter. This means Value1 + Value2 is the same as Value2 + Value1. In
the same way a + b + c is the same as a + c + b the same as b + a + c
and the same as c + b + a.
In C++, you can add values you know already, or you can use values the program gets from the user.
The simplest way to add values in C++ is by performing a simple sum. Here is an example:
// Program used to get the sum of two values
#include <iostream>
using namespace std;
iint main()
{
// Get the sum of two values
cout << "244 + 835 = " << 244 + 835;
cout << "\n\n";
return 0;
}
Here is the result:
244 + 835 = 1079
You can also add some values already declared and initialized in your program. Here is an example:
// Program used to get the sum of two initialized values
#include <iostream>
using namespace std;
int main()
{
int a = 244;
int b = 835;
// Get the sum of a and b
cout << a << " + " << b << " = " << a + b;
cout << "\n\n";
return 0;
}
You can also get the values from the user as illustrated in the following program:
// Program used to get the sum of two initialized values
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
// Get the sum of a, b, and c.
cout << "\nType the first value: "; cin >> a;
cout << "The second value: "; cin >> b;
cout << "The last value: "; cin >> c;
cout << a << " + " << b << " + " << c << " = " << a + b + c << "\n\n";
return 0;
}
Practical
Learning: Using the Addition
|
|
- To perform an addition, change the file as follows:
#include <iostream>
using namespace std;
int main()
{
char customerName[60], customerPhone[20];
unsigned short shirts;
unsigned short pants;
unsigned short dresses;
unsigned short ties;
unsigned short totalItems;
double priceShirts = 1.25;
double pricePants = 2.75;
double priceDresses = 3.25;
double priceTies = 1.65;
int orderDay;
int orderMonth;
int orderYear;
cout << " -=- Georgetown Cleaning Services -=-\n";
cout << "Enter Customer Name: ";
cin >> ws;
cin.getline(customerName, 60);
cout << "Enter Customer Phone: ";
cin.getline(customerPhone, 20);
cout << "Enter the date this order was placed\n";
cout << "Order Day: ";
cin >> orderDay;
cout << "Order Month: ";
cin >> orderMonth;
cout << "Order Year: ";
cin >> orderYear;
cout << "Enter number of shirts: ";
cin >> shirts;
cout << "Enter number of pants: ";
cin >> pants;
cout << "Enter number of dresses: ";
cin >> dresses;
cout << "Enter number of ties: ";
cin >> ties;
totalItems = shirts + pants + dresses + ties;
cout << "\n====================================";
cout << "\n-=- Georgetown Cleaning Services -=-";
cout << "\n====================================";
cout << "\nCustomer Order";
cout << "\nCustomer Name: " << customerName;
cout << "\nCustomer Phone: " << customerPhone;
cout << "\nOrder Date: " << orderMonth
<< '/' << orderDay << '/' << orderYear;
cout << "\n------------------------------------"
<< "\nItem Type Unit Price Qty";
cout << "\n------------------------------------"
<< "\nShirts: " << priceShirts << " " << shirts
<< "\nPants: " << pricePants << " " << pants
<< "\nDresses: " << priceDresses << " " << dresses
<< "\nTies: " << priceTies << " " << ties;
cout << "\n------------------------------------"
<< "\nTotal Number of Items: " << totalItems << "\n\n";
return 0;
}
|
- Execute the application and process an order. Here is an example:
Order Month: 12
Order Year: 2002
Enter number of shirts: 12
Enter number of pants: 8
Enter number of dresses: 4
Enter number of ties: 6
====================================
-=- Georgetown Cleaning Services -=-
====================================
Customer Order
Customer Name: Alain Kounkou
Customer Phone: (240) 843-8220
Order Date: 12/12/2002
------------------------------------
Item Type Unit Price Qty
------------------------------------
Shirts: 1.25 12
Pants: 2.75 8
Dresses: 3.25 4
Ties: 1.65 6
------------------------------------
Total Number of Items: 30
|
- Return to your programming environment
The multiplication allows adding one value to itself a certain number of
times, set by a second value. As an example, instead of adding a value
to itself in this manner: a + a + a + a, since the variable a is
repeated over and over again, you could simply find out how many times a
is added to itself, then multiply a by that number which, is this case,
is 4. This would mean adding a to itself 4 times, and you would get the
same result.
The multiplication is performed with the * sign which is typed with Shift + 8.
Just like the addition, the multiplication is associative: a * b * c = c * b * a.
When it comes to programming syntax, the rules we learned with the addition operation also apply to the
multiplication.
Here is an example:
// Multiplication of multiple values
#include <instream>
using namespace std;
int main()
{
float a, b;
// Multiple of a and b.
cout << "\nType two values: ";
cin >> a >> b;
cout << a << " * " << b << " = " << a * b;
cout << "\n\n";
return 0;
}
|
No comments:
Post a Comment