Introduction to C++
C++ is an object-Oriented programming language. Initially named ‘C with Classes’,
C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories in New
Jersey, USA, on the early eighties.
C++ is an extension of C with a major addition of the class construct feature of
SIMULA67.
C++ is a superset of C. Most of what we already know about C applies to C++ also.
Therefore, almost all C programs are also C++ programs with slight modifications.
The most important facilities that C++ adds on to C are classes, objects,
inheritance, function overloading, and operator overloading. These features
enable us to create abstract data types, inherit properties from existing data types
and support polymorphism, thus making C++ a truly object-Oriented Language.
The Object-Oriented features in C++ allow programmers to build large programs
with clarity, extensibility and ease of maintenance, incorporating the spirit and
efficiency of C.
Features Object-Oriented Programming(OOPs)
Explain the features of OOPs.
The following are the some of the features to know extensively about the Object-
Oriented Programming.
1) Objects
2) Classes
3) Encapsulation
4) Inheritance
5) Multiple inheritance
6) Polymorphism
7) Dynamic hiding
8) Message Passing
9) Data Abstraction
10)Extensibility
11)Persistence
Mrs. V Sailaja, Dept. of Computer Science Page 1
,B.Sc I Year/II Semester UNIT I OOP’S USING C++
12)Genericity
13)Data binding
1) Object:
Objects are the basic run-time entities in an Object-Oriented system. They
may represent a person, a place, a bank account, a table of data or any item
that the program must handle.
Object is a collection of number of entities. Objects take up space in the
memory.
Objects are instances of classes. When a program is executed , the objects
interact by sending messages to one another. Each object contains data and
code to manipulate the data.
Objects can interact without having known details of each other’s data or
code.
An object is considered to be a partitioned area of computer memory that
stores data and set of operations that can access that data.
2) Class :
It is an abstract data type that contains data members and member functions
that operates on data. It starts with the keyword class.
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 to type class. Once a class has been defined, we can
create any number of objects belonging to that class.
3) Encapsulation:
The wrapping of data and functions into a single unit 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 provides an
interface between object’s data and the program. This insulation of the data from
direct access by the program is called data hiding.
4) Inheritance:
Inheritance is the process by which objects of one class acquire the
properties of objects of another class.
Mrs. V Sailaja, Dept. of Computer Science Page 2
,B.Sc I Year/II Semester UNIT I OOP’S USING C++
That is, deriving a new class from existing class. Here new class is called
derived class where as existing class is called base class.
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.
This is possible by deriving a new class from the existing one. The new class
will have the combined features of both the classes.
5) Multiple Inheritance:
The mechanism by which a class is derived from more than one base class is known
as multiple inheritance.
6) Polymorphism:
Polymorphism is another important OOP concept.
Polymorphism means the ability to take more than one form.
For example, an operation may exhibit different behavior ion different
instances. The behavior depends upon the types of data used in the
operation.
Function overloading and operator overloading are the examples of
polymorphism.
7) Data hiding:
We can hide the information of class like we can hide data and member
functions of a class by specifying private and protected members of class. Private
and protected members are not accessed by outside the class, thus we can hide the
data of a class.
8) Message Passing:
An Object-Oriented program consists of a objects that communicate with
each other.
Objects communicates with one another by sending and receiving
information much the same way as people pass messages to one another.
A message for an object is a request for execution of a procedure, and
therefore will invoke a function in the receiving object that generates the
desired result.
Mrs. V Sailaja, Dept. of Computer Science Page 3
, B.Sc I Year/II Semester UNIT I OOP’S USING C++
Message passing involves specifying the name of the object, the name of the
function and the information to be sent.
Objects have a life cycle. They can be created and destroyed.
9) Data Abstraction:
Abstraction refers to the act of representing essential features without
including the background details or explanation. Since the classes use the concept of
data abstraction, they are known as Abstract Data Types (ADTs).
10) Extensibility: It is a feature which allows the extension of the functionality
of the existing software components. In C++, this is achieved through inheritance.
11) Persistence: The mechanism where the object outlives the program
execution time and exists between executions of a program is known as persistence.
12) Genericity: It is a technique for defining software components that have
more than one interpretation depending on the data type. In C++, genericity is
achieved by function templates and class templates.
13) Dynamic Binding: Binding refers to the linking of a procedure call to the
code to be executed in response to the call. Dynamic binding means that the code
associated with it given procedure call is not known until the time of the call at run-
time. It is associated with Polymorphism and Inheritance. A function call associated
with a polymorphic reference depends on the dynamic type of that reference.
Adding Comments in a program
C++ introduces a new comment symbol “//”(double slash). Comments start with
a double slash symbol and terminate at the end of the line. A comment may start
anywhere in the line. The double slash comment is basically a single line
comment.
Ex: //this is an example
The C comment symbols “/* , */are still valid and are more suitable for multiline
comments.
Ex: /*this is an example of
C++ program */.
Mrs. V Sailaja, Dept. of Computer Science Page 4