Professional Documents
Culture Documents
OBJECTIVE
SCOPE
C++ introduction
Understanding Menu
Data
Symbolic Constants
Reference Variable
Programming constructs
Input Operation
Chapter-1/1
C++
C++ INTRODUCTION
WHAT IS C++
(a) C’s power & flexibility in dealing with the hardware / software interface.
C++ versus C
Similarities
Case sensitive
Local & global variables can have same names. The program uses the value
of local variable.
Chapter-1/2
C++
Difference
C++ C
File name have .cpp extension File name have .c extension
Identifier can have any number of C compiler identifies only first 32
characters. characters of an identifier name.
Strongly typed language. It checks for Function prototypes may or may not be
the number & type of formal arguments included.
as well as the return data type. This
check is performed between the function
call & function definition.
Default values for the parameters in the This facility is not available in C.
function’s prototype is possible.
The function main() returns an integer The function main() does not return
type value any value
The user-defined data type class is also Only struct & union , data types are
available alongwith struct & union. available
The declarations of the variables can be The declaration of the variables must be
placed close to the statements that use at the beginning of the function.
the variables
Global variable can be accessed with the Global variable cannot be accessed if
scope resolution operator (::), in case the the program has the local variable with
program has the local variable with the the same name.
same name.
C++ compiler interprets the & operator C compiler interprets the & operator as
according to the context of the address.
statement, either as address or as
reference.
Chapter-1/3
C++
PROGRAM FEATURES
Every C++ program starts with main(), which returns an integer type value.
main() declaration :
C++ has its own set of I/O library syntax, which are defined in a header file
called ‘iostream.h’. This header file needs to be included before main().
EXAMPLE
EXAMPLE
PROGRAM STATEMENT:
cout << “C++ IS A SUPERSET OF C” << ‘\n’ << “IT SUPPORTS OBJECT-ORIENTED PROGRAMMING”;
Chapter-1/4
C++
COMMENT ENTRIES
Chapter-1/5
C++
UNDERSTANDING MENU
WRITING A PROGRAM
Various type of files can be opened. To start with choose Text file.
To save the file - Save option of File sub-menu. The extension of .cpp is
given by the Developer Studio ( an interface between the user & Visual C++ ).
CLASSROOM EXERCISE
Chapter-1/6
C++
DATA
TOKENS
Types Of Tokens
IDENTIFIERS
KEYWORDS
DATA TYPES
IDENTIFIERS
EXAMPLE
Valid Identifiers
liGrandTotal indicates local integer variable for storing grand total
gfFreuency indicates global float variable for storing frequency
sNameOfEmp indicates local string variable [ character array ] for storing name of
employees
piRangeOfNum indicates integer pointer (local pointer)
CalculateSum indicates a function to calculate sum
Chapter-1/7
C++
CLASSROOM EXERCISE
KEYWORDS
They are the predefined identifiers that have special meanings to the
C/C++ compiler.
C++ Keywords
auto do if short
break double inline sizeof
case else int static
char enum long struct
class extern new switch
const float operator typedef
continue for private union
default friend protected virtual
delete goto public void
return while
DATA TYPES
Chapter-1/8
C++
EXAMPLE
Chapter-1/9
C++
EXAMPLE
float CalculateAverage(void);
void *pvGenericPointer;
C++ uses void data type. This data type is available in ANSI C.
Chapter-1/10
C++
CLASSROOM EXERCISE
struct & union are user-defined data types . These data types are
same in C as well as in C++.
EXAMPLE
Note - Table will have value 0 , Chair will have 1, and so on.
enum eMarks {Zero, One, Two, JustPass = 49, Pass, Full = 100};
Note - Zero will have value 0, One will have 1, JustPass will have 49
instead of 3, Pass will have 50 & Full will have 100.
eMarks eNumberObtained;
Chapter-1/11
C++
EXAMPLE
The data types formed from the basic data types are called derived
data types.
Arrays, Functions & Pointers are derived data types available in both
C & C++.
EXAMPLE
int iMarks;
int * const piMarksObtained = & iMarks ; // constant pointer
Chapter-1/12
C++
CLASSROOM EXERCISE
Identify which of the following declarations / assignments are illegal & why?
(a) int * const piIntPointer = & iNumber;
(b) int * const piIntPointer = & iMark;
(c) char * const pcCharPointer;
(d) const char * pcCharPointer = & cAlphabet;
(e) const char * pcCharPointer = & cStatus;
Chapter-1/13
C++
SYMBOLIC CONSTANT
(a) #define
EXAMPLE
#define iMax 20
or
const int iMax = 20;
or
enum { iMax = 20 };
/* This indicates that in the program instead of using 20 the word iMax can be used.
iMax is a constant variable */
Chapter-1/14
C++
REFERENCE VARIABLE
Reference variable and the original variable share the same memory location.
EXAMPLE 1
EXAMPLE 2
PROBLEM- Calculate the average of 5 numbers using a function call. The average needs to
be calculated with sum as 50.5 (given in the main) & with sum as 10
(modified in the function).
/* Function to calculate average where fSump & fAveragep are the parameters. fSump
have the value of fSum & fAveragep have the value of fAverage */
Chapter-1/15
C++
/* Main Function */
void main ( ) {
CLASSROOM EXERCISE
Chapter-1/16
C++
EXAMPLE
CLASSROOM EXERCISE
void main( ) {
int liSum = 12;
int liNumber = 5;
float lfAverage;
/* Printing Average */
cout << "Average =" << lfAverage << '\n';
/* Printing Average */
cout << "Average =" << l fAverage << '\n';
/* Printing Average */
cout << "Average =" << lfAverage << '\n';
Chapter-1/17
C++
Used to access the global variable if the local variable has the same name as
that of the global variable.
EXAMPLE 1
PROBLEM- Display the value of local & global variables having same name.
void main ( ) {
int iNumber = 7; // Local variable
/* Displaying variables */
cout << "Number = " << iNumber << '\n'; // Displaying local variable
cout << "Number = " << :: iNumber << '\n'; // Displaying global variable
}
OUTPUT - Number = 7
Number = 10
EXAMPLE 2
PROBLEM- Display the value of local & global variables having same name. Multiple
local variables are declared in different blocks.
Chapter-1/18
C++
void main ( ) {
/* Inner block */
{
int iNumber = 7; // Local variable of inner block
HOME ASSIGNMENT
#include <iostream.h>
void DisplayNumber ( ) {
int iNumber = 5;
cout << "Number = " << iNumber << '\n';
cout << "Number = " << :: iNumber << '\n';
}
void main ( ) {
int iNumber = 7;
DisplayNumber ( );
cout << "Number = " << iNumber << '\n';
cout << "Number = " << :: iNumber << '\n';
}
Chapter-1/19
C++
PROGRAMMING CONSTRUCTS
(a) if Statement
Syntax -
if (expression) {
true actions ;
}
else {
false actions ;
}
Note: The result of the expression should be either TRUE or FALSE.
else is optional.
Nested ifs are possible.
if-else-if is also possible.
Syntax -
switch (expression) {
case constant1:
statements to be performed;
break;
case constant2:
statements to be performed;
break;
default:
statements to be performed;
}
Note - Number of case can vary depending upon the instances //alternatives
possible.
break statement skips the remaining checks.
Chapter-1/20
C++
EXAMPLE
PROGRAM-
/* iOption is an integer variable used to accept the choice from the user based
on the menu */
if (iOption == 1)
DisplayAverage ( );
else if (iOption == 2)
CalculateAverage ( );
else if (iOption == 3)
InputNumbers ( );
else
cout << “ERROR HAS OCCURRED” ;
HOME ASSIGNMENT
Write the solution for the above discussed problem with switch
statement ?
LOOP CONSTRUCT
Syntax -
for (initialization expression ; test expression ; incrementation expression ) {
action steps ;
}
EXAMPLE
Chapter-1/21
C++
PROGRAM-#include <iostream.h>
void main( ) {
OUTPUT- Sum is 55
Syntax -
while ( test expression ) {
action steps ;
}
EXAMPLE
void main( ) {
Chapter-1/22
C++
/* Initializing liCounter to 1 */
liCounter = 1;
OUTPUT- Sum is 55
Syntax -
do {
action steps ;
} while ( test expression );
EXAMPLE
PROGRAM-#include <iostream.h>
void main( ) {
/* Initializing liCounter to 1 */
liCounter = 1;
Chapter-1/23
C++
OUTPUT- Sum is 55
HOME ASSIGNMENT
0
1 **
2 ****
3 ******
4 ********
Chapter-1/24
C++
INPUT OPERATIONS
EXAMPLE
int iNumber;
cin >> iNumber; // Accepting the value in the variable iNumber
EXAMPLE
/* The input can be given as together with a space between the two values 23.4 45.0
or on separate lines like
23.4
45.0
*/
Chapter-1/25
C++
CREATING A CLASS
Class
Data Members
EXAMPLE
Chapter-1/26
C++
private :
Member Functions
Can have access as public, private or protected depending upon its usage.
EXAMPLE
SOLUTION- Identify the functions required for the class Number declared earlier.
Data members ( liCounter , liSum) of this class are also declared.
Functions can be
(a) To initialize the data members (InitialDataMembers)
(b) To calculate sum (CalculateSum)
(c) To display sum (DisplaySum)
Chapter-1/27
C++
/* Function definition */
OUTPUT - Sum is 55
CREATING AN OBJECT
Public data members & member functions are accessed through an object.
EXAMPLE
Driver program plays a key role in controlling the flow of the software.
Chapter-1/28
C++
object-name.member-function-name(parameters)
EXAMPLE
CLASSROOM EXERCISE
HOME ASSIGNMENT
Chapter-1/29
C++
SUMMARY
Passing parameters with reference gives the same facility as with pointers. The
added advantage is reference variable doesn’t require new memory location.
Chapter-1/30
C++
LAB EXERCISE
1. Write a program to encrypt the character string entered by the user. The string
cannot have more than 10 characters. The encrypted string should have each
character replaced by the one that occurs after 2 places in the alphabet. For
example - APPLE is encrypted as CRRNG.
Chapter-1/31