Features of Oops Are The Following:: Objects
Features of Oops Are The Following:: Objects
Encapsulation
Data abstraction
Inheritance
Polymorphism
Message passing
Extensibility
Persistence
Delegation
Genericity
Multiple Inheritance
Objects:
Objects are the basic run-time entities in an object-oriented system. Every object is
associated with data and functions which define meaningful operations on that object.
What is Encapsulation
It is a mechanism that associates the code and the data it manipulates into a single unit to
and keeps them safe from external interference and misuse. In C++ this is supported by
construct called class. An instance of an object is known as object which represents a real
world entity
constructor
A constructor takes the same name as the class name.
The programmer cannot declare a constructor as virtual or static, nor can the programmer
declare a constructor as const, volatile, or const volatile.
No return type is specified for a constructor.
The constructor must be defined in the public. The constructor must be a public member.
Objects
Classes
Inheritance
Data Abstraction
Data Encapsulation
Polymorphism
Overloading
Reusability
Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique
name. An object represents a particular instance of a class. There can be more than one instance
of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also
known as methods.
Classes:
Classes are data types based on which objects are created. Objects with similar properties and
methods are grouped together to form a Class. Thus a Class represent a set of individual objects.
Characteristics of an object are represented in a class as Properties. The actions that can be
performed by objects becomes functions of the class and is referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR
represents individual Objects. In this context each Car Object will have its own, Model, Year of
Manufacture, Colour, Top Speed, Engine Power etc., which form Properties of the Car class and
the associated actions i.e., object functions like Start, Move, Stop form the Methods of Car
Class.
No memory is allocated when a class is created. Memory is allocated only when an object is
created, i.e., when an instance of a class is created.
Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The base
class is also known as parent class or super class, The new class that is formed is called derived
class. Derived class is also known as a child class or sub class. Inheritance helps in reducing the
overall code size of the program, which is an important concept in object-oriented programming.
Data Abstraction:
Data Abstraction increases the power of programming language by creating user defined data
types. Data Abstraction also represents the needed information in the program without presenting
the details.
Data Encapsulation:
Data Encapsulation combines data and functions into a single unit called Class. When using Data
Encapsulation, data is not accessed directly; it is only accessible through the functions present
inside the class. Data Encapsulation enables the important concept of data hiding possible.
Polymorphism:
Polymorphism allows routines to use variables of different types at different times. An operator
or function can be given different meanings or functions. Polymorphism refers to a single
function or multi-functioning operator performing in different ways.
Overloading:
Overloading is one type of Polymorphism. It allows an object to have different meanings,
depending on its context. When an exiting operator or function begins to operate on new data
type, or class, it is understood to be overloaded.
Reusability:
This term refers to the ability for multiple programmers to use the same written and debugged
existing class of data. This is a time saving device and adds code efficiency to the language.
Additionally, the programmer can incorporate new features to the existing class, further
developing the application and allowing users to achieve increased performance. This time
saving feature optimizes code, helps in gaining secured applications and facilitates easier
maintenance on the application.
The implementation of each of the above object-oriented programming features for C++ will be
highlighted in later sections.
Constants
Constants have fixed value. Constants, like variables, contain data type. Integer constants are
represented as decimal notation, octal notation, and hexadecimal notation. Decimal notation is
represented with a number. Octal notation is represented with the number preceded by a zero
character. A hexadecimal number is preceded with the characters 0x.
Access specifiers:
Access specifiers are used to identify access rights for the data and member functions of the
class. There are three main types of access specifiers in C++ programming language:
private
public
protected
A private member within a class denotes that only members of the same
class have accessibility. The private member is inaccessible from outside the
class.
.
Creation of Objects:
Once the class is created, one or more objects can be created from the class as objects are
instance of the class.
The dot (.) used above is called the dot operator or class member access operator. The dot
operator is used to connect the object and the member function
What is a Stream?
A stream is an object where a program can either insert or extract characters to or from it. The
standard input and output stream objects of C++ are declared in the header file iostream.
Copy constructor:
This constructor takes one argument. Also called one argument constructor. The main use of
copy constructor is to initialize the objects while in creation, also used to copy an object. The
copy constructor allows the programmer to create a new object from an existing one by
initialization.
For example to invoke a copy constructor the programmer writes:
Exforsys e3(e2);
or
Exforsys e3=e2;
Both the above formats can be sued to invoke a copy constructor.
For Example:
#include <iostream.h>
class Exforsys
{
private:
int a;
public:
Exforsys()
{ }
Exforsys(int w)
{
a=w;
}
Exforsys(Exforsys& e)
{
a=e.a;
cout<< Example of Copy Constructor;
}
void result()
{
cout<< a;
}
};
void main()
{
Exforsys e1(50);
Exforsys e3(e1);
cout<< ne3=;e3.result();
In the above the copy constructor takes one argument an object of type Exforsys which is passed
by reference. The output of the above program is
Destructors
What is the use of Destructors
Destructors are also special member functions used in C++ programming language. Destructors
have the opposite function of a constructor. The main use of destructors is to release dynamic
allocated memory. Destructors are used to free memory, release resources and to perform other
clean up. Destructors are automatically named when an object is destroyed. Like constructors,
destructors also take the same name as that of the class name.
Some important points about destructors:
Destructors take the same name as the class name.
Like the constructor, the destructor must also be defined in the public. The destructor
must be a public member.
The Destructor does not take any argument which means that destructors cannot be
overloaded.
What is an array?
An array is a group of elements of the same type that are placed in contiguous memory locations.
What is a Structure?
Structure is a collection of variables under a single name. Variables can be of any type: int, float,
char etc. The main difference between structure and array is that arrays are collections of the
same data type and structure is a collection of variables under a single name.
Operator Overloading
What is a function?
A function is a structure that has a number of program statements grouped as a unit with a name
given to the unit. Function can be invoked from any part of the C++ program.
Passed By Value
Passed By Reference
Passed By Reference:
Passed by reference indicates a contrast in that the variables are not passed by value. When a
variable is passed by reference, it passes the variable to the function definition and not the copies
of the value. Any changes to the variable in function definition would effect or reflect
corresponding changes in the variables passed as reference.
The symbol used for denoting the passed by reference is & (ampersand).
Local Variables
Global Variables
Local Variables:
The variables defined local to the block of the function would be accessible only within the block
of the function and not outside the function. Such variables are called local variables
Global Variables:
Global variables are one which are visible in any part of the program code and can be used
within all functions and outside all functions used in the program. The method of declaring
global variables is to declare the variable outside the function or block.
C++ Inheritance
What is Inheritance?
Inheritance is the process by which new classes called derived classes are created from existing
classes called base classes. The derived classes have all the features of the base class and the
programmer can choose to add new features specific to the newly created derived class.
For example, a programmer can create a base class named fruit and define derived classes as
mango, orange, banana, etc. Each of these derived classes, (mango, orange, banana, etc.) has
all the features of the base class (fruit) with additional attributes or features specific to these
newly created derived classes. Mango would have its own defined features, orange would have
its own defined features, banana would have its own defined features, etc.
This concept of Inheritance leads to the concept of polymorphism.
For example, if the base class is exforsys and the derived class is sample it is specified as:
class sample: public exforsys
The above makes sample have access to both public and protected variables of base class
exforsys. Reminder about public, private and protected access specifiers:
Inheritance Example:
class exforsys
{
public:
exforsys(void) { x=0; }
void f(int n1)
{
x= n1*5;
}
private:
int x;
};
void output(void)
{
exforsys::output();
cout << s1;
}
private:
int s1;
};
int main(void)
{
sample s;
s.f(10);
s.output();
s.f1(20);
s.output();
}
In the above example, the derived class is sample and the base class is exforsys. The derived
class defined above has access to all public and private variables. Derived classes cannot have
access to base class constructors and destructors. The derived class would be able to add new
member functions, or variables, or new constructors or new destructors. In the above example,
the derived class sample has new member function f1( ) added in it. The line:
sample s;
creates a derived class object named as s. When this is created, space is allocated for the data
members inherited from the base class exforsys and space is additionally allocated for the data
members defined in the derived class sample.
The base class constructor exforsys is used to initialize the base class data members and the
derived class constructor sample is used to initialize the data members defined in derived class.
What is Abstraction
The concept of abstraction relates to the idea of hiding data that are not needed for presentation.
The main idea behind data abstraction is to give a clear separation between properties of data
type and the associated implementation details. This separation is achieved in order that the
properties of the abstract data type are visible to the user interface and the implementation details
are hidden. Thus, abstraction forms the basic platform for the creation of user-defined data types
called objects. Data abstraction is the process of refining data to its essential form. An Abstract
Data Type is defined as a data type that is defined in terms of the operations that it supports and
not in terms of its structure or implementation.
In object-oriented programming language C++, it is possible to create and provide an interface
that accesses only certain elements of data types. The programmer can decide which user to give
or grant access to and hide the other details. This concept is called data hiding which is similar in
concept to data abstraction.
through a specific interface defined to invoke the function. In contrast, in data abstraction, access
to the data is provided through a specific set of operations defined to examine and manipulate the
data. For instance, when a programmer is using C++ standard data types, this means that users
are using the concept of data abstraction. When using data types, the users are not concerned
with how the data is stored but they are concerned with what operations are provided and what
properties are supported.
C++ Encapsulation
Encapsulation is the process of combining data and functions into a single unit called class.
Using the method of encapsulation, the programmer cannot directly access the data. Data is only
accessible through the functions present inside the class. Data encapsulation led to the important
concept of data hiding. Data hiding is the implementation details of a class that are hidden from
the user. The concept of restricted access led programmers to write specialized functions or
methods for performing the operations on hidden members of the class. Attention must be paid to
ensure that the class is designed properly.
Neither too much access nor too much control must be placed on the operations in order to make
the class user friendly. Hiding the implementation details and providing restrictive access leads
to the concept of abstract data type. Encapsulation leads to the concept of data hiding, but the
concept of encapsulation must not be restricted to information hiding. Encapsulation clearly
represents the ability to bundle related data and functionality within a single, autonomous entity
called a class.
For instance:
class Exforsys
{
public:
int sample();
int example(char *se)
int endfunc();
.........
......... //Other member functions
private:
int x;
float sq;
..........
......... //Other data members
};
In the above example, the data members integer x, float sq and other data members and member
functions sample(),example(char* se),endfunc() and other member functions are bundled and put
inside a single autonomous entity called class Exforsys. This exemplifies the concept of
Encapsulation. This special feature is available in object-oriented language C++ but not available
in procedural language C. There are advantages of using this encapsulated approach in C++. One
advantage is that it reduces human errors. The data and functions bundled inside the class take
total control of maintenance and thus human errors are reduced. It is clear from the above
example that the encapsulated objects act as a black box for other parts of the program through
interaction. Although encapsulated objects provide functionality, the calling objects will not
know the implementation details. This enhances the security of the application.
The key strength behind Data Encapsulation in C++ is that the keywords or the access specifiers
can be placed in the class declaration as public, protected or private. A class placed after the
keyword public is accessible to all the users of the class. The elements placed after the keyword
private are accessible only to the methods of the class. In between the public and the private
access specifiers, there exists the protected access specifier. Elements placed after the keyword
protected are accessible only to the methods of the class or classes derived from that class.
The concept of encapsulation shows that a non-member function cannot access an object's
private or protected data. This adds security, but in some cases the programmer might require an
unrelated function to operate on an object of two different classes. The programmer is then able
to utilize the concept of friend functions. Encapsulation alone is a powerful feature that leads to
information hiding, abstract data type and friend functions.
C++ Polymorphism
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives
different meanings or functions to the operators or functions. Poly, referring to many, signifies
the many uses of these operators and functions. A single function usage or an operator
functioning in many ways can be called polymorphism. Polymorphism refers to codes,
operations or objects that behave differently in different contexts.
Types of Polymorphism:
C++ provides three different types of polymorphism.
Virtual functions
Function name overloading
Operator overloading
In addition to the above three types of polymorphism, there exist other kinds of polymorphism:
run-time
compile-time
ad-hoc polymorphism
parametric polymorphism
run-time:
The run-time polymorphism is implemented with inheritance and virtual functions.
compile-time:
The compile-time polymorphism is implemented with templates.
ad-hoc polymorphism:
If the range of actual types that can be used is finite and the combinations must be individually
specified prior to use, this is called ad-hoc polymorphism.
parametric polymorphism:
If all code is written without mention of any specific type and thus can be used transparently
with any number of new types it is called parametric polymorphism.
For example: a Make function in a class Vehicle may have to make a Vehicle with red color. A
class called FourWheeler, derived or inherited from Vehicle, may have to use a blue background
and 4 tires as wheels. For this scenario, the Make function for FourWheeler should now have a
different functionality from the one at the class called Vehicle. This concept is called Virtual
Function.
Virtual Functions are resolved during run-time or dynamic binding. Virtual functions are also
simple member functions. The main difference between a non-virtual C++ member function and
a virtual member function is in the way they are both resolved. A non-virtual C++ member
function is resolved during compile time or static binding. Virtual Functions are resolved during
run-time or dynamic binding
When a class is declared as a friend, the friend class has access to the
private data of the class that made this a friend.
.
The function can be invoked without the use of an object. The friend
function has its argument as objects, seen in example below.
name and the scope resolution operator, unlike in normal member functions where these are not
used.
The programmer must note the following while using static member functions:
A static member function can only access static member data, static member functions
and data and functions outside the class. The programmer must take note not to use static
member function in the same manner as non-static member function, as non-static
member function can access all of the above including the static data member.
.
A non-static member function can be declared as virtual but care must be taken not to
declare a static member function as virtual.
.
The programmer must first understand the concept of static data while learning the
context of static functions. It is possible to declare a data member of a class as static
irrespective of it being a public or a private type in class definition. If a data is declared
as static, then the static data is created and initialized only once. Non-static data members
are created again and again. For each separate object of the class, the static data is created
and initialized only once. As in the concept of static data, all objects of the class in static
functions share the variables. This applies to all objects of the class.
.
A non-static member function can be called only after instantiating the class as an object.
This is not the case with static member functions. A static member function can be called,
even when a class is not instantiated.
.
A static member function cannot have access to the 'this' pointer of the class.
C++ Pointers
Concept of Pointers:
Every storage location of memory has an associated address. Address is a number that grows
sequentially. For every program placed in memory, each variable or function in the program has
an associated address.
Pointer to Void
General Syntax:
void* pointer_variable;
NULL Pointer:
The concept of NULL pointer is different from the above concept of void pointer. NULL pointer
is a type of pointer of any data type and generally takes a value as zero. This is, however, not
mandatory. This denotes that NULL pointer does not point to any valid memory address.
new
delete
These two memory management operators are used for allocating and freeing memory blocks in
efficient and convenient ways.
New operator:
The new operator in C++ is used for dynamic storage allocation. This operator can be used to
create object of any type.
delete operator:
The delete operator in C++ is used for releasing memory space when the object is no longer
needed. Once a new operator is used, it is efficient to use the corresponding delete operator for
release of memory.
General syntax of delete operator in C++:
The general syntax of delete operator in C++ is as follows:
delete pointer variable;
In the above example, delete is a keyword and the pointer variable is the pointer that points to the
objects already created in the new operator. Some of the important points the programmer must
note while using memory management operators are described below:
The programmer must take care not to free or delete a pointer variable
that has already been deleted.
.
We know that sizeof operator is used for computing the size of the
object. Using memory management operator, the size of the object is
automatically computed.
.
The programmer must take care not to free or delete pointer variables
that have not been allocated using a new operator.
.