1 Basics of OOP
1 Basics of OOP
Functions Functions
• They may also represent user-defined data such as vectors, time and lists. Average
Display
The Key to Success = Mind + Intellect + Soul Page | 1
Dr. Piyush Arora Object Oriented Programming using C++
• Programs objects name should match closely with the real-world objects.
• Objects represent a person, a place, a bank account, a table of data or any item that the
program handles.
• Objects take up space in the memory and have an associated address like a variable or
a structure in c.
• Objects can communicate with each other using message passing. Objects can interact
without knowing the details of each other’s data or code.
e.g.: if “customer” and “account” are two objects in a program, then the customer
object may send a message to the account object requesting for the bank balance.
2. Classes
• Classes are user-defined data types and behave like built-in data types of a
programming language.
• The entire set of data and code of an object can be made a user-defined data type with
the help of class.
• Objects are variables of the type class.
• We can create any number of objects belonging to a single class.
• Each object is associated with the data of type class with which they are created.
• A class is a collection of objects of similar types.
e.g.: Mango, Apple and orange are members of class “fruit”.
• Syntax to create an object of a class: Similar to syntax to declare a variable
➔ Class_name object_name It is same as “data_type variable_name”
e.g.: Fruit Mango It is same as “int a”
4. Abstraction
• Abstraction refers to the act of representing essential features without including the
background details or explanation.
• Classes use the concept of abstraction.
• We define the list of attributes of a class such as size, weight, and cost. Attributes are
also called as data members.
• We define the functions that operate on these attributes. Functions are also called as
methods or member functions.
e.g.: When we define an object “duster”, we simply use that object, without even
thinking that how the duster is made, what is the size (dimension) or weight of the
duster, etc.
5. Inheritance
• Through inheritance, objects of one class acquire the properties of objects of another
class.
• It supports the concept of hierarchical classification.
• e.g.: The bird, ‘Sparrow’ is a part of class ‘flying bird’ which is again a part of the
class ‘bird’.
• Each derived class shares common characteristics with the class from which it is
derived (Figure below).
• In OOP, the concept of inheritance provides the idea of reusability. This means that we
can add additional features to an existing class without modifying it.
• We can derive a new class from the existing class. The new class will have the
combined features of both the classes.
• So, we can reuse the class.
• Without inheritance, each class would be required to include all the features in it.
Bird Shape
Attributes
Feathers Draw( )
Lay Eggs
8. Message Passing
• Objects communicate with each other by sending and receiving information.
• The concept of message passing makes it easier to build systems that describe the real-
world situations.
• A Message for an object is a “Request for execution of a procedure”. So, a message
will call a function (procedure) in the receiving object that generates the desired
results.
• Object has a life cycle. They can be created and destroyed. Objects can communicate
as long as they are alive.
• Message passing involves specifying:
a. The name of object
b. The name of the function (message)
c. The information to be sent
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 that communicate with one
another.
• We don’t require to write the code from the scratch (starting). So, we can save
development time and we can have higher productivity.
• The principle of data hiding helps the programmer to build secure programs that cannot
be accessed by the code in other parts of the programs.
• It is possible to have multiple instances of an object to co-exist without any
interference.
• It is possible to create objects in the program that define real world problems.
• It is easy to partition the work in a project based on objects. So, work load can be
distributed.
• By using data-centered design approach, we can design the model of a system easily
and its implementation is easy.
• Object-oriented systems can be easily upgraded from small systems to large systems.
• Message passing techniques for communication allow the objects to have easy
communication that makes the interface easy.
• Software complexity can be easily managed.
Types of Languages based on Object Oriented Features
The Key to Success = Mind + Intellect + Soul Page | 4
Dr. Piyush Arora Object Oriented Programming using C++
a. Object – Based Programming Languages
• These languages support:
o Data encapsulation and object identity
o Data hiding (Encapsulation) and access mechanisms
o Automatic initialization and clear-up of objects (e.g.: Constructor and destructor)
o Operator overloading
• These languages do not support:
o Inheritance
o Dynamic Binding
e.g.: Ada language
b. Object – Oriented Programming Languages
• These languages support all object-oriented features.
• So, we can say that: OOP = Object-Based features + Inheritance + Dynamic Binding
e.g.: C++, java, Smalltalk, Pascal language
Applications of OOP
Object oriented languages can be used in the Design of:
• user interface, such as windows
• Real-time systems
• Simulation and modeling
• Object-oriented databases
• Hypertext, Hypermedia
• AI (Artificial Intelligence) and expert systems
• Neural networks and parallel programming
• Decision support and Office Automation Systems (OAS)
• Computer Integrated Manufacturing (CIM), Computer Aided (Assisted) Manufacturing
(CAM), Computer Aided (Assisted) Design (CAD) systems
Comments in C++
We can specify comments in two ways:
1. Single line comments:
By using symbol // (double slash).
A comment may start anywhere in the line, and whatever follows till the end of the line
is ignored.
Note that there is no closing symbol.
e.g.: // This is an example of …….
// C++ program to illustrate …….
2. Multiline comments:
By using the symbols /*…….*/
e.g.: /* This is an example of
C++ program to illustrate
some of its features */
Output Operator
• The statement is:
cout << “C++ is better than C.”;
• The operator << is called the insertion or put to operator. It inserts (or sends) the
contents of the variable on its right to the object on its left.
• We can also write:
o cout << string;
Here, string is the variable name. It will display the contents of the variable.
Input Operator
• Suppose we have a statement:
o cin >> a;
• It 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 a.
• The identifier cin is a predefined object in C++ that corresponds to the standard input
stream. Here, this stream represents the keyboard.
• The operator >> is known as extraction or get from operator. It extracts (or takes) the
value from the keyboard and assigns it to the variable on its right.
o cout << “Sum = ” << sum << “\n” << “Average = ” << average << “\n”;
• This is one statement, but provides two lines of output, because of “\n”
• If you want only one line of output, the statement will be:
o cout << “Sum =” << sum << “,” << “Average =” << average << “\n”;
• The output will be:
o Sum = 14, average = 7
Tokens in C++
It consists of:
1. Keywords 2. Identifiers 3. Constants 4. Strings 5. Operators
• Built-in data types are also called as basic or fundamental data types.
• We can specify the modifiers before the built in data types. This is useful in several
situations.
e.g.: signed, unsigned, short, long – these are generally applied to character and integer
basic data types.
e.g.: long – is applied to double.
Size and Range of data types with modifiers for 16-bit word machine
The Key to Success = Mind + Intellect + Soul Page | 9
Dr. Piyush Arora Object Oriented Programming using C++
Type Size (in Bytes) Range
char 1 -128 to 127
unsigned char 1 0 to 255
signed char 1 -128 to 127
int 2 -32768 to 32767
unsigned char 2 0 to 65535
signed char 2 -32768 to 32767
short int 2 -32768 to 32767
unsigned short int 2 0 to 65535
signed short int 2 -32768 to 32767
long int 4 -2147483648 to 2147483647
signed long int 4 -2147483648 to 2147483647
unsigned long int 4 0 to 4294967295
float 4 3.4E -38 to 3.4E +38
double 8 1.7E -308 to 1.7E +38
long double 10 3.4E -4932 to 1.1E +4932
Uses of void
Void data type is used for following purposes:
a. To specify the return type of a function, when it is not returning any value
void addition ( );
c. Pointers
• Pointers are declared and used in the same way as in C.
int *p, x;
x = 10;
The Key to Success = Mind + Intellect + Soul Page | 11
Dr. Piyush Arora Object Oriented Programming using C++
p = &x;
*p = 25;
• C++ also uses the concept of constant pointer and pointer to constant.
o Constant Pointer
char * const ptr1 = “Good”; // Constant Pointer
We cannot modify the address that ptr1 is initialized to.
o Pointer to a constant
int const * ptr2 = &m;
ptr2 is declared as pointer to a constant. It can point to any variable of correct
type, but wherever it points, its contents cannot be changed.
• We can declare both the pointer and the variable as constants in the following way:
const char * const cp = “Good”;
This statement declares cp as a constant pointer to the string which has been declared
as a constant.
So, neither we can change the address assigned to the pointer cp nor the contents it
points to.
Symbolic Constants
We can create symbolic constants in C++ in two ways:
1. By using the qualifier const.
• Any value declared as const, cannot be modified by the program in any way. So, we
cannot change its value.
• We can use const in a constant expression, such as:
const int size = 10;
char name[size];
Note: This was illegal in C;
• So, we don’t need to create constants using #define.
• If we use long and short keywords with const modifier it defaults to int.
const size = 10;
Means
const int size = 10;
• In C++, variable declared as const have local scope. It is local to the file where it is
declared.
• In C, variables declared as const have global scope. They are visible outside the file,
where they are declared. We can make them local by declaring them as static.
• If we want that the const value should be allowed to be referenced by another file (used
outside the file), we must define it as extern.
extern const total = 100;
2. By defining a set of integer constants using enum keyword. (Also refer enum in user
defined data types)
• We can create integer constants by enumeration.
enum { x, y, z };
This define x, y, z as integer constants with value 0, 1, 2 respectively. This is similar
to:
const x = 0;
const y = 1;
The Key to Success = Mind + Intellect + Soul Page | 12
Dr. Piyush Arora Object Oriented Programming using C++
const z = 2;
• We can also assign value to x, y and z explicitly.
enum { x = 100, y = 50, z = 200 };
Type Compatibility
• If we define variables of one data types, compiler will not automatically convert them
to different types.
• The int, short int and long int data types are different in C++. They must be cast (data
type must be changed), when their values are assigned to one another.
• Even if the size of unsigned char, char and signed char is same, that is 1 byte, they are
considered as different data types.
• We need to do casting of data types that is conversion to correct types, to support
function overloading.
• In function overloading, we have two functions with the same name, but different
number or different types of arguments.
• There is one major difference in the way, how char constants are stored in C and C++.
o In C,
sizeof (‘x’)
is equivalent to:
sizeof (int)
o In C++,
sizeof (‘x’)
Is equivalent to:
sizeof (char)
Declaration of variables
• In C, we have to declare and define all the variables at the beginning of the scope.
• So, we declare them at the top in the beginning of the program, where the main
function starts, or some other function starts.
• We cannot declare them in the middle of the program or function.
• We actually use them elsewhere in the program, which may be too far from the
declaration location.
• So, if we need a new variable, we again need to go back to the beginning of the
program and declare it.
• In C++, we can declare the variables, anywhere in the scope of the program.
• So, we can declare the variable at the place of its first use.
• So, this makes the program much easier to write and reduces the errors that may be
caused by scanning back and forth (up and down) in the program.
• Also, understanding the program becomes easy.
Reference Variables