Introduction to C++ For Complete Beginners | Free Course Certification Acadpedia

Introduction to C++ For Complete Beginners | Free Course Certification Acadpedia

Andrew Scott
Friday 19 June 2020

C VS C++

C++ was developed by Bjarne Stroustrup at Bell labs in 1979, as an extension to the C language.

The major difference between C and C++ is that C is a procedural programming language; which means that it is derived from sequential step by step structured programming. Some of it's applications includes scheduling running of other programs, designing games, graphics etc.

On the other hand C++ is a combination of procedural programming as well as object oriented programming. Objects consists of Data in form of it's characteristics and are coded in the form of methods. In object oriented programming computer programs are designed using the concept of objects that interact with the real world.

C++ For Complete Beginners With Certification



Some other differences includes:

Since C is a procedural programming, it is a function driven language.

C++ is an object driven language.

In C, functions cannot be defined inside structures

In C++, functions can be used inside a structure.

C uses functions for input/output. For example scanf and printf.

C++ uses objects for input output. For example cin and cout.

C does not provide direct support for error handling (also called exception handling)

C++ provides support for exception handling. Used for errors that make the code incorrect.

C does not support reference variables.

C++ supports reference variables.


C++ Syntax

 

The following code is written in C++

We shall look at each element of the following code in detail to understand the syntax of C++

 

123456789#include <iostream>
using namespace std;

int main() 
{
    cout <<"Hello World.";
    return 0;
}

 

#include <iostream>: This is a header file library. <iostream> stands for standard input-output stream. It allows us to include objects such as cin and cout, cerr etc.

 

using namespace std: Means that names for objects and variables can be used from the standard library. It is also used as additional information to differentiate similar functions. 

 

int main(): The function main is called just as in C. Any code inside its curly brackets {} will be executed.

 

cout: is an object used to print a particular text after << in quotes. In our example it will output "Hello World". (for personal reference we can say it is similar to printf in c)

 

return 0: Terminates the function main

 

Note:

1) Every C++ statement ends with a semicolon ';'

2) Compiler ignores white spaces. Multiple line spaces are used to make the code more readable.

 

Omitting Name spaces:

C++ programs run without the standard namespace library. This can be done by writing std keyword followed by :: operator inside int main()

Example:

#include <iostream> 

int main() 

std::cout <<"Hello World"; 

return 0; 

}

Output(Print Text)

 

Using the cout object

As discussed earlier the cout object, together with the << operator, is used to output values/print text.

 

You can add as many cout objects as you want. However, note that it does not insert a new line at the end of the of each object all of them will be printed in a single line.

 

Example:

 

12345678910111213#include <iostream>
using namespace std;


int main() {
  cout << "Demo Code.";
  cout << "I am learning C++.";
  cout << "Print text.";

    return 0;
}

Output:

1Demo Code.I am learning C++.Print text.

 

 

New Lines

 

In order to insert a new line after each object declaration \n is used. Or another way to do so is using end1 manipulator

123456789#include <iostream>
using namespace std;
int main() {
  cout << "Demo Code.\n";
  cout << "I am learning C++" <<end1;
  cout << "print text";

  return 0;
}

 

Output:

12345Demo Code.
I am learning C++
print text

Note: \n is the preferred way to break lines.


User Input

 

As we have already discussed earlier cin is used to get user input. This is paired along with the extraction operator (>>)

 


The following example reads a value from the user and prints it on the screen.

Example:

 

123456789101112#include <iostream>
using namespace std;

int main()
{
  int num;
  cout << "Type a number: "; // displayed on the screen
  cin >> num; //value taken from User
  cout << "Your number is: " << x; // number displayed on the screen

  return 0;
}