Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views9 pages

Topic 1 - Programming in C++

The document provides an overview of C++ fundamentals, including its history, comment syntax, input/output operators, program structure, tokens, keywords, identifiers, data types, constants, and variable declarations. It explains the significance of various components such as the main function, enumeration constants, and symbolic constants. Additionally, it highlights the use of reference variables and their initialization in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Topic 1 - Programming in C++

The document provides an overview of C++ fundamentals, including its history, comment syntax, input/output operators, program structure, tokens, keywords, identifiers, data types, constants, and variable declarations. It explains the significance of various components such as the main function, enumeration constants, and symbolic constants. Additionally, it highlights the use of reference variables and their initialization in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1

TOPIC 1- C++ Fundamentals

C ++ is an object oriented programming language, C ++ was developed by Bjarne Stroustrup at AT &


T Bell lab, USA in early eighties. C ++ was developed from c and simula 67 language. C ++ was early
called ‘C with classes’.

C++ Comments:

C++ introduces a new comment symbol // (double slash). Comments start with a double slash symbol
and terminate at the end of line. A comment may start anywhere in the line and whatever follows till
the end of line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment.

// this is an example of
// c++ program
// thank you
Multi line comments can be written as follows:

The c comment symbols /* ….*/ are still valid and more suitable for multi-line comments.

/* this is an example of c++ program */

Output Operator:

The statement cout <<”Hello, world” displayed the string with in quotes on the screen. It is a
predefined object that corresponds to the standard output stream. The cout object, whose properties
are defined in iostream.h represents that stream. The insertion operator << also called the ‘put to’
operator.

Input Operator:

The statement
cin>> number1;
is an input statement and causes the program to wait for the user to type in a number. The number
keyed in is placed in the variable number1. The identifier cin is a predefined object in C++ that
corresponds to the standard input stream.
The operator >> is known as get from operator. It extracts value from the keyboard and assigns it to
the variable on its right.

Cascading Of I/O Operator:

cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
cin>>number1>>number2;

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


2

Structure Of A Program :
// my first program in C++

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

#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code
lines with expressions but indications for the compiler's preprocessor. In this case the directive
#include<iostream> tells the preprocessor to include the iostream standard file.
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace, the
namespace with the name std. So in order to access its functionality we declare with this expression
that we will be using these entities.
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the
point by where all C++ programs start their execution, independently of its location within the source
code. It does not matter whether there are other functions with other names defined before or after it –
the instructions contained within this function's definition will always be the first ones to be executed.

STRUCTURE OF C++ PROGRAM

• Include files
• Class declaration
• Class functions, definition
• Main function program
TOKENS:
Def: The smallest individual units in program are known as tokens. C++ has the following
Tokens.
i. Keywords
ii. Identifiers
iii. Constants
iv. Strings
v. Operators
KEYWORDS:

The keywords implement specific C++ language feature. They are explicitly reserved identifiers and
can’t be used as names for the program variables or other user defined program elements. The
keywords not found in ANSI C are shown in red letter.

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


3

C++ KEYWORDS:
asm double new switch
auto else operator template
break enum private this
case extern protected throw
catch float public try
char for register typedef
class friend return union
const goto short unsigned
continue if signed virtual
default inline sizeof void
delete long struct while

IDENTIFIERS:
Identifiers refers to the name of variable, functions, array, class etc. created by programmer. Each
language has its own rule for naming the identifiers.
The following rules are common for both C and C++. In ANSI C the maximum length of a variable is
32 chars but in C++ there is no bar.
1. Only alphabetic chars, digits and underscore are permitted.
2. The name can’t start with a digit.
3. Upper case and lower-case letters are distinct.
4. A declared keyword can’t be used as a variable name.

BASIC DATA TYPES IN C++

C ++ Data Types

User defined type Built in types Derived type


Structure Array
Union Function
Class pointer
enumeration

Integral type void Floating point

int char float double

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


4

