C++ Deep Dive

Prateek Narang

Topics

  • Header Files

  • Preprocessor Directive

  • main()

  • Keywords & Identifiers

  • Comments

  • Namespaces

Hello World!

#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}

Header files contain the set of predefined standard library functions & data type definitions. Include a header file in your program by including it with the preprocessing directive “#include

Header File

#include<iostream>
#include<algorithm>
#include<string>
...

The preprocessors are the directives, which give instructions to the compiler to preprocess some code before actual compilation starts.
The directive begins with '#'

The actual code is compiled by compiler only

Preprocessor Directive

#include<iostream>
#include<algorithm>
#include<string>
...
#include  
directive tells the compiler to include the header file in the source code.

#define 
directive tells the compiler to create symbolic constants. The symbolic constant is called a macro
#include<iostream>
#define PI 3.14
int main(){
	//Area of Circle
 	int r = 5;
 	float area = PI*r*r;
	return 0;
}

iostream: iostream stands for standard input-output stream. This header file contains definitions of objects like cin, cout etc.

<iostream>
#include<iostream>
...

int main(){
	cout <<"Hello!";
}

The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device

cout
cout << "Hello!";

The cout object in C++ is an object of class ostream. It is defined in iostream header file. It is used to display the output to the standard output device. More than one variable can be printed using the insertion operator(<<) with cout

cout
String name = "Prateek";
cout << "Hello " << name ; // Hello Prateek
#include<iostream>
#include<string>
using namespace std;


int main(){

	// Your Code Goes Here
	String name = "Prateek";
	cout <<"Hello World!";
	cout << 5 << endl;
	cout <<"Hi " + name;
	
	return 0;
}

Introduction to Computers & Programming Languages

Prateek Narang

Topics

  • Computer Applications

  • Programming Language

  • Source Code

  • Compiler

  • How Computers work?

Computers are everywhere!

  • Cab Booking
  • Shopping
  • Food Ordering
  • Instagram 
  • News
  • Weather Forecasts

What does a computer do?

Input Data

Output Data

What does a computer do?

Location (lat,long)

Weather Forecast

Weather Forecast App

(40,70,30)

Sum = 140

Simple Calculator

Behind the scenes, there is a computer application, that does all the work!

Its  the Programmer, who writes all this code 👨‍💻 

A computer program is a set of instructions, written in a programming language.

 

Computer Program/Application

We need Language to communicate our ideas!

English, Hindi,

German, French

etc.

We need Language to communicate our ideas!

C++, Java, Python, JavaScript etc

We need Language to communicate with a computer too!

There are many programming languages like C++, Java, Python, JavaScript, Go, Kotlin, DART etc. A computer program is a set of instructions, written in a programming language.

 

 

Programming Language

Sample C++ Code

#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}
#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}

Source Code

Computers don't understand Programming Language Directly!

#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}

Source Code

A computer program in its human-readable form is called source code

Source code needs another computer program to execute because computers can only execute their native machine instructions. Therefore, source code may be translated to machine instructions using the language's compiler.

#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}

Source Code

Compiler

Machine language is a set of instructions executed directly by a computer’s central processing unit (CPU). Each instruction performs a very specific task, such as a load, a jump, or an ALU operation on a unit of data in a CPU register or memory. Every program directly executed by a CPU is made up of a series of such instructions.

#include<iostream>
using namespace std;


int main(){
	// Your Code Goes Here
	cout <<"Hello World!";
	
    return 0;
}

Source Code

Compiler

Once the instructions are fed, the computer program is ready to accept input data and generate required outputs.

Output

Input 

Logic

Variable name: A label for a memory location

Value: The something that would be stored in a variable

Storage: A place where data can be stored

Declaration: Announcing a variable (usually) at the beginning of a program

Naming convention: A set of rules about the names of variables

Assignment: Giving (setting) a variable a value

Variables

// Valid names
int topics = 3
int marks = 20
int student_1 = 5

// Invalid Names
1_student = 8;

Naming Convention

  • For variable name we can use uppercase and lowercase letters, digits from 1 to 9 and underscore(_).
  • First character must be underscore or letter.
  • C++ is strongly typed language. So every variable needs to be declare before using it

Variable Initialisation

Variables when just declared have garbage value until they are assigned a value for the first time.

We can assign a specific value from the moment variable is declared, called as initialisation of variable.

// Declare
int marks;

// Declare + Assign (Init)
int x = 10;

CODE DEMO

[Topics C++ 04] Diving Deeper

By Prateek Narang

[Topics C++ 04] Diving Deeper

  • 14