Coding is analogically(and structurally) like a language. The way societies are built from the ground up on the basis of language and communication, computers are built from the ground up on lines of code. These lines of code, execute a task which they are specifically asked to carry out. The hardware of a system provides the power needed to push a command while the software is a series of instructions and task details. That is the essence of coding. Its initiation seems abstract and its application vague. But bit by bit (or may I say byte by byte) you’re contributing your part to the digital era.
As an introduction to C++ coding, I would like to provide an overview into its basics. C++ is a language which differentiates itself from other languages because of its additional features of Object Oriented Programming. By definition, we could code, lets say, a car. To add features to this car, we would create a class for it called Car. In this car we could define the name of the car, its features, registration no. etc. Now we have for ourselves a well described car with all its characteristics stored in our data. Lets say we are a company which records the sale of a thousand such cars daily. Instead of repeating our code over and over, object oriented programming allows us to creates objects, derived from this class which have all the characteristics of the class, but specific to the object. The ability to create such objects and classes and inheriting data members from one class to another is what sets C++ aside from most other languages.
C++ has a few essential elements: each C++ code has a mandated main function, which is the primary code in which tasks place. The main function is the only mandatory part of every code. To use predefined functions, the compiler (the software which executes a code) includes header files, which act as files which contain code for the more usual commands like inputting data, outputting data, mathematical operations etc. Header files act as preprocessor directives (just fancy terminology) and allow you to circumvent the need to code some basic commands. The last essential element of the language are keywords, these preexisting words are what helps you access the codes in the header files.
#include <name of header file> //syntax to invoke a header file
int main() //syntax for main function
cout<<“Hello world! ”;//cout is a keyword present in the header file <iostream> used for outputting data
That was the most basic of all C++ codes.
To carry out algebraic tasks one must first define data members.
C++ has a large amount of data types. For the more conventional coding we use the following data types:
int – integer data type
char – character data type
float – decimal data type
long int – integer data type with double the digits
long float – decimal data type with double the digits.
To define a variable, we need a variable name. The name of a variable can be anything except a keyword.
int x , y , z; // initialises three variables of integer data type called x, y and z
int x=2; // initialises an integer x with the value 2
A variable whose value hasn’t been defined has a junk value.
Now that our data members have been allotted lets carry out some mathematical tasks.
To add two numbers we simply create a linear equation equated to a variable:
x=2+3; // sets x’s value to 5
We can also allot the sum, product, difference of two variables to a third variable
z=x*y; // sets z to the product of x and y
The value of variables can be further incremented, decremented or changed by the following short cuts:
x++; // increments x by 1
x- – ; // decrements x by 1
x+=4; // adds a definite value to x
and so on…
x==5; //is a statement which compares if x is equal to 5 or not, it doesn’t allocate the value 5 to x
x> 5 or x<5; // checks of x is greater than or less than a value
Using just these you can now go run the most basic of codes. The tutorial on C++ and applications of coding shall continue in the next issue.
To go create your own codes visit cpp.sh, the online compiler.