Both C and C++ compilers support all the built in types. With the exception of void the basic datatypes
may have several modifiers/Qualifier preceding them to serve the needs of various situations. The
modifiers/Qualifier signed, unsigned, long and short may applied to character and integer basic
data types. However the modifier long may also be applied to double.

Data types in C++ can be classified under various categories.

DATA TYPE BYTES RANGE


Char 1 -128 to – 127
unsigned char 1 0 to 265
signed char 1 -128 to 127
Int 2 -32768 to 32768
unsigned int 2 0 to 65535
singed int 2 -32768 to 32768
short int 2 -32768 to 32768
long int 4 -2147483648 to 2147483648
signed long int 4 -2147483648 to 2147483648
unsigned long int 4 0 to 4294967295
Float 4 3.4E-38 to 3.4E+38
Double 8 1.7E -308 to 1.7E +308
long double 10 3.4E-4932 to 1.1E+ 4932

The type void normally used for:

1) To specify the return type of function when it is not returning any value.
2) To indicate an empty argument list to a function.

CONSTANTS
Def: Constants are expressions with a fixed value.

Types of constants
• Integer constant
• Floating point/ Real constant
• Character constant
• String constant
• Enumeration constant
• Symbolic Constant

• Integer Constant
Eg:
1776
707
-273
These are numerical constants that identify integer values. Notice that they are not enclosed in quotes or
any other special character; they are a simple succession of digits representing a whole number in decimal
base; for example, 1776 always represents the value one thousand seven hundred seventy-six.
TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS
5

In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal numbers
(base 8) and hexadecimal numbers (base 16) as literal constants. For octal literals, the digits are preceded
with a 0 (zero) character. And for hexadecimal, they are preceded by the characters 0x (zero, x). For
example, the following literal constants are all equivalent to each other:

75 // decimal
0113 // octal
0x4b // hexadecimal

All of these represent the same number: 75 (seventy-five) expressed as a base-10 numeral, octal numeral
and hexadecimal numeral, respectively.

These literal constants have a type, just like variables. By default, integer literals are of type int. However,
certain suffixes may be appended to an integer literal to specify a different integer type:
Suffix Type modifier
u or U unsigned
l or L long
ll or LL long long

Unsigned may be combined with any of the other two in any order to form unsigned long or unsigned long
long.
For example:
75 // int
75u // unsigned int
75l // long
75ul // unsigned long
75lu // unsigned long

In all the cases above, the suffix can be specified using either upper or lowercase letters.

• Floating Point constant


They express real values, with decimals and/or exponents. They can include either a decimal point, an e
character (that expresses "by ten at the Xth height", where X is an integer value that follows the e
character), or both a decimal point and an e character:

3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
3.0 // 3.0
These are four valid numbers with decimals expressed in C++. The first number is PI, the second one is the
number of Avogadro, the third is the electric charge of an electron (an extremely small number) -all of
them approximated-, and the last one is the number three expressed as a floating-point numeric literal.

The default type for floating-point literals is double. Floating-point literals of type float or long double can
be specified by adding one of the following suffixes:
Suffix Type
f or F float
l or L long double

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


6

For example:
3.14159L // long double
6.02e23f // float
Any of the letters that can be part of a floating-point numerical constant (e, f, l) can be written using either
lower or uppercase letters with no difference in meaning.

