INTRODUCTION TO C++ PROGRAMMING
- Differences Between C and C++ Programming
- Compiler
- C++ IDE
- Creating and running C++ Program
- Variables
- Scope of variables
Explanations to week content
The program that you write should be clear not only to you, but also to the reader of your program. Part of good programming is the inclusion of comments in the program. Typically, comments can be used to identify the authors of the program, give the date when the program is written or modified, give a brief explanation of the program, and explain the meaning of key statements in a program.
Comments are for the reader, not for the compiler. So when a compiler compiles a program to check for the syntax errors,it completely ignores comments.
Single Line Commenting
// is used to comment on one line.
Single-line comments begin with // and can be placed anywhere in the line. Everything encountered in that line after // is ignored by the compiler.
Examples
// My first comment
// Variable for firstname
char first_name;
//prints: 7 + 8 = 15
cout << "7 + 8 = " << 7 + 8<< endl;
You can put comments at the end of this line as follows:
cout << "7 + 8 = " << 7 + 8<< endl; //prints: 7 + 8 = 15
Block Commenting
Block comment begins with /* and ends with */
Thus:
/* comment line 1
Comment line2
Comment line3
…
*/
Examples:
/*
Last updated 07/06/2019
An application to register students
Variables And What they stand for
first_name : for first name ofthe student
surname : for the surname of the student
age: for the age of the student
*/
The variable cin has access to operators and functions that can be used to extract data from the standard input device.
Input Statement
Data can be extracted from the standard input device by using the cin and the extract operation.
The syntax of an input statement using cin and the extraction operator >> is:
cin >>variable;
e.g
inta,b,c;
cin>> a;
a = value entered
cin>>b;
b = value entered
cin>>c;
c = value entered;
Youcan extract the multiple data at once using just a single cin in the format below.
cin >> variable>> variable...;
Thus:
cin >> a>> b >> c;
Output Statement
The syntax for output of is:
cout<< expression or manipulator << expression or manipulator...;
Examples
cout<< “My name is Godwin Ashong”;
cout<< “I am a Ghanaian”;
In order to print out on a new line, the syntax endl is added to the syntax prior to the syntax that prints the text on a new line;
The above syntax prints out “My name is Godwin Ashong I am a Ghanaian”
In order to print out:
My name is Godwin Ashong
I am a Ghanaian
The syntax << endl needs to be added to the cout <<”My name is Godwin Ashong”
Thus the new syntax will be:
cout<< “My name is Godwin Ashong” <
cout<< “I am a Ghanaian”;
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
There are following arithmetic operators supported by C++ language −
Assume variable A holds 10 and variable B holds 20, then −
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
- | Subtracts second operand from the first | A - B will give -10 |
* | Multiplies both operands | A * B will give 200 |
/ | Divides numerator by de-numerator | B / A will give 2 |
% | Modulus Operator and remainder of after an integer division | B % A will give 0 |
Examples
+ | Addition | int a,b,c; a = 1; b = 2; c = a+b; cout << a<<"+"<<b<<"="<<c<<endl; c = b + a; cout << b<<"+"<<a<<"="<<c<<endl; |
---|---|---|
- | Subtraction | int a,b,c; a = 1; b = 2; c = a-b; cout << a<<"-"<<b<<"="<<c<<endl; c = b - a; cout << b<<"-"<<a<<"="<<c<<endl; |
* | Multiplication | int a,b,c; a = 1; b = 2; c = a*b; cout << a<<"x"<<b<<"="<<c<<endl; c = b*a; cout << b<<"x"<<a<<"="<<c<<endl; |
/ | Division | int a,b,c; a = 1; b = 2; c = a/b; cout << a<<"/"<<b<<"="<<c<<endl; // This will output 0 c = b/a; cout << b<<"/"<<a<<"="<<c<<endl; NOTE: When using the “/”, it is better to declare the variables as float in order to have decimal values. Only the whole number part of the decimal will be stored in the result if the variables are declared int. Example int a,b,c; a = 3; b = 2; c = a/b; cout << a<<"/"<<b<<"="<<c<<endl; // This will output 1 instead of 1.5 c = b/a; cout << b<<"/"<<a<<"="<<c<<endl; // This will output 0 NOTE: Even the result of c will still be the same if c is declared float as illustrated below. int a,b; float c; a = 3; b = 2; c = a/b; cout << a<<"/"<<b<<"="<<c<<endl;//This will output 1 instead of 1.5 c = b/a; cout << b<<"/"<<a<<"="<<c<<endl; // This will output 0 It is therefore necessary to declare all the variables in which the “/” operator is used as float in case there is a possibility of having a decimal resut. Thus the above code could be written as: float a,b,c; a = 3; b = 2; c = a/b; cout << a<<"/"<<b<<"="<<c<<endl; //This will output 1.5 c = b/a; cout << b<<"/"<<a<<"="<<c<<endl; // This will output 0.666667 |
% | Modulus | int a,b,c; a = 5; b = 2; c = a%b; cout << a<<"%"<<b<<"="<<c<<endl; // This will output 1 c = b%a; cout << b<<"%"<<a<<"="<<c<<endl; // This will output 2 NOTE: Modulus only work when the variables are declared int. If any of the variables is declared float, the will error in execution. Example float a,b,c; a = 5; b = 2; c = a%b; cout << a<<"%"<<b<<"="<<c<<endl; c = b%a; cout << b<<"%"<<a<<"="<<c<<endl; When the above code is run, the following error is produced: error: invalid operands of types 'float' and 'float' to binary 'operator% |
Constant
The keyword const means that a variable cannot be changed once it has been declared and
initialized.
The syntax below is used to declare a constant.
const variable type variable name = value;
Thus you type const then the variable type and leave a space and then variable name then assign the value of the constant.
Examples
const double PI = 3.1415926535;
const double acceleration = 9.8;
1. Write an application in C++ that calculates the area of a circle when a user enters the radius
2. Write an application in C++ that calculates the force on a body when the mass is entered.
SOLUTION CODES
1. APPLICATION TO CALCULATE THE AREA OF A CIRCLE
#include <iostream>
using namespace std;
/*
An application to calculate the area of a circle
Formula of area of a circle = pi x radius x radius
*/
int main()
{
const double PI = 3.1415926535;
float radius,area;
//Alert user to enter the radius
cout << "Please enter the radius of the circle"<<endl;
//Get the radius of the circle from the user
cin >> radius;
//Calculate the area of the circle
area = PI*radius*radius;
// Display the value of the area to the user
cout << "The area of the circle of radius "<<radius<<" is "<<area<<endl;
return 0;
}
2. APPLICATION TO CALCULATE THE FORCE OF A BODY
#include <iostream>
using namespace std;
/*
An application to calculate the force of a body
Formula of calculating the force of a body = mass x acceleration due to gravity
*/
int main()
{
const double acceleration_due_to_gravity = 9.8;
float mass,force;
//Alert user to enter the mass
cout << "Please enter the mass of the body"<<endl;
//Get the mass from the user
cin >> mass;
//Calculate the force
force = mass*acceleration_due_to_gravity;
// Display the value of the area to the user
cout << "The force of the body of mass "<<mass<<"kg is "<<force<<"N"<<endl;
return 0;
}
Write an application in C++ that takes the temperature in degree Celsius and concerts it to Fahrenheit.