100% found this document useful (1 vote)
152 views48 pages

Introduction to C++ Programming

The document provides an overview of the C++ programming language. It discusses that C++ is an object-oriented programming language that is viewed as best for creating large-scale applications. It also notes that C++ is a combination of Simula67 and C, allowing for classes and objects while maintaining backwards compatibility with C. The document then covers various C++ concepts like variables, data types, operators, input/output, and control structures at a high level.
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
100% found this document useful (1 vote)
152 views48 pages

Introduction to C++ Programming

The document provides an overview of the C++ programming language. It discusses that C++ is an object-oriented programming language that is viewed as best for creating large-scale applications. It also notes that C++ is a combination of Simula67 and C, allowing for classes and objects while maintaining backwards compatibility with C. The document then covers various C++ concepts like variables, data types, operators, input/output, and control structures at a high level.
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/ 48

C++ Programming

Elementary Procedural

Advanced Procedural

Special Procedural

Object Oriented Eiffel

Advanced Object Oriented

Fortran

Algol 68

C++

C#

Cobol

PL/I

Pascal

Ada 95

Java

Ada 83

What is Programming?
Take
A problem A set of data A set of functions

Apply functions to data to solve the

problem

What is C++?
C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating largescale applications. C++ is a multi paradigm language. C++ is an object driven language.

Introduction to C++
C++ (pronounced see plus plus) was developed by Bjarne Stroustrup at Bell Labs in 1979. Combination of Simula67 and c C With classes In 1983,the name was changed to C++. C++ is an extended version of C. C++ is a superset of C. All the concepts of C are applicable to C++ also.

Difference b/w C and C++

C C Procedure Oriented C does not have any classes or objects. C input/output is based on library and the processes are carried out by including functions. C++ Object Oriented It supports Classes and Objects C++ i/o is made through console commands cin and cout.

Difference b/w C and C++


C functions do not support overloading. C does not support new or delete commands. C has a top down approach . C has predefined data types. C++ supports Overloading. New keyword is used to create an object for a class. C++ has a bottom up approach. In c++, User can creates its own datatype using class.

Variables

Variables are like small blackboards


We can write a number on them We can change the number We can erase the number

C++ variables are names for memory locations


We can write a value in them We can change the value stored there We cannot erase the memory location
Some value is always there

Identifiers
Variables names are called identifiers Choosing variable names
Use meaningful names that represent data to be stored First character must be
a letter the underscore character

Remaining characters must be


letters numbers underscore character

Keywords
Keywords (also called reserved words)
Are used by the C++ language Must be used as they are defined in the programming language Cannot be used as identifiers Keyword is an essential part of a language definition.

Keywords

Types of C Constants
C constants can be divided into two major categories
Primary Constants Secondary Constants
C Constants Secondary constants

Primary Constants

Integer Constant Real Constant Character Constant

Array, Pointer Structure, Union enum

Integer Constants
An integer constant must have at least one digit It must not have a decimal point It could be either positive or negative If no sign precedes an integer constant, it is assumed to be positive ex., 123, -321, +78 No commas or blanks are allowed within an integer constant

Real Constants
Real constants (RC) must have at least one digit
It must have a decimal point

It could be either positive or negative


Default sign is positive No commas or blank are allowed within a RC
Ex., 0.0083, -0.75, +247.0

Single Character Constants


A Single character constant is either a single alphabet, a single digit or a single special symbol enclosed within

single quotes
The maximum length of a character constant can be 1 character
Eg a, 1, 5, = (Valid) asd, 12 (Invalid)

String Constants
It is a sequence of characters

enclosed in double quotes


Characters may be letters, numbers, special characters enclosed in double quotes.
Eg., Hello! 1987 ?...! X

Data Types
The most commonly used Data Types in C++ programming language:
short int int long int float double long double char bool

Declaring Variables
Before use, variables must be declared Declaration syntax:
<type_name> <Variable_1>, <Variable_2>, . . . ;

Tells the compiler the type of data to store

Declaring Variables
Examples: int number_of_students; double weight;

int is an abbreviation for integer. could store 3, 102, 3211, -456, etc. number_of_students is of type integer double represents numbers with a fractional component could store 1.34, 4.0, -345.6, etc. Weight is of type double

Initializing / Assigning Variables


Declaring a variable does not give it a value
Giving a variable a value in the declaration is initializing the variable Eg: int age = 5;

Giving a value to the declared variable is assigning the variable


Eg: int age;
age = 5;

Structure of C++ Program

Include files Global variable and function declaration

Main function Function subprogram

Simple C++ Program


/* A first C++ Program*/ #include<iostream.h> int main() {
Insertion operator
Statement terminated with ;

Comment

cout<<Learning C++ Programming is easy return 0;

\n";

}
One way to exit the function or to return something

String to print

Execution of C++ Program


Phase 1

Editor

Disk

Program edited in Editor and stored on disk

Phase 2

Preprocessor

Disk

Preprocessor program processes the code

Creates object code and stores on disk Phase 3

Compiler

Disk Links object code with libraries and stores on disk

Phase 4

Linker

Disk

Execution of C++ Program


Primary memory
Puts program in memory Phase 5

Loader

Primary memory Takes each instruction and executes it storing new data values

Phase 6

CPU

Input and Output


A data stream is a sequence of data
Typically in the form of characters or numbers

An input stream is data for the program to use


Typically originates
at the keyboard at a file

An output stream is the programs output


Destination is typically
the monitor a file

Output using cout


cout is an output stream sending data to the monitor The insertion operator "<<" inserts data into cout Example:
cout << number_of_students << " Students\n";

This line sends two items to the monitor The value of number_of_students The quoted string of characters " Students\n The \n causes a new line to be started following the s in Students A new insertion operator is used for each item of output

Input Using cin


cin is an input stream bringing data from the keyboard
The extraction operator (>>) gets data to be used Example:
cout << "Enter the number of students in a class\n; cin >> number_of_students;

This code prompts the user to enter data then read one data items using cin The value read is stored in number_of_students

Reading Data From cin


Multiple data items are separated by spaces Data is not read until the enter key is pressed Example:
cin >> v1 >> v2 >> v3; Requires three space separated values User might type 34 45 12 <enter key>

Designing Input and Output


Prompt the user for input that is desired cout statements provide instructions
cout << "Enter your age: "; cin >> age;

Displaying the output what was read


cout << age << " was entered." << endl;

Comment
Comment should be enclosed between /* Single Line Comment is // It is used to increase the readability of the program. Any number of comments can be given at any place in the program. Comment cannot be nested. It can be split over more than one line */

Escape Sequence
\n \t \r \a \\ \ \? \ \0 new line tab carriage return alert backslash double quote Question Mark Quotation Mark Null

Operators in C++
An operator is a symbol (Eg:+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables.
Eg: a + b

Operators in C++
1. 2. 3. 4. 5. 6. 7. 8. Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators

Arithmetic Operator

C++ operation Addition(+) Subtraction (-) Multiplication(*) Division(/) Modulus(%)

Algebraic f+7 p-c bm x/y, x , x r mod s y

C++ f+7 p-c b*m x/y r%s

Precedence order
Highest to lowest
()

*, /, %
+, -

Example
Algebra:

a(b+c)+ c(d+e)
C:

a * ( b + c ) + c * ( d + e ) ;
Precedence: 3 1 5 4 2

Example
/* Program using Arithmetic operator */ #include<iostream.h> #include<constream.h> void main() { int a=5,b=6; // Initializing variables int c; clrscr(); // To clear the screen c=a+b; // + operator cout<<The result is:<<c; c=b%a; // % operator cout<<The result is:<<c; }

Relational Operators
Operator < <= > >= Meaning Is less than Is less than or equal to Is greater than Is greater than or equal to

==
!=

Equal to
Not equal to

Logical Operators
Operator && || Meaning Logical AND Logical OR

Logical NOT

Logical expressions are a compound relational expressions -An expression that combines two or more relational expressions Eg: if (a==b && b==c)

Assignment operators
Syntax: v op = exp; Where v = variable, op = shorthand assignment operator exp = expression Ex:

x=x+3 x+=3

Shorthand Assignment operators


Simple assignment operator a = a+1 a = a-1 a = a* (m+n) a = a / (m+n) a = a %b Shorthand operator a + =1 a - =1 a * = m+n a / = m+n a %=b

Increment & Decrement Operators


1. 2. Increment ++ Decrement -The ++ operator adds a value 1 to the operand The operator subtracts 1 from the operand ++a / --a => pre increment / decrement

a++ / a-- => post increment / decrement

Examples for pre & post operators


Let the value of a =5 and b=++a then a = b =6 Let the value of a = 5 and b=a++ then

a =5 but b=6
i.e.: 1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 2. a postfix operator first assigns the value to the variable on left and then increments the operand.

Conditional operators
Syntax:

exp1 ? exp2 : exp3 Where exp1,exp2 and exp3 are expressions


Working of the ? : Operator: Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression, If exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression Eg: m=2; n=3; r=(m>n) ? m : n; // Here r = n

Example
/* Program using Conditional operator */ #include<iostream.h> #include<constream.h> void main() { int a,b,c; // Declaring variables a=5; // Assigning values b=6; clrscr(); // To clear the screen c=(a>b)?a:b; // Conditional operator cout<<The result is:<<c; }

Bitwise operators
Operator & | ^ << >> Meaning Bitwise AND Bitwise OR Bitwise exclusive OR Shift left Shift right

Example
/* Program using bitwise operators */ #include<iostream.h> #include<constream.h>

void main() { clrscr(); int a=5,b=6; // a = 0101 , b = 0110 int c; c=a&b; cout<<"\n a & b : "<<c; // c = 0100 = 4

Continues

c=a|b; cout<<"\n c=a^b; cout<<"\n int d; d=c<<1; cout<<"\n d=c>>1; cout<<"\n }

a | b : "<<c; // c = 0111 = 7

a ^ b : "<<c; // c = 0011 = 3

c << 1 :"<<d; // d = 0110 = 6 c >> 1 :"<<d; // d = 0001 = 1

You might also like