module1_C++
module1_C++
Module-1
Introduction to Object Oriented Programming: Computer programming background- C++ overview.
First C++ Program -Basic C++ syntax,
Object Oriented Programming: What is an object, Classes, methods and messages, abstraction and
encapsulation, inheritance, abstract classes, polymorphism.
INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
1.1 Computer programming background
The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing
work for his Ph.D. thesis. He began work on "C with Classes", which as the name implies was meant to
be a superset of the C language. His goal was to add object-oriented programming into the C language,
which was and still is a language well-respected for its portability without sacrificing speed or low-level
functionality.
His language included classes, basic inheritance, inlining, default function arguments, and strong type
checking in addition to all the features of the C language. The first C with Classes compiler was called
Cfront, which was derived from a C compiler called CPre. It was a program designed to translate C with
Classes code to ordinary C
In 1983, the name of the language was changed from C with Classes to C++. The ++ operator in the C
language is an operator for incrementing a variable, which gives some insight into how Stroustrup regarded
the language. Many new features were added around this time, the most notable of which are virtual
functions, function overloading, references with the & symbol, the const keyword, and single-line comments
using two forward slashes.
In 1985, C++ was implemented as a commercial product. The language was not officially standardized
yet. The language was updated again in 1989 to include protected and static members, as well as an
inheritance from several classes.
In 1990, Turbo C++ was released as a commercial product. Turbo C++ added a lot of additional libraries
which have had a considerable impact on C++'s development.
In 1998, the C++ standards committee published the first international standard for C++ ISO/IEC
14882:1998, which is informally known as C++98. The Standard Template Library, which began its
conceptual development in 1979, was also included.
In 2003, the committee responded to multiple problems that were reported with their 1998 standard and
revised it accordingly. The changed language was named C++03
In mid-2011, the new C++ standard (C++11) was finished. The new features included Regex support, a
randomization library, a new C++ time library, atomics support, a standard threading library, a new for loop
syntax providing functionality similar to for each loops in certain other languages, the auto keyword, new
container classes, better support for unions and array-initialization lists and variadic templates.
Class: A Class is a user defined data-type which has data members and member functions. Data
members are the data variables and member functions are the functions used to manipulate these
Object: An Object is an identifiable entity with some characteristics (Properties) and behavior
(Method). An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated.
Example:
Abstraction: Data abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and hiding the
details. Data abstraction refers to providing only essential information about the data to the outside
world, hiding the background details or implementation. Abstraction using Classes, Abstraction in
Header files
Inheritance: The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object-Oriented
Programming. Sub Class, Super Class, Reusability.
Polymorphism: The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. Operator
Overloading: Function Overloading
Example:
Message/Method: A request for an object to perform one of its operations is called message
operation method/function. All communication between object is done via message is called message
passing
OBJECT-ORIENTED PROGRAMMING
Object orientation, as mentioned earlier, is an outcome of the research to find the solution to programming
large systems. There are many issues related to object orientation as it has become a discipline of its own
today.
Object-based programming: Object-based programming uses objects and classes but without inheritance.
It provides flexibility to the program and is easier to work with. Operator overloading, function overloading,
and the use of constructors and destructors are included in object-based programming.
Object-oriented programming: Object-oriented programming uses classes and objects with inheritance. It
can have classes inherited from other classes and have complex means for calling and using functions of
both the classes. Base (or parent) classes (from which we derive or inherit another class) and inherited or
derived (from base classes) classes are the two categories of classes.
Example: Suppose we are given a job to write a program about maintaining the roll call of a college using a
C++ program. The roll call is usually done by calling the student’s roll number or name one by one and
making an entry in the register, by using a special sheet where in every student present in the class should
sign, or through a card-based system, which automatically registers a student when he/she approaches the
classroom. This register, sheet, or computer file is then used to provide input to the program. The program
should then be able to generate reports such as the individual attendance of a student and the attendance for a
particular subject.
To program for the roll call process, we have to decide the entities involved in that process. The first
observation yields some entities that are listed here. This list is based on the author’s observation. Readers
may come up with lists with a few differences. There are no clear-cut guidelines for choosing the entities; it
is more a task involving intuition. The author’s list of entities involved in the roll call program, as shown in
Fig. 1.1 is as follows:
1. The teacher who is responsible for taking the roll call
2. The subject for which the roll call is taken
Figure 1.2(a) shows an example of a message being asked and replies being sought from an entity. The
question or the query is normally referred to as a request and the reply is termed as the response. a teacher
asking how many students were present on a particular day in the class and the roll call entity answering that
question. We may need to find out how many such questions are possible to be asked and how the answers
to these questions are sought out. This is the next assignment when we are developing our system. The
method to generate the answer of a message must be provided to the receiving entity; otherwise, the
receiving entity cannot answer that question. In the case of the question regarding the number of students
present, the roll call entity must have a method to calculate the total students present from the data it has.
This method is called or invoked by the receiving entity when the other entity asks for that specific
information.
1.5 What is an object, Classes
A class is an extension to the C struct. The struct Student in C will contain only the attributes of the student
such as name and address.
These ADTs are defined in two ways, one with initialization and the other without. For example, Point P
(2,3) not only defines a point P but also defines that the x and y coordinates of the point P are 2 and 3. On
the other hand, point S is defined without any mention of the coordinates.
Quite a few useful class definitions fall under the category of ADT, including stack, queue, string,
vector, and dequeue. We will learn about complex, point, circle, and rectangle while we discuss operator
overloading.
Teacher SomeTeacher;
SomeTeacher = RollCall16030903.getTeacher ( );
SomeTeacher = RollCall16030903.getTeacher ( );
In the entire discussion so far, the getTeacher () function is known as a message being passed to an object
RollCall16030903. The object takes some action (executes the body of the function specified, the getTeacher
() function in our example) and returns with the response show in figure 1.5
Fig 1.7: Abstraction implemented as combination of exposed (public) and hidden (private) parts
Encapsulation:
The process of designing a class with some attributes hidden and some exposed is known as
encapsulation. The entire class is insulated from the outside world. Only the public (or exposed) attributes
can be manipulated by the user of that object. Thus, we can have controlled access to the class attributes
unlike struct in C. If we define a struct Student with attributes such as name, address, and roll number, then
all the attributes are available for manipulation. However, in C++, it is possible to define a class Student
where we cannot directly manipulate the name, address, and roll number, but we can only set their values
using a function InsertDetails ( ) and print the information using PrintDetails ( ). Objects have two different
types of attributes.
A programmer may define a class as follows, separating the private and public members:
class Dummy
{
private:
...
public:
...
}
1.8 Inheritance
The idea of extending an already defined class is known as inheritance. if we are writing a program to
maintain the object MCAStudent and we have a built-in class Student, which has almost all the details
needed for our program, except a few, which need to be added manually. The additional information may be
anything that an MCA student possesses in addition to the normal student.
Let us now look at inheritance from the point of view of object orientation. The world is full of cases
where one class is a specialization of another. Human beings are a specialization of mammals; dogs are a
specialization of animals; Indians are a specialization of people. This relationship between two different
classes is significant in more than one way.
There can be quite a large number of relationships possible between any two given entities. Inheritance
(or specialization) is one such relationship, where one class inherits all the attributes of some other class
1.10 Polymorphism
In general, polymorphism is the ability of a single object to appear in many forms. Polymorphism is
related to something that behaves differently under different circumstances. One may be a student in a
The + operator in these three cases behaves differently. In the first case, it adds two integers; in the
second case, it adds two double values; and in the third case, it concatenates two different strings into one.
The string is available in C++ as a data type. + is overloaded in the string class. So, the given operation
makes sense. When the operator behaves differently for different types of operands, it is also a type of
ad-hoc polymorphism. In C++ parlance, this is called operator overloading.
In C++, both function and operator overloading are resolved at compile time. The compiler, while
compiling the program, decides the operation to be performed and generates the object code that describes
that operation. In the given example, when 12 + 25 is seen in the source code, the compiler provides the code
that adds two integer values and adds that to the object code being generated and when ‘Indian’ + ‘Cricketer’
is seen, it adds the string concatenation routine at that place.
1.10.2 Dynamic Polymorphism at Run-time
This is a case when the resolution of the function happens at run-time. The restriction here is to choose the
function only from the hierarchy of classes and not from elsewhere.