Practical Programming 01: Introduction to C++

Nextrick
5 min readJul 25, 2023

--

Introduction to C++ header image

What is Programming ?

A technological process for telling a computer which tasks to
perform in order to solve a problem or to gain any desired outcome. In other words, it is a conversation with the computer.

Types of Programming Languages

There are three types of programming languages:

  1. High-level programming language
  2. Mid-level programming language
  3. Low-level programming language

Python, java and javascript are high-level programming languages with built-in functions. Whereas, C++, C# and R are mid-level programming languages. Machine language and Assembly language are low-level programming languages. In this specific course, we will learn about C++ programming language.

Introduction to C++

C++ language was formed in the early 1970’s. It was called C with
classes. There are many different features in C++. Object-Oriented Programming is done in C++ with many important concepts such as encapsulation, abstraction, inheritance and polymorphism. C++ is very fast and efficient and furthermore we will be studying memory management.

HelloWorld Program

Basic C++ programming

The above code is written in C++ programming language. The program is written on visual studio code (compiler to write and execute the code). The code started from the header.

Whenever we are writing a C++ program, we will always start our program with these header. #include <iostream> means that iostream is the library for input and output stream functionalities. Next, using namespace std means that it tells the compiler to use “standard namespace” avoiding name conflicts. After this, we write our main function starting from int main(). Every program in C++ has a main function which is run first. Then this main function has its own body. The body of function is enclosed in
parenthesis. Every C++ statement ends with a semicolon(terminator). It’s very basic and important at the same time. If you ever forget to put a semicolon after writing the correct code, the compiler will give you an error. This type of error is called syntax error and C++ is case sensitive. If you are writing a C++ program for hello world, you need to write cout<<”Hello World”<<endl; Now whenever we execute a statement in C++ we need to write cout. Cout uses << stream insertion operator. The statement ends with endl which means the program will ultimately go to the next line. The program ends with return 0; There are two types of file. One is called a source file that contains the source code. The file extension is .cpp. The other file is called an executable file which ends with the extension of .exe.

Difference between IDE and Compiler

IDE stands for integrated development environment. It’s a software application that provides comprehensive tools and features to programmers and developers for writing, testing and debugging code. We will use Visual Studio code as IDE. Compiler is a software program that translates source code written in a high-level programming language into a lower-level programming language, typically machine code or bytecode, that can be directly executed by computer’s hardware or a virtual machine. The process of compilation is called compiling. We will use MiniGW as a compiler.

Image explaining how compiler works
How compiler works

Errors

Syntax errors or compile-time errors come when some punctuation is
missing or some alphabet which is supposed to be small is written capital. C++ is a case sensitive language. Run-time errors or logic errors appear when a program fails to provide the required task.

Data Types in C++

There are different data types used in C++.

  1. Int: Int type refers to an integer. Integers can be positive and negative. It doesn’t have a fractional part; for example 3 is an int and 3.00 is not. Int doesn’t store fractional part and it is automatically dropped. Int takes 4 bytes of memory. The value ranges from -2,147,483,648 to 2,147,483,648.
  2. Float: Float refers to floating point numbers or numbers with fractional part. It also takes 4 bytes of memory but has a precision of 6 decimal places. It can store single-precision floating point numbers in the range of about +- 10 raised to power 38.
  3. Double: Double is used to represent double-precision floating point numbers. It is similar to float but provides a precision of 15 decimal places. It uses 8 bytes of memory. The value range that double can hold is +-10 raised to power 308.
  4. Char: Char is used to represent individual characters. This data type can not store more than one character. Character includes an alphabet, a punctuation mark, whitespace character and special symbols. It takes 1 byte of the memory.
  5. String: String is a dynamic array of characters. Its size in memory depends on the length of the string. The size of an empty string is 24 bytes.
  6. Bool: Bool data type represents Boolean values. The values can be either true or false. False is stored as 0 and true is stored as 1. It takes 1 byte of memory.

Variables

In programming, a variable is a storage location with a name that holds a value. Variables can be of different data types. In C++, it is necessary to specify the type of data type while declaring a variable. Values are assigned using the assignment operator “=” . The value of the variable can be changed. According to coding conventions, variable names should be lowercase. For example, int x=2;

Constants

Constants are like variables but their value can not be changed once assigned. In C++, the keyword const is used for declaring a constant variable. According to coding conventions, the name of constants should be in uppercase. For instance, const double PI=3.14;

Arithmetic Operators

The arithmetic operators that are used in C++ are addition (+), subtraction (-), multiplication (*), division (/), modulus (%) also known as remainder, increment (++), and decrement ( — ) which will be explained in the next lesson.

This lesson is explained in details in the video:

If you are interested in learning more about practical programming, you can subscribe us on our youtube channel: @NextrickLearning and discuss with others on our forum: nextric.freeflarum.com

Content written by Abia Javed

--

--

Nextrick
Nextrick

Written by Nextrick

Provide free education lectures for anyone, designers of next-Gen education system tools developed by the company will be used in AI-oriented education.

Responses (1)