C++ Programming Basics: Comments, Preprocessor Directives, Namespaces, and Header Files, Slides of Computer Programming

An introduction to the basics of c++ programming, covering comments, preprocessor directives, namespaces, and header files. It includes examples of single-line and multi-line comments, the use of the #include preprocessor directive, and the declaration of namespaces and global variables. The document also explains the purpose of header files and the role of the main() function in c++ programs.

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CP
Lecture 2
Fall 2011
September 13-15, 2011
Ghufran Ahmed
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download C++ Programming Basics: Comments, Preprocessor Directives, Namespaces, and Header Files and more Slides Computer Programming in PDF only on Docsity!

CP

Lecture 2Fall 2011

September 13-15, 2011

Ghufran Ahmed

Basics

  • Comments in C/C++• Preprocessor Directives• Name Spaces• Header Files• Program Entry Point• Return Type

Basic C++ Program Structure

/***********************************************************^

Header Comments **********************************************************/pre-processor directivesglobal declarationsint main(){

declarations and executable statementsreturn 0; }//end block of main// comments required by some organizations!

/****************************************************** Sum two numbers******************************************************/#include <iostream.h>

//declares standard I/O library

int main()

//must have one and only one function named main

{

int number1, number2;

//declare two integer variables

cout << “enter two integers” << endl;

//prompt for input

cin >> number1 >> number2;

//input values for two variables from keyboard

cout << number1 + number2;

//output sum of two numbers to the screen

return 0; }//end block of main

Dissection of Program

Comments have two forms in C++

-^

//Single line comments

-^

/Multi-line comments/

/ Program #1 - A first C++ program.*

Enter this program, then

compile and run it.

*/ •^

This is a

comment

(C style)

-^

Ignored by the compiler i.e. can write anything and thecompiler won’t see it at all

-^

Multiline or single or mixed with code.

-^

Why use comments?

Dissection of Program

//

main()

is

where

program

execution

begins.

•^

C++ style comment

-^

Compiler ignores the whole line

-^

If multiline comments, use // on each line

Dissection of Program

cout

<<

“Hello,

world!";

•^

Think of

cout

as the monitor screen (

console output)

<<

gives direction of data;

Put to

or

Insertion

operator

•^

Prints the entire string within quotes to the screen

-^

This whole line is a statement

-^

C/C++ Statement

that ends with a ;

Dissection of Program

#include

Tells

preprocessor

to include contents of the text file

iostream with your source code before compiling

-^

iostream has details about things that handle inputand output e.g. cout

-^

iostream is called an

include file

or

header file

•^

This is called an

include

directive

or

preprocessor

directive

-^

Can also use <iostream.h>; then no need to use the namespace

construct on the next slide. Older version

#include files

  • <filename.h> //Standard C library header file– //Standard C++ library files
    • Files found in the directory defined by the INCLUDE

environment variable

  • “myfile.h” //Your files
    • Files found in the current directory

Simple I/O

cin

  • streams input from standard input– uses the >> operator
    • (input operator, stream extraction operator)

cout

  • streams output to standard output– uses the << operator
    • (output operator, stream insertion operator)

Rise&Shine Algorithm

Get out of bed

Take off pajamas

Take a shower

Get dressed

Eat breakfast

Carpool to work

-^

Any comments?

A Variation of Rise&Shine

Get out of bed

Take off pajamas

Get dressed

Take a shower

Eat breakfast

Carpool to work

-^

Any comments?