0% found this document useful (0 votes)
28 views45 pages

OOPS-unit 3 Basic C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views45 pages

OOPS-unit 3 Basic C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Object Oriented Programming

Using C++

UNIT 3

OOPS Using C++ 1


Characteristics of Procedure Oriented Programming

1. Emphasis is on doing things (Algorithms)


2. Large programs are divided into smaller programs
known as functions
3. Most of the functions share global data
4. Data move openly around the system from function
to function
5. Functions transform data from one form to another
6. Employs Top-Down approach in program design

OOPS Using C++ 2


Features of Object Oriented Programming
1. Emphasis is on data rather than procedure
2. Programs are divided into Objects
3. Data structures are designed such that they
characterize the Objects
4. Functions that operate on the data of an Object are
tied together in the data structure
5. Data is hidden and can’t be accessed by external
functions
6. New data and Functions can be easily added
7. Follows Bottom-Up approach in Program design

OOPS Using C++ 3


Basic Concepts of Object Oriented Programming

• Objects are the basic run time entities in an Object Oriented


system.
e.g. Person, Place, Bank Account, Table etc.
• Classes: The entire set of data and code of an Object can be
made a user defined data type with the help of a Class.
Objects are variables of the type Class.
• Data Abstraction and Encapsulation
– The wrapping up of data and functions into a single unit is known
as Encapsulation.
– Abstraction refers to the act of representing essential features
without including the background details or explanations

OOPS Using C++ 4


Basic Concepts of Object Oriented Programming

• Inheritance is the process by which Objects of one class


acquire the properties of Objects of another class
– Inheritance provides Reusability
• Polymorphism means the ability to take more than one
form.
• (sum(); sum(int a); sum(int a, int b);
– Operator Overloading
– Function Overloading
• Dynamic Binding means that the code associated with a
given procedure call is not known till the time of the call
at Run-time
• Message Passing

OOPS Using C++ 5


Benefits of OOP
 Through Inheritance, we can eliminate redundant code and extend the use of

existing classes

 We can build programs from the standard working modules, rather than

having to start writing the code from scratch

 The principle of data hiding helps the programmer to build secure programs

 It is possible to have multiple instances of an object to co-exist without any

interference

 It is possible to map objects in the problem domain to those in the program

OOPS Using C++ 6


Benefits of OOP
 It is easy to partition the work in a project based on objects
 The data-centered design approach enables us to capture
more details of a model in implementable form
 Object oriented systems can be easily upgraded from small to
large systems
 Message passing techniques for communication between
objects makes the interface descriptions with external
systems much simpler
 Software complexity can be managed

OOPS Using C++ 7


Object-based programming languages (Major Features)

1. Data encapsulation
2. Data hiding and access mechanisms
3. Automatic initialization and clear-up of
objects
4. Operator Overloading

OOPS Using C++ 8


Object Oriented programming languages (Major
Features)

Object based features


+
Inheritance
+
Dynamic Binding

OOPS Using C++ 9


Applications of OOP
 Real Time systems
 Simulation and Modeling
 Object – oriented databases
 Hypertext and hypermedia
 AI and Expert systems
 Neural network and Parallel programming
 Decision support and Office automation
systems
 CIM / CAM / CAD systems
OOPS Using C++ 10
Types of Applications with C++
 Object oriented libraries can be developed that can
be used by several applications later.
 Since C++ is easily expandable and maintainable , any
application that always requires new additions can
be well developed with C++.
 C ++ standard library has two halves
1. Standard C library.

2. Class library (that supports object oriented


programming) OOPS Using C++ 11
Programming Features
1. Function main returns an “int”
2. Comments may be
 single line //
e.g //this is documentation
OR…..
 Multiple lines /*…….*/
e.g/* this is
documentation*/
3. Spaces and tabs can be inserted anywhere to
increase readability
e.g a=12*b-(c/2) is same as 12 * b –( c / 2)

OOPS Using C++ 12


4. cout identifier is used with operator “<<“to output
data to output devices.
e.g cout<<name;
<< is also used for bit wise left shift operations,
thus << is overloaded to perform both as shift
operator and “insertion / put to “ operator.
5. cin identifier is used with operator “>>” to take
inputs from input devices.
E.g cin>>name;
>> is also overloaded operator to perform both as
right shift and “extraction / get from”operator.
• cin and cout operators can be cascaded as
cin>>a>>b; OR cout<<“name is”<<name;
This is known as Cascading of I/O operators

OOPS Using C++ 13


NameSpace
This defines a scope for the identifiers that are
used in a program.
For using the identifiers defined in the namespace
scope we must include the using directive, like
Using namespace std;
 std is the namespace where C++ standards class
libraries are defined.
This will bring all the identifiers defined in std to
the current global scope.
OOPS Using C++ 14
Structure of C++ program
 Any C++ program includes:-

1. Header files (are referred with the help of #include


directive)
Syntax is
#include<iostream.h>( this file will be searched in space
assigned to include files)
OR
#include “iostream.h”( this file will be searched in current
working directory)
2. Class declaration
3. Member function definitions
4. Main function
OOPS Using C++ 15
These sections can be coded in different files and
then linked at the time of compilation:-
Program is organised into 3 files:-
• The class declaration are placed in a header file.
• The definition of member functions go into
another file.
• Finally main program that uses the class placed
in third file which includes other two files.
The approach is based on client-server model.
• Class definition including member function
constitute server that provides services to the
main program known as client.

