Oop
Oop
Oop
Book to follow
Storage Classes
Arrays , Strings
3
Programming paradigm
OOP Introduction
Procedural programming
I am going to walk
(method)
Features of OOP
Comparison
Procedural Oriented
Object Oriented
Characteristics of Object-Oriented
Languages
Object
Class
Dynamic Binding
Message Passing
Inheritance
Polymorphism
Header files
The line using namespace std; tells the compiler to use the std namespace.
Namespaces are a relatively recent addition to C++.
The line int main() is the main function where program execution begins.
The next line return 0; terminates main( )function and causes it to return the
value 0 to the calling process.
1
Basics
C++ Keywords
Table 1 Keywords
asm
else
new
this
auto
enum
operator
throw
bool
explicit
private
TRUE
break
export
protected
try
case
extern
public
typedef
catch
FALSE
register
typeid
char
float
reinterpret_cast
typename
class
for
return
union
const
friend
short
unsigned
const_cast
goto
signed
using
continue
if
sizeof
virtual
default
inline
static
void
delete
int
static_cast
volatile
do
long
struct
wchar_t
double
mutable
switch
while
dynamic_cast
namespace
template 1
Keyword
Boolean
bool
Character
char
Integer
int
Floating point
float
double
Valueless
void
Typical Range
char
1byte
unsigned char
1byte
0 to 255
signed char
1byte
-128 to 127
int
4bytes
-2147483648 to 2147483647
unsigned int
4bytes
0 to 4294967295
signed int
4bytes
-2147483648 to 2147483647
short int
2bytes
-32768 to 32767
unsigned short
int
2bytes
0 to 65,535
2bytes
-32768 to 32767
long int
4bytes
-2,147,483,648 to
2,147,483,647
4bytes
-2,147,483,648 to
2,147,483,647
unsigned long
int
4bytes
0 to 4,294,967,295
float
4bytes
double
8bytes
Example 1
#include <iostream>
using namespace std;
int main()
{
cout <<
cout <<
cout <<
cout <<
cout <<
cout <<
return 0;
}
"Size
"Size
"Size
"Size
"Size
"Size
of
of
of
of
of
of
C++ Variables
datatype variable_list;
30
23.3333
// actual initialization
a = 10;
b = 20;
c = a + b;
f = 70.0/3.0;
cout<<c<<endl<<f;
return 0;
1
Scope of variables
Global Variables
Local variables
include <iostream>
using namespace std;
int x;
// Global variable
declared
int main()
{
int a;
// Local variable
declared
x=10;
// Initialized once
cout <<"first value of x = "<< x;
x=20;
// Initialized again
a=10;
// Initialized once
cout <<"Initialized again with value =
"<< x;
}
Constants
There are two simple ways in C++ to define constants:
Using #define preprocessor.
#define identifier value
Using const keyword.
const type variable = value;
#include <iostream>
using namespace std;
50
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area;
cout << NEWLINE;
return 0;
2
50
int main()
{
const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area;
Type conversions
#include <iostream>
using namespace std;
main()
{
double a = 21.09399;
float b = 10.20;
int c ;
c = (int) a;
cout << "Line 1 - Value of (int)a is :" << c << endl ;
c = (int) b;
cout << "Line 2 - Value of (int)b is :" << c << endl ;
return 0;
double d = 0.1234;
float f = d;
std::cout << f;
}
2
0.1234
return 0;
Operators
Types of Operators
Assignment Operator
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Unary Operators
Ternary Operator
Comma Operator
Operators
Assignment Operator
Operates '=' is used for assignment, it takes the right-hand side (called
rvalue) and copy it into the left-hand side (called lvalue). Assignment
operator is the only operator which can be overloaded but cannot be
inherited.
int x=10;
Arithmetic Operators
Operators
Relational Operators
Operators
Logical Operators
Operator Description
Example
&&
(A && B) is false.
||
Operators
Bitwise operators
p&q
p|q
p^q
Operators
A = 0011 1100
B = 0000 1101
Bitwise operators
Operator Description
Example
<<
>>
Operators
Unary Operators
These are the operators which work on only one operand. There are
many unary operators, but increment + + and decrement -- operators are
most used.
Ternary Operator
The ternary if-else ? : is an operator which has three operands.
int a = 10;
a > 5 ? cout << "true" : cout << false";
Comma Operator
Library Functions
Meaning
sin(x)
cos(x)
tan(x)
exp(x)
log(x)
logarithm of x
sqrt(x)
Square root of x
pow(x,
y)
abs(x)
Library Functions
Table 2 Character Functions (cctype)
Function
Meaning
isalpha(c)
isdigit(c)
isalnum(c)
islower(c)
isupper(c)
toupper(c)
tolower(c)
Decision Making
Decision making is about deciding the order of execution of
statements based on certain conditions or repeat a group of
statements until certain specified conditions are met. C++
handles
decision-making
by
supporting
the
following
statements,
if statement
switch statement
conditional operator statement
goto statement
Looping
In
any
programming
language, loops are used to
execute a set of statements
repeatedly until a particular
condition is satisfied.
There are 3 type of loops in
C++ language
while loop
for loop
do-while loop
while loop
for loop
do while loop
continue : It causes the control to go directly to the testcondition and then continue the loop process.
C++ structures
#include <iostream>
using namespace std;
Structure Example 1
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4
struct person {
char name[50];
int age;
float salary;
};
int main() {
person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout
cout
cout
cout
}
4}
return 0;
C++ Functions
Calling a function
Call by Value
Call by Reference
Call by value
#include < iostream>
using namespace std;
int main()
{
int x = 10;
calc(x);
cout<<x;
}
void calc(int x)
{
x = x + 10 ;
}
int calc(int x)
{
x = x + 10 ;
return x;
}
10
20
4
Call by reference
#include < iostream>
using namespace std;
void calc(int *p);
int main()
{
int x = 10;
calc(&x);
// passing address of x as argument
printf("%d", x);
}
void calc(int *p)
{
*p = *p + 10;
}
20
5
Recursion
In recursion, a function calls itself until condition is
satisfied.
#include
<iostream>
Enter a number to find factorial: 4
Factorial of 4 = 24
int factorial(int);
int main() {
int n;
cout<<"Enter a number to find factorial: ";
cin>>n;
cout<<"Factorial of "<<n<<" = "<<factorial(n);
return 0;
}
int factorial(int n) {
if (n>1) {
return n*factorial(n-1);
}
else {
return 1;
}
}
5
Inline functions
#include <iostream>
Default arguments
No argument passed:
*
First argument passed:
#
Both argument passed:
$$$$$
Storage Classes
Global variables
Local variables
Register variables
Static variables
Extern variables
Global variables
These are defined at the starting , before all function bodies and
are available throughout the program.
Storage Classes
Local variables
They are defined and are available within a particular
scope. They are also called Automatic variable.
Register variables
This is also a type of local variable. This keyword is
used to tell the compiler to make access to this variable
as fast as possible. Variables are stored in registers to
increase the access speed.
Storage classes
Static variable
Static variables are the variables which are initialized &
allocated storage only once
void fun()
{
static int i = 10;
i++;
cout << i;
}
int main()
{
fun();
// Output = 11
fun();
// Output = 12
fun();
// Output = 13
}
Storage classes
Extern variables
This keyword is used to access variable in a file which is
declared & defined in some other file, that is the
existence of a global variable in one file is declared
using extern keyword in another file.
Arrays
Defining Arrays :
type array_name[size];
int age[5];
Arrays Example 1
Enter 5 numbers: 4
-3
5
2
0
First number: 4
Last number: 0
nclude <iostream>
sing namespace std;
nt main()
int n[5];
cout<<"Enter 5 numbers: ;
Storing 5 number entered by user in an array using for loop. */
for (int i = 0; i < 5; ++i) {
cin>>n[i];
}
int x[3][4];
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
int main()
{
int test[3][2] = {
{2, -5},
{4, 0},
{9, 1}
};
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cout<< "test["<< i << "][" << ;j << "] = " << test[i]
[j]<<endl;
}
}
return 0;
}
6
Strings
Strings that are objects of string class (The Standard C++ Library string
class
C-strings (C-style Strings)
char str[] = "C++";
str is a string and it holds 4 character. Although, "C++" has 3
character, the null character \0 is added to the end of the string
automatically.
char str[4] = "C++";
C- Strings
S.No
C-Strings Example 1
#include <iostream>
using namespace std;
int main() {
char str[100];
cout<<"Enter a string: ";
cin>>str;
cout<<"You entered:
"<<str<<endl;
cout<<"\nEnter another string: ";
cin>>str;
cout<<"You entered:
"<<str<<endl;
return 0;
}
C-Strings Example 2
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] =
char str2[10] =
char str3[10];
int len ;
"Hello";
"World";
return 0;
#include <iostream>
#include <string>
str3 : Hello
str1 + str2 : HelloWorld
str3.size() : 10
int main ()
{
string str1 =
string str2 =
string str3;
int len ;
"Hello";
"World";
return 0;
Discussions