0% found this document useful (0 votes)
52 views

Ist Lecture On C++

This document provides an overview of the key components of a C++ program, including: 1. It discusses the basic structure of a C++ program including header files, the main function, and output statements using cout. 2. It explains various operators and functions in C++ like cout, cin, namespaces, and escape sequences. 3. It provides examples of simple C++ programs that demonstrate input/output, comments, and calculations to illustrate how to write interactive C++ programs.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Ist Lecture On C++

This document provides an overview of the key components of a C++ program, including: 1. It discusses the basic structure of a C++ program including header files, the main function, and output statements using cout. 2. It explains various operators and functions in C++ like cout, cin, namespaces, and escape sequences. 3. It provides examples of simple C++ programs that demonstrate input/output, comments, and calculations to illustrate how to write interactive C++ programs.

Uploaded by

Abhishek Modi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Structure of a C++ Program

Date: 17/01/11

Outlines
Introduction Components of a C++ Program Escape Sequences Variable declaration & Memory allocation Namespaces User Interactive Programs Formatting the Output

Introduction
C++ improves on many of Cs features. C++ provides object-oriented programming (OOP). C++ is a superset to C.

History
C was designed by Ritchi, Tompson, Kernighan It was fast, good & powerful

but programs maintance was a nightmare! Solution: ... Object-Oriented Programming

Some New Operators in C++


>> Extraction Operator extracts data from input stream. << Insertion Operator inserts data into output stream :: Scope Resolution Operator used to: 1. Override the default scope of variables. 2. Specify the scope of member functions.

Components of C++ program


Header files INT MAIN() or VOID MAIN() Output Statement- Use Of COUT The CIN

Simple C++ Program


#include<iostream.h> int main() {
Std :: cout << Hello, Welcome to programming with C++ !\n ; Std :: cout << Are you interested to joinc? << std :: endl ; return 0 ;
}

HEADER FILES
#include <iostream> #include <cmath> #include <cstdlib>

INT MAIN OR VOID MAIN


#include<iostream> int main() {Statements ; If(expression) {statement ; ---------------; } Statement ; // comment return 0 ; }

Output Statements
The first output statement of previous program
std :: cout << Hello, Welcome to programming with C++!\n ;

The line std::cout means that we are calling an object cout which is defined under namespace std or which belongs to header file <iostream.h> of C++ stander library. The object cout is used with the stream insertion operator(<<).

Using cout and Insertion operator


Screen

cout

<<

C++ variables

Escape Sequences
Some character preceded by slash character(\) like the one at the end of the above output statement, i.e. \n having a special meaning for the compiler. Back Slash (\) is an escape character. \n is new line character. Thus, std::cout<<\n; means output shifted to the next line.

Escape Character
\a \b \f

Description of action
Bell or beep is generated by the computer on program execution. Back space. Move cursor to previous position From feed Advance cursor to next page

\n
\r \t \v

Shift to next line


Carriage return. Position the cursor to the beginning of current line. Horizontal tab. Vertical tab.

\\
\ \? \0

Displays a back slash character (\)


Displays a single quote character() Displays a question mark(?). Null termination character. Signifies end of a character string.

\o
\x \

Code for octal numbers.


Code for hexadecimal numbers respectively Displays a double quote character().

Application of some escape sequence


#include<iostream> int main() { std::cout<<Hello,\a\Welcome to programming with C++!\\n; /*Inclusion of character \a will cause the computer to generate a sound (a beep) when program is executed*/ std::cout<<Hello,\\ Welcome to programmingwith C++!\\\n; // This prints \ see 2nd line of output. std::cout<< Wellcome to programming with C++ !\rHello\n; // inclusion of \rHello brings Hello to front of line. std::cout<<Hello\tWelcome to programming with \tC++!\\n; // The character \ puts double quotes to the statement return 0 ; }

Output
The expected output is : Hello, Welcome to programming with C++! Hello,\ Welcome to programming with C++!\ Hello Welcome to programming with C++! Hello Welcome to programming with C++!

Comments
A comment introduced by first putting double slash (//) followed by the comment up to the end of the line. If the comment is long and goes to the next line, another double slash (//) is needed before the next line. Alternatively, a long comment may as wellas be between the C type comment symbols,i.e. /* before the start of comment and */ at the end of the comment.

How to include comments and use of void main() in place of int main()
// A program with comments #include<iostream> Void main() // Anything written after double slash is neglected up to the // end of line. The comment can be put anywhere in the program. { std::cout << Hello, Welcome to programming in C++! \n ; /* Notice the blank spaces in the beginning of the first two lines, and also the missing lines. All these blank spaces are neglected by compiler. Comments may also be put in the c-style, i.e. between the symbols /* and */ as done in this comment. */ std::cout<<Are you interested?<<std::endl ; /* When you use void main() do not include the statement return 0 ; , because the void functions do not return any value */ // and compiler will show it as an error if return statement is included. }

The cin
To put in some data we make use of function cin This is used along with the operator >> which is called extraction operator. This is followed by the name of variable in whose memory block the data has to be stored. After typing the data for cin, do press the enterkey, otherwise, the computer will not proceed further.

Using cin and Extraction operator


cin >> number

Keyboard

Application of cin in a user interactive program


#include<iostream> int main() { int length = 0 ; // length is the name of variable of type int Int width = 0, area =0; // width and area are also names of variables std:: cout << Write the length and width of rectangle : ; // output statement std::cout << length = <<length<<\t width = << width<<\t area = <<area<<endl ; // output statement return 0 ; }

Namespaces
It enables the programmer to group a set of objects or function under one name. All classes, objects and functions of C++ Standard Library are defined under namespace std . We can simplify the writing of std :: cout to cout and std :: cin to cin by including following the directive in the program: using namespace std ;

Application of namespaces
#include<iostream> Using namespace std ; // use of std namespace namespace NS1 // no semicolon at the end of line { int n = 3 ; float m = 2.5 ; int k = 2 ; double R = n*m*k ; } namespase NS2 // no semicolon at the end of line { float n = 4.0 ; // Names are same as in NS1 //but values are different . int m =2 ; double k = 3.0 ; double R = n*m*k ; } int main() { int Squatre ; int Product ; using namespace NS2 ; Square = n*n + m*m ; // values under NS2 are used Product = NS1 :: k*NS2 :: m ; // k belongs to NS1 and //m to NS2 cout << Square = <<Square<<, \t Product = <<Product<<endl; cout <<R = << NS1 :: R <<endl ; // This R belongs to NS1 cout << R = << NS2 :: R << endl ; // This R belongs to NS2 return 0 ; }

User Interactive Programs


#include<iostream> using namespace std ; int main() { int D ; float PI ; // Here D stands for //diameter and PI stands //for . double A , C ; // A stands for area and C //stands for circumference cout << Write values of D and PI: ; cin >> D >> PI ; // statement for input D and PI cout << You have written the values as D = <<D<< and PI = <<PI<<endl ; //endl may be used in place of \n A = PI * D * D/4 ; // computation of area C = PI * D ; // Computation of circumference cout<< D<< , A = << A<< and C= << C<< endl ; return 0 ; }

Formatting The Output


In next Class ..

Conclusion
We tried to grasp the fundamentals of C++ programming.

Thanks for listening.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy