Unit 3 - C++ Basics
Unit 3 - C++ Basics
Object Oriented
Programming
Unit - 3
C++ Basics
Vinita Shah
Asst. Professor, IT Dept., GCET
A Simple C++ Program
#include <iostream> //include header file
using namespace std;
int main()
{
cout << "Hello World"; // C++ statement
}
I like C++ so much
return 0;
▪I iostream
will score good
is just like marks
we include stdio.hin
in c C++
program.
▪ It contains declarations for the identifier cout and the insertion
operator <<.
▪ iostream should be included at the beginning of all programs
that use input/output statements.
▪I Awill score
namespace goodregion.
is a declarative marks in C++
▪ A namespace is a part of the program in which certain names are
recognized; outside of the namespace they’re unknown.
▪ namespace defines a scope for the identifies that are used in a
program.
▪ using and namespace are the keywords of C++.
}
I like C++ so much
return 0;
}
I like C++ so much
return 0;
▪I In
will scorereturns
C++, main() good marks
an integer in C++
type value.
▪ Therefore, every main() in C++ should end with a return 0;
statement; otherwise error will occur.
▪ The return value from the main() function is used by the
runtime library as the exit code for the process.
Name: GCET
City: V V Nagar
Country: India
I like C++ so much
I will score good marks in C++
Output
Name: GCET City: V V Nagar Country: India
Unit - 3 C++ Basics 8
Program: Basic C++ program(Cont…)
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int main()
{ {
cout << "Name: GCET\n"; cout << "Name: GCET"<<endl;
cout << "City: V V Nagar\n"; cout << "City: V V Nagar"<<endl;
cout << "Country: India"<<endl;
}
I like C++ so much
cout << "Country: India";
return 0;
}
return 0;
I object
will score good marks in C++
that represents standard
input stream in C++.
▪ Here, standard input stream KeyBoard
represents the Keyboard.
▪ The operator >> is used as
bitwise right shift operator also.
cout<<"Addition : ";
cout<<number1+number2; //Display Addition
return 0;
}
CONSTANTS
SINGLE
INTEGER REAL STRING
CHARACTER
CONSTANTS CONSTANTS CONSTANTS
CONSTANTS
i.e. i.e. i.e.
i.e.
123,-321, 6543 0.0083, -0.75 “Hello”, “197”
‘5’, ‘X’, ‘;’
Unit - 3 C++ Basics 18
C++ Operators
C++ Operators
▪ All C language operators are valid in C++.
1. Arithmetic operators (+, - , *, /, %)
2. Relational operators (<, <=, >, >=, ==, !=)
3. Logical operators (&&, ||, !)
I like C++ so much
4. Assignment operators (+=, -=, *=, /=)
5. Increment and decrement operators (++, --)
I will score good marks in C++
6. Conditional operators (?:)
7. Bitwise operators (&, |, ^, <<, >>)
8. Special operators ()
Operator Meaning
< Is less than
<= Is less than or equal to
I like C++ so>
much
Is greater than
I will score>= good marks
Is greater than orin C++
equal to
== Equal to
!= Not equal to
Literal: ex. i = 1;
Variable identifier: ex. start = i;
Expression: ex. sum = first + second;
Unit - 3 C++ Basics 24
Assignment Operators (Shorthand)
Syntax:
leftSide Op= rightSide ;
It is an arithmetic
operator.
IEx:like C++ so much
Simple assignment
Shorthand operator
x=x+3; operator
I willx+=3;score good marks
a = a+1
in C++a += 1
a = a-1 a -= 1
a = a * (m+n) a *= m+n
a = a / (m+n) a /= m+n
a = a % b a %= b
Unit - 3 C++ Basics 25
Increment and Decrement Operators
▪ Increment ++
The ++ operator used to increase the value of the variable by one
▪ Decrement ─ ─
The ─ ─ operator used to decrease the value of the variable by one
Example:
I like x=100;
C++
x++;
so much
I will
After scorethegood
the execution value of x marks
will be 101. in C++
Example:
x=100;
x--;
After the execution the value of x will be 99.
x = 10 ; After execution
x = 10 ; After execution
p = x++; x will be 11
p will be 10
First assign value of x
(A) 749735
(B) 736749
(C) 367497
(D) none of the mentioned
I .*likePointer-to-member
C++ so much operator To access pointer to data members
of class
Inew
willMemory
score good
allocation operatormarks in C++
Allocates memory at run time
delete Memory release operator
endl Line feed operator Deallocates memory at run time
int main() {
char a = 's';
return 0;
}
Output:
The static variable : 50
The local variable : s
The global variable : m
C++ Data Types
Basic Data types
C++ Datatypes
Type Conversion
I like C++ so much
I will score good
Implicit
marks in
Explicit
C++
(Automatically converts (Forcefully converts one
one datatype to another datatype to another
datatype) datatype)
Icout
like<< C++ so much
a << endl; // this will print 2
a = int(b); //explicit type conversion
Icout
will<< score good marks in C++
a << endl; // this will print 2
long
I like C++ so much float double double
long int
I will score intgood marks in C++
unsigned
int
char
I will average
score= good marks in C++
sum/float (i); //C++ notation
char ch = 'Z';
cout << "The code for " << ch << " is "; //print as char
cout << int(ch) << endl; //print as int
return 0;
}
Output:
a = 31, b = 30, c = 30
The code for Z is 90
Enumeration
Enumeration (A user defined Data Type)
▪ An enumeration is set of named integer constants.
▪ Enumerations are defined much like structures.
enum days{Sun,Mon,Tues,Wed,Thur,Fri,Sat};
0 1 2 3 4 5 6
I like C++
Keyword
Tag
so much
I will name
score Integer
good marks in C++
Values for symbolic constants
dollar 102
int main()
{
I like C++ so much
week today;
today = Wednesday;
cout << "Day " << today+1;
I will score good marks in C++
}
return 0;
Output:
Day 4
int main()
return 0;
Output:
Summer = 10
Unit - 3 C++ Basics 52
Control Structures
Control Structures
▪ The if statement:
• Simple if statement
• if…else statement
• else…if ladder
I like C++nested
• if…else so much
I will score good marks in C++
▪ The switch statement :
▪ The do-while statement: An exit controlled loop
▪ The while Statement: An entry controlled loop
▪ The for statement: An entry controlled loop