OOPS Using C++ 16


Tokens
Smallest Individual units in a program.
C++ has following tokens :
• Keywords
• Identifiers
• Constant
• Operators

• Int a=10;

OOPS Using C++ 17


KEYWORDS in C++

• Are reserved identifiers in C++.


• They cannot be used as names
(identifiers) for the program variables ,
functions etc.
• Examples include:-
double, new, else, if, break, continue,
long,
int, float, for, while, class, inline, struct,
void, etc.
OOPS Using C++ 18
IDENTIFIERS in C++

 Identifiers are user defined names for variables,


functions ,labels etc.

 Rules to name a variable in C++ are


1. It can include alphabets, digits, underscore(_).

2. It can not start with a number.

3. C++ is Case sensitive thus name, and NAME are


different identifiers.

4. Keywords cannot be used as variable names.


OOPS Using C++ 19
CONSTANTS in C++

 Constants refer to fixed values that do not change


during the execution of a program.
 Like C, C++ supports various literal constants. They
include integers, characters, floating point numbers and
strings.
1. 123
2. 12.34
3. “Abc”
4. ‘h’
OOPS Using C++ 20
OPERATORS in C++

• Relational operators (represent


relationships between values)
Operator Action
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to
!= Not equals
OOPS Using C++ 21
• Logical operators (used to connect
relationships )

Operator Action
• && AND
• || OR
• ! NOT

OOPS Using C++ 22


• Bit wise operators (performs operations on
bits of char and int data types)
Operator Action
& AND
| OR
^ Exclusive OR
~ Complement
>> Shift Right
<< Shift Left

OOPS Using C++ 23


• Arithmatic Operators (perform
mathematical operations)

• Addition(+)
• Subtraction(-)
• Multiplication(*)
• Division(/)
• Modulus(%)
• Unary minus(-)
• Unary Plus(+)

OOPS Using C++ 24


• Assignment Operator(=), for assigning
value to a variable.
• scope resolution operator (:: ) to define
scope of a variable or function.
• Conditional Operator(?) is a ternary
operator can replace if-then-else structure
Expr ? Val1 : Val2
(a<5)?0:1;
Expr is evaluated, if true, value of
expression will be Val1, else Val2.
OOPS Using C++ 25
 Address of operator(&) returns the
memory address of the operand
int *p, a, b;
p = &a;
 Pointer operator(*) returns value at
address specified by the operand.
b = *p;
 Sizeof operator returns length (in bytes) of
the operand
int l , a;
l =sizeof (a);
( l will be assigned 2 (size of int data type))
OOPS Using C++ 26
DATA TYPES in C++

DATA TYPES

Built-in User-defined
Integer Structure
Derived Union
Character Array
Float Class
Pointer
Void Enumeration
Reference
Double

OOPS Using C++ 27


Void Data Types
• Two uses of void are:
1 To specify the return type of function.
2 To indicate an empty list of arguments.
Eg: void func1 (void);
• Another use of void is in declaration of generic
pointers. Eg: void *gp;
• A generic pointer can be assigned a pointer value of
any basic data type. Eg:
int *ip;
gp = ip;
OOPS Using C++ 28
Enumerated Data Type
 User Defined data type.
 Provides a way of attaching names to no.
 Enum keyword assigns the values 0,1,2 & so on. e.g
enum colour {red, green, yellow};
 We can explicitly assign the values. Eg :
enum colour {red, green=4, yellow=7};
 By using these tag names, we can declare new variables.
Eg: colour background;
 C++ doesn’t automatically convert an “int” value to an
enumerated value. (which C does)
colour c; c=red;
c=3; // wrong in C++ ,correct in C
c=colour(3); // in C++ it should be
OOPS Using C++ 29
POINTER & REFERENCE DATA TYPES

1. Pointer: A pointer is a variable that


holds the memory address of other
variable. It is of different data types,
e.g- char pointer can store address of
only char variables, int pointer can
store address of int variables and so
on.
2. Reference: A reference in the simplest
sense is an alias or alternate name for
a previously defined variable.

OOPS Using C++ 30


Declaration of variables in C++
• C++ permits declaration of variables anywhere,
before or at its first use.
• Eg.
int main()
{
…………
for(int i=1; i<5; i++)
{…….}
double sum, number;
}
OOPS Using C++ 31
Dynamic initialization of variables

• In C++ variables can be initialized at


