Understanding C++ and OOPs with an Example – Minimal Guide 2018

Each person can have his own reason for programming but it is one of the best ways to gain a deep understanding of computers and computer technology. Learning to program makes you understand why computers and computer programs work the way they do. It also puts some sense into you about how hard it is to create software.

What is C++ & OOP?

C++ is an extended version C. C was developed at Bell Labs, in 1978. The purpose was to create a simple language (simpler than assembly & machine code) which can be used on a variety of platforms. Later in the early 1980’s C was extended to C++ to create an object-oriented language. O(bject) O(riented) P(rogramming) is a style of programming in which programs are made using Classes.

A class id code in a file separate from the main program – more on classes later. OOP in general & C++ in particular made it possible to handle the complexity of graphical environments. (like windows, macintosh..)

What do I need to program?

Well, you need a computer and a compiler to start with but you also need some curiosity and a lot of time. You can find different compilers for free from borlands website. If you have the curiosity but lack in time read stuff at lessons and detention hours. Read whenever you find time.

YOUR FIRST PROGRAM – Running a C++ Program


A C++ program must be compiled and linked before it can be executed, or run, on the computer. A great lot of compilers do this automatically. So what is a compiler? A compiler is a program that translates C++ code into machine language. Machine language is the language consisting of 1s and 0s, and is the native language of a computer.

A typed C++ program is called the source-code, and the compiled code is called the object code. Before the object code can be executed, it must be linked to other pieces of code (e.g. included libraries) used by the program. The compiled & linked program is called an executable file. Finally, the program is executed by the system. It’s output is displayed in a window.

C++ Program Structure

All C++ programs contain statements (commands) that tell the computer what to do.

Here is an example of a simple C++ program:

#include

int main()
{
cout<<“Hello Yeahhub”; // the first statement
return(0); // the second statement
}

Run the program. It should display :

Hello Yeahhub

The structure of a simple C++ program is:

#include

int main()
{
statements; // comments
return(0);
}

Now we will have a closer look on the structure. The whole program is divided into 5 main sections:

  • Comments
  • Libraries
  • Functions
  • Streams
  • Return

1. Comments


Comments are used to explain the contents of a program for a human reader. The computer ignores them. The symbols /* and */ are used for the beginning and end of a comment for multi-line comments. // symbols are also used for commenting. All characters on a line after the // symbol are considered to be comments and are ignored.

Most newbies think that commenting a program is a waste of time. They are wrong. Commenting is very important because it makes the code understandable by other programmers and makes it easier to improve a program or fix the bugs in it. You’ll understand better after trying to decipher a hundred pages of code you wrote a few months later.

2. Libraries


Look at the program above. Following the opening comment was the line:

#include

This line simply tells the computer that the iostream library is needed therefore it should be included. A library is a collection of program code that can be included (and used) in a program to perform a variety of tasks.

iostream is a library – also called as a header file, look at its extension – used to perform input/output (I/O) stream tasks. There are a lot of non-commercial C++ libraries for various purposes written by good guys who spent more than enough time in front of their computers.

3. Functions


The next line in the program was:

int main()

Which is the header of the main function. Makes sense? No? A function is a set of statements that accomplish a task. A function header includes the return type of the function and the function name.

As shown in the main() header, main returns an integer(int) through return(0). So all the functions that have an integer as the return type returns integers. Very clear. The statements in a function (in this case the main function) are enclosed in curly braces. The { and } symbols indicates the beginning and the end of statements.

4. Streams


What is a stream? In C++ input/output devices are called streams. cout (we used above) is the c(onsole) out(put) stream, and the send (insertion) operator is used to send the data “Hello Yeahhub” into the stream. In the first statement:

cout<<“Hello Yeahhub”;

The words following the << operator are put in quotation marks(“) to form a string. When run, the string Hello Yeahhub is sent to the console output device. Yes, it is also called the computer screen.

Important note: C++ is case sensitive. That means cout and Cout is not the same thing.

5. Return


The second statement was:

return(0);

which causes the program to terminate sending the value 0 to the computer. The value “0” indicates that the program terminated without error.

Note: The statements end with a semicolon (;). A semicolon in C++ indicate the end of a statement.

DATA & NUMBER SYSTEMS


  • Decimals

The base 10 number system. Uses 10 digits: 0 to 9. Numbers raised to the zero power is equal to one.

For example: 5 to the power 0 = 1. Base ten equivalent of the number

2600 = 2 x (10 to the power 3) + 6 x (10 to the power 2)
33 = 3 x (10 to the power 1) + 3 x (10 to the power 0)

  • Binaries

The base 2 number system. Uses 2 digits : 0 and 1. Works the same as base 10 except we multiply numbers by the powers of 2 instead.

For example 110 is equal to 6 in base 10:

110 = 1 x (2 to the power 2) + 1 x (2 to the power 1) = 6(base10)

  • Hexadecimal

The base 16 number system. Uses 16 digits. 0 to 9 & “A” to “F”. Works the same as base 10 & base two except the numbers are multiplied by the powers of 16 instead:

1B = 1 x (16 to the power 1) + 2(B) x (16 to the power of 0) = 30(base10)

You may also like:

Sarcastic Writer

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

Related Posts