Lect # 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

PROGRAMMING

FUNDAMENTALS
Instructor: Ms. Madiha Rehman
Objectives

 Basic Structure of C++ Program


 Preprocessor Directives
 Main Method
 Using namespace std;
 Dev C++ installation
Basic Structure of a C++ Program

 There are three main parts of a C++ Program


I. Preprocessor directive or Compiler directive
II. Main () Function
III. Program body (C++ Statements)
A Simple C++ Program
Preprocessor Directive
 It is an instruction given to the compiler before the execution of actual program
 Preprocessor directives are lines included in the code of program preceded by a hash sign
(#)
 These directives are always written at the start of program code, i.e., (the very first line of
your source code)
 Mainly pre-processor directives are used to include header files in your source code.
 “Header Files are collection of standard library functions to perform different tasks” for
example, math.h
 Example
 #include <iostream>
 The above directive tells the compiler to include input / output library in your source code.
 Many different header files can be included in one program of c++.
Main Method
 Starting point of a C++ program
 Example:
int main()
{
// C++ Lines of Code
}

Description
 Main() must return int. The return value is passed back to the operating System to indicate whether the
program ran successfully.
 We can leave the int from main, in that case it will always return zero
using namespace std;
 std is the standard namespace, cout, cin and a lot of other things are defined in it.
 To omit writing std:: , we need to include the std namespace, i.e.,

#include <iostream>
using namespace std;
int main() { cout<<“Hello World”; }

 This means that one way to call them is by using std::cout and std::cin. And another way is to add the
namespace.
 The keyword using technically means, use this whenever you can. This refers, in this case, to the std
namespace.
Class Activities
 Class Activity 1: Write a C++ Program to display the following output:
Hello World
C++ Programming is fun

 Class Activity 2: Create and execute a program that outputs your name on one line and your age on
the next line.

 Class Activity 3: The following program produces several compiler errors. Find these errors and
correct them so the program can compile cleanly and run.
include <iostream>
Int main()
{
std:cout << "Hello World" << std:endl
)

You might also like