run time( without using constant
expressions)
• Eg.
int main()
{…..
int sum=32 ,number=4;
………
float average=sum/number;
}
OOPS Using C++ 32
Reference Variables
• A reference variable provides an alias (alternative name)
for a previously defined variables. Syntax :
Data-type & reference-name = variable name ;
• Ex : float total =100;
float & sum = total;
• Here & is not an address operator but float & means
reference to float.
e.g. int x;
int *p = &x ;
int & m = *p;

OOPS Using C++ 33


//Program to illustrate References
#include<iostream.h>
int main(void)
{
int var;
int & refvar=var;
//here a reference variable to var is declared
var=10; //var is given the value 10
cout<<var<<endl;
refvar=100; //reference variable of var is changed
cout<<var; //but var also gets changed
return 0;
}
OOPS Using C++ 34
Operators in C++
• :: scope resolution operator
• delete memory release operator
• endl line feed operator
• new memory allocation operator
• setwfield width operator
• ::* Pointer to member declarator
• ->* Pointer to member operator
• .* Pointer to member operator

OOPS Using C++ 35


Scope Resolution Operator
• The scope resolution operator (::) in C++ is used to define
the already declared member functions.
• Syntax : :: variable-name
#include <iostream>
using namespace std;
int n = 12; // A global variable
int main()
{ int n = 13; // A local variable
cout << ::n << endl; // Print the global variable: 12
cout << n << endl; // Print the local variable: 13
}

OOPS Using C++ 36


Memory Management Operators
• Two operators new & delete are used to allocate the
memory dynamically. Syntax:
pointer-variable = new data-type;
• int *p = new int ;
float *q = new float;
*p = 25 ;
*q = 7.5 ;
• Can also initialize as
pointer-variable = new data-type (value);
• For arrays :
pointer-variable = new data-type [size];

OOPS Using C++ 37


• The fields must not be empty.
array_ptr = new int [4][6][6] ; // legal
array_ptr = new int [4][ ][8] ; //illegal
array_ptr = new int [ ][3][9] ; // illegal
• The delete operator is as:
delete pointer-variable ;
Ex: delete p;
delete q;
For array:
delete [size] pointer-variable;
Ex : [ ] a;

OOPS Using C++ 38


Manipulators
• The endl manipulator, causes a linefeed to be inserted. It
has the same effect as that of “\n” .
Ex: cout << «Infosys" << endl;
cout << "Training";
• The setw manipulator sets the minimum field width on
output. The syntax is: setw (x)
int main(
{
int x1=12345, x2= 23456;
cout<<setw(8) <<”Infosys”<<setw(20)<<”Values”<< endl
<< setw(8) << “E1234567” << setw(20)<< x1 << endl
<< setw(8) << “S1234567” << setw(20)<< x2 << endl;
return 0 ;
} OOPS Using C++ 39
Types of expressions
• Constant Expression (all values are
constant)
e.g. 13+2 and 6+7*67
• Integral Expression (integral results)
e.g. m*2 and a+b*3-c (a, b, c, m are
integers)
• Float Expression ( float results)
e.g. a+b/2 and 12. 34 + a
• Pointer Expression (producing address)
e.g. &m and *ptr
OOPS Using C++ 40
• Relational Expression (yielding
true/false)
e.g. a>b , (c==4)
• Logical expression (having &&,||
etc) e.g. a>2 && b>=5
• Bit wise expressions (having bitwise
operators)
e.g. a<<3 (shift 3 bit position to left)
b>>2 (shift 2 bit positions to right)

OOPS Using C++ 41


Special assignment expressions

1. Chained Assignment
z=(y=12) or z=y=12
// same z=12 and y=12
2. Embedded assignment
x=(y=22)+12;
// y is assigned 22 first then x is assigned 34.
3. Compound Assignment (combination of
assignment operator with binary arithmetic
operator)
for e.g x+=12; // same as x=x+12;
x++=x+1;
Also called as short hand operator.
OOPS Using C++ 42
Control Structures in C++
• If-else
• Switch Selection
• do-while
• while Iteration/Loop
• For

• Break
• Continue Jump
• go to
• Return

these structures are used to control the flow of execution in a


C++ program.

OOPS Using C++ 45


A C++ program
// FIRST PROGRAM
#include<iostream.h>
int main()
{
Float n1,n2,sum,average;
cout<<“enter two numbers”;
cin>>n1>>n2;
sum=n1+n2;
average=sum/2;
cout<<“sum is”<<sum<<“and average is “<<average;
return 0;
}

OOPS Using C++ 46


An example with Class
#include <iostream.h> Void person :: display (void)
Class person {
cout << “\n Name:” << name;
{ cout << “\n Age:” << age;
char name[30]; }
int age;
int main ( )
public: {
void getdata (void); //declaration person p; //Object creation
void display (void); //object.fuction_name
}; p.getdata ( ); //function call
void person :: getdata (void) p.display ( );
{
return 0;
cout << “Enter name:”; }
cin >> name;
cout << “Enter age:”;
cin >> age;
}
OOPS Using C++ 47

You might also like