• Character Constant
Eg: 'z'
'p'
Notice that to represent a single character, we enclose it between single quotes ('),

• String Constant
Eg: "Hello world"
"How do you do?"
The above two represent string literals composed of several characters. and to express a string (which
generally consists of more than one character), we enclose the characters between double quotes (").

Imp. Note: Both single-character and string literals require quotation marks surrounding them to
distinguish them from possible variable identifiers or reserved keywords. Notice the difference between
these two expressions:
x
'x'
Here, x alone would refer to an identifier, such as the name of a variable or a compound type, whereas 'x'
(enclosed within single quotation marks) would refer to the character literal 'x' (the character that
represents a lowercase x letter).

Character and string literals can also represent special characters that are difficult or impossible to express
otherwise in the source code of a program, like newline (\n) or tab (\t). These special characters are all of
them preceded by a backslash character (\) and are called as Escape Sequences.

Escape Sequences:
Escape Sequence Description
\n newline (When a new line is necessary in the output)
\r carriage return (moves the cursor at the beginning of current line)
\t tab (equal to eight spaces. ...)
\v vertical tab
\b backspace (Whenever we want to delete a single character)
\f form feed (used in printing, to insert a blank paper in the printed output)
\a alert (to play beep during execution)
\0 string termination character or null character

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


7

Enumeration Constant:
Enum is a user defined data type where we specify a set of values for a variable and the variable can only
take one out of a small set of possible values. We use enum keyword to define a Enumeration.
Eg:

METHOD 1 METHOD 2

#include<iostream.h> #include<iostream.h>
enum direction {East, West, North, South}dir; using namespace std;
int main() enum direction {East, West, North, South};
{ int main()
dir = West; {
cout<<dir; direction dir;
return 0; dir = West;
} cout<<dir;
return 0;}

In the above example Enumeration name is direction which can only take one of the four specified values,
the dir at the end of the declaration is an enum variable. Value West is assigned to the enum variable dir
and when it is displayed, the value of dir it shows is 1. This is because by default the values are in
increasing order starting from 0, which means East is 0, West is 1, North is 2 and South is 3.

Why use enum in C++

Now that we understand what is enum and how to use them in program, lets discuss why we use them:
Enums are used only when we expect the variable to have one of the possible set of values, for example,
we have a dir variable that holds the direction. Since we have four directions, this variable can take any one
of the four values, if we try to assign a another random value to this variable, it will throw a compilation
error. This increases compile-time checking and avoid errors that occurs by passing in invalid constants.

How to change default values of Enum


Eg:
#include <iostream.h>
enum direction {East=11, West=22, North=33, South=44};
int main(){
direction dir;
dir = South;
cout<<dir;
return 0;
}
Symbolic Constant
A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes
the type, name, and initialization.
Eg:
Here's a statement that sets the point reward for killing a zombie:
const int KILL_BONUS = 5000;
Whenever a zombie is killed, the player's score is increased by the reward:
playerScore = playerScore + KILL_BONUS;
If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS,
and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it
would be more difficult to find all the places it is used and change the value. This reduces errors.

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


8

DECLARATION OF VARIABLES:

In ANSIC C all the variable which is to be used in programs must be declared at the beginning of the
program .But in C++ we can declare the variables any whose in the program where it requires .This makes the
program much easier to write and reduces the errors that may be caused by having to scan back and forth. It also
makes the program easier to understand because the variables are declared in the context of their use.
Eg:
int main()
{
float x;
float sum=0;
for(int i=1;i<5;i++)
{ cin>>x;
sum=sum+x;
}
float average;
average=sum/x;
cout<<average;
return 0;}

REFERENCE VARIABLES:
C++interfaces a new kind of variable known as the reference variable. A references variable
provides an alias. (alternative name) for a previously defined variable. For example , if we make the
variable sum a reference to the variable total, then sum and total can be used interchangeably to represent
the variable.
A reference variable is created as follows:
syntax: Datatype & reference –name=variable name;

A reference variable must be initialized at the time of declaration.

Example:

1) #include <iostream>
using namespace std;
int main() {
int a = 8;
int &b = a;
cout << "The variable a : " << a;
cout << "\nThe reference variable r : " << b;
return 0;
}

Output:
The variable a : 8
The reference variable r : 8

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS


9

2) #include <iostream.h>
int main () {
int i;
double d;

// declare reference variables


int & r = i;
double & s = d;

i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;

d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}

Output:

Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7

TOPIC 2- BASICS OF C++ TR. ADITI P. NAIK, DON BOSCO HSS

You might also like