Unit 4 C++ Basic Object Oriented Programming
Unit 4 C++ Basic Object Oriented Programming
Object: - In C++, Object is a real-world entity, for example, chair, car, pen,
mobile, laptop etc.
In other words, object is an entity that has state and behaviour. Here, state
means data and behaviour means functionality.
Class:- A group of objects that share common properties for data part and some
program part are collectively called as class.
In C ++ a class is a new data type that contains member variables and member
functions that operate on the variables.
DATA ABSTRACTION:
Abstraction refers to the act of representing essential features without
including the background details or explanations. Classes use the concept of
abstraction and are defined as size, width 6.-and cost and functions to operate
on the attributes.
DATA ENCAPSALATION:
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. The data is not accessible to the outside world and only those
functions which are wrapped in the class can access it. These functions provide
the interface between the objects data and the program.
POLYMORPHISIM:
Polymorphism means the ability to take more than one form. An operation may
exhibit different instance. The behaviour depends upon the type of data used in
the operation. A language feature that allows a function or operator to be given
more than one definition. The types of the arguments with which the function
or operator is called determines which definition will be used.
INHERITENCE:
Inheritance is the process by which objects of one class acquire the properties
of another class. In the concept of inheritance provides the idea of reusability.
This mean that we can add additional features to an existing class without
modifying it. This is possible by designing a new class will have the combined
features of both the classes.
C++ supports five types of inheritance:
1. Single inheritance
2. Multiple inheritance
3. Hierarchical inheritance
4. Multilevel inheritance
5. Hybrid inheritance
DYNAMIC BINDING:
Binding refers to the linking of a procedure call to the code to the executed in
response to the call. Dynamic binding means the code associated with a given
procedure call is not known until the time of the call at run-time. It is associated
with a polymorphic reference depends upon the dynamic type of that
reference.
MESSAGE PASSING: An object-oriented program consists of a set of objects that
communicate with each other. A message for an object is a request for
execution of a procedure and therefore will invoke a function (procedure) in the
receiving object that generates the desired result. Message passing involves
specifying the name of the object, the name of the function (message) and
information to be sent.
C++ Comments
Comments can be used to explain C++ code, and to make it more readable. It
can also be used to prevent execution when testing alternative code.
Comments can be singled-lined or multi-lined.
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the compiler (will not
be executed).
C++ Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler:
Output Operator: (cout<<) The statement cout <<”Hello, world” displayed the
string with in quotes on the screen. The identifier cout can be used to display
individual characters, strings and even numbers. It is a predefined object
that corresponds to the standard output stream. Stream just refers to a flow of
data and the standard Output stream normally flows to the screen display.
The cout object, whose properties are defined in iostream.h represents that
stream.
The insertion operator << also called the ‘put to’ operator directs the
information on its right to the object on its left.
void person::display()
{
cout<<"name:"<<name;
cout<<"age:"<<age;
}
int main( )
{
person p;
p.getdata();
p.display();
return(0);
}
What is a namespace?
Namespaces provide a way of declaring variables within a program that have
similar names. It allows users to define functions with the same name as a
function in a pre-defined library or used-defined functions within main().
Namespaces can also be used to define classes, variable names, and functions.
Namespaces allow us to group named entities that otherwise would have global
scope into narrower scopes, giving them namespace scope. This allows
organizing the elements of programs into different logical scopes referred to by
names.
Namespace is a feature added in C++ and not present in C.
• A namespace is a declarative region that provides a scope to the
identifiers (names of the types, function, variables etc) inside it.
• Multiple namespace blocks with the same name are allowed. All
declarations within those blocks are declared in the named scope.
• A namespace definition begins with the keyword namespace followed by the
namespace name as follows:
namespace namespace_name
{
int x, y; // code declarations where
// x and y are declared in
// namespace_name's scope
}
• Namespace declarations appear only at global scope.
• Namespace declarations can be nested within another namespace.
• Namespace declarations don’t have access specifiers. (Public or private)
• No need to give semicolon after the closing brace of definition of
namespace.
• We can split the definition of namespace over several units.
Example :
#include <iostream>
using namespace std;
namespace foo
{
int value()
{
return 5;
}
}
namespace bar
{
const double pi = 3.1416;
double value()
{
return 2*pi;
}
}
int main () {
cout << foo::value() << '\n';
cout << bar::value() << '\n';
cout << bar::pi << '\n';
return 0;
}
Namespace aliasing
Existing namespaces can be aliased with new names, with the following syntax:
TOKENS:
The smallest individual units in program are known as tokens. C++ has the
following tokens.
1. Keywords
2. Identifiers
3. Constants
4. Strings
5. Operators
KEYWORDS:
Keywords are those words whose meaning is already defined by Compiler.
These keywords cannot be used as an identifier. Note that keywords are the
collection of reserved words and predefined identifiers. Predefined identifiers
are identifiers that are defined by the compiler but can be changed in meaning
by the user.
There are a total of 95 reserved words in C++. The reserved words of C++ may
be conveniently placed into several groups. In the first group, we put those that
were also present in the C programming language and have been carried over
into C++. There are 32 of these.
IDENTIFIERS:
Identifiers refers to the name of variable, functions, array, class etc. created by
programmer. Each language has its own rule for naming the identifiers.
The following rules are common for both C and C++.
1. Only alphabetic chars, digits and underscore are permitted.
2. The name can’t start with a digit.
3. Upper case and lower-case letters are distinct.
4. A declared keyword can’t be used as a variable name.
Variable
A variable is a name given to a memory location. It is the basic unit of storage in
a program.
A variable name can consist of alphabets (both upper and lower case), numbers
and the underscore ‘_’ character. However, the name must not start with a
number.
Integer Constants
As the name itself suggests, an integer constant is an integer with a fixed value,
that is, it cannot have fractional value like 10, -8, 2019.
For example,
String Constants
A string constant is an array of characters that has a fixed value enclosed within
double quotation marks ( “ “ ).
Boolean Literals
There are two Boolean literals and they are part of standard C++ keywords −
You should not consider the value of true equal to 1 and value of false equal to 0.
Operator In C++
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C++ is rich in built-in operators and provide the following types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Arithmetic Operators They are the types of operators used for
performing mathematical/arithmetic operations. They include:
Operator Description
Relational Operators
These types of operators perform comparisons on operands. For
example, you may need to know which operand is greater than the
other, or less than the other. They include:
Logical Operators
The logical operators combine two/more constraints/conditions.
Logical operators also complement evaluation of original condition
under consideration. They include:
&& logical AND operator. The condition is true if both operands are
not zero.
|| logical OR operator. The condition is true if one of the operands is
non-zero.
! logical NOT operator. It reverses operand's logical state. If the
operand is true, the ! operator makes it false.
Bitwise Operators
Bitwise operators perform bit-level operations on operands. First,
operators are converted to bit level then operations are performed
on the operands. When arithmetic operations like addition and
subtraction are done at bit level, results can be achieved faster. They
include:
• & (bitwise AND). It takes 2 numbers (operands) then performs
AND on each bit of two numbers. If both are 1, AND returns 1,
otherwise 0.
• | (bitwise OR) Takes 2 numbers (operands) then performs OR
on every bit of two numbers. It returns 1 if one of the bits is 1.
• ^ (the bitwise XOR) Takes 2 numbers (operands) then
performs XOR on every bit of 2 numbers. It returns 1 if both bits
are different.
• << (left shift) Takes two numbers then left shifts the bits of
the first operand. The second operand determines total places
to shift.
• >> (right shift) Takes two numbers then right shifts the bits of
the first operand. The second operand determines number of
places to shift.
• ~ (bitwise NOT). Takes number then inverts all its bits.
Assignment Operators
Assignment operators assign values to variables. The operand/variable is added
to left side of the operator while the value is added to the right side of the
operator. The variable and the value must belong to the same data type,
otherwise, the C++ compiler will raise error. For example:
x = 5;
sizeof operator
This operator determines a variable's size. Use sizeof operator to determine the
size of a data type. For example: sizeof(int)
• character
• integer
• floating point
• boolean
• double floating point
• void
• wide character