Oop Notes Mod 1
Oop Notes Mod 1
Procedure oriented programming basically consists of writing a list of instructions for the
computer to follow, and organizing these instructions into groups known as functions. We
normally use flowcharts to organize these actions and represent the flow of control from one
action to another.
In a multi-function program, many important data items are placed as global so that they may
be accessed by all the functions. Each function may have its own local data. Global data are
more vulnerable to an inadvertent change by a function. In a large program it is very difficult
to identify what data is used by which function. In case we need to revise an external data
structure, we also need to revise all functions that access the data. This provides an
opportunity for bugs to creep in.
Another serious drawback with the procedural approach is that we do not model real world
problems very well. This is because functions are action-oriented and do not really
corresponding to the element of the problem.
The data of an object can be accessed only by the function associated with that object.
However, function of one object can access the function of other objects.
Object-oriented programming is the most recent concept among programming paradigms and
still means different things to different people.
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
1.3.1 Objects
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 has to handle.
Programming problem is analyzed in term of objects and the nature of communication
between them. Program objects should be chosen such that they match closely with the real-
world objects. Objects take up space in the memory.
When a program is executed, the objects interact by sending messages to one another. For
example, if “customer” and “account” are to object in a program, then the customer object
may send a message to the count object requesting for the bank balance. Each object contain
data, and code to manipulate data.
Each object is associated with the data of type class with which they are created. A class is
thus a collection of objects similar types. For examples, Mango, Apple and orange members
of class fruit.. The syntax used to create an object is not different then the syntax used to
create an integer object in C. If fruit has been defines as a class, then the statement
Fruit Mango;
Will create an object mango belonging to the class fruit.
Abstraction refers to the act of representing essential features without including the
background details or explanation. Classes use the concept of abstraction and are defined as a
list of abstract attributes such as size, wait, and cost, and function operate on these attributes.
They encapsulate all the essential properties of the object that are to be created. Like you
don’t need to understand working of a car to drive.
The attributes are sometime called data members because they hold information. The
functions that operate on these data are sometimes called methods or member function.
1.3.4 Inheritance
Inheritance is the process by which objects of one class acquired the properties of objects of
another classes. For example, the bird, ‘robin’ is a part of class ‘flying bird’ which is again a
part of the class ‘bird’. The principal behind this sort of division is that each derived class
shares common characteristics with the class from which it is derived as illustrated in
figure1.3.4
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 feature of both the
classes. The real appeal and power of the Allows the programmer to reuse a class i.e almost,
but not exactly, what he wants, and to tailor the class in such a way that it does not introduced
any undesirable side-effects into the rest of classes.
Using a single function name to perform different type of task is known as function
overloading.
Objects communicate 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 (procedure) in the receiving
object that generates the desired results. Message passing involves specifying the name of
object, the name of the function(message) and the information to be sent.
Example: Object has a life cycle. They can be created and destroyed. Communication with an
object is feasible as long as it is alive.
• Through inheritance, we can eliminate redundant code extend the use of existing Classes.
• We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
• The principle of data hiding helps the programmer to build secure program that can not be
invaded by code in other parts of a programs.
• It is possible to have multiple instances of an object to co-exist without any interference.
• It is possible to map object in the problem domain to those in the program.
• It is easy to partition the work in a project based on objects.
• The data-centered design approach enables us to capture more detail of a model can
implemental form.
• Object-oriented system can be easily upgraded from small to large system.
• Message passing techniques for communication between objects makes to interface
descriptions with external systems much simpler.
• Software complexity can be easily managed.
Real-business system are often much more complex and contain many more objects
with complicated attributes and method. OOP is useful in these types of application
because it can simplify a complex problem. The promising areas of application of OOP
include:
• The promising areas includes the followings, Real Time Systems Design Simulation and
Modeling System
• Object Oriented Database
• Client-Server System
• Hypertext, Hypermedia
• Neural Networking and Parallel Programming
• Decision Support and Office Automation Systems
• CIM/CAD/CAM Systems
• AI and Expert Systems
C+ + is a superset of C. Almost all c programs are also C++ programs. However, there
are a few minor differences that will prevent a c program to run under C++ complier. We
shall see these differences later as and when they are encountered.
The most important facilities that C++ adds on to C care classes, inheritance, function
overloading and operator overloading. These features enable creating of abstract data
types, inherit properties from existing data types and support polymorphism, thereby
making C++ a truly object-oriented language.
Next section will have definitions of member functions of classes. Next section will have
main function which joins together all these sections.Last section will have function
definitions. These sections can be placed in different files or in the same file.
Include Files
Class declaration &Function prototypes
Member functions definitions
Main function program
Other function definitions
Figure 1.7 Structure of a C++ program
This approach is based on the concept of client-server model, where client asks for the
service of the server like your browser and a website, as shown in figure below. The class
definition including the member functions constitute the server that provides services to the
main program known as client. The client uses the server through the public interface of the
class.
#include<iostream>
Using name space std;
int main()
{
cout<< “C ++ is the best”;
return 0;
}
Output
C++ is the best
This simple program demonstrates several C++ features.
If you want to write comments that have more than one line use /*,*/:
/*This is an example
of C++ program to illustrate
some of its features */
Causes the string in quotation marks to be displayed on the screen. This statement introduces
two new C++ features, cout and <<. The identifier cout(pronounced as C out) is a predefined
object that represents the standard output stream(monitor) in C++. Here, the standard output
stream represents the screen. It is also possible to redirect the output to other output devices.
The operator << is called the insertion or put to operator.
#include<iostream>
The #include directive instructs the compiler(the software which converts your program into
an exe and also checks for any mistakes or errors in your code) to include the contents of the
file enclosed within angular brackets into the source file. The header file iostream should be
included at the beginning of all programs that use input/output statements that is to print or
read from keyboard.
1.8.5 Namespace
Namespace is a new concept introduced by the ANSI C++ standards committee. This defines
a scope for the identifiers that are used in a program. For using the identifier defined in the
namespace scope we must include the using directive, like
Using namespace std;
Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI
C++ programs must include this directive. This will bring all the identifiers defined in std to
the current global scope. Using and namespace are the new keyword of C++.
protected:
Variable declaration;
Function declaration;
public variable declaration;
Function declaration;
};
Here, the keyword class specifies that we are using a new data type and is followed by the
class name.
(i) private
(ii) public
In C++, the keywords private and public are called access specifiers. The data hiding concept
in C++ is achieved by using the keyword private.
Private data and functions can only be accessed from within the class itself.
Public data and functions are accessible outside the class also. This is shown below :
Private Can only be accessed from within the class data members and member functions Data
hiding not mean the security technique used for protecting computer databases. The security
measure is used to protect unauthorized users from performing any operation (read/write or
modify) on the data.
The data declared under Private section are hidden and safe from accidental manipulation.
Though the user can use the private data but not by accident. The functions that operate on
the data are generally public so that they can be accessed from outside the class but this is not
a rule that we must follow.
Data Members
Data members are variables that you create in a normal program. Here while we define
members we also specify its protection level(public, private). Unlike a normal variable data
members are usually accessed within a class
Member Function
We have already seen the class definition syntax as well as an example. In C++, the member
functions can
be coded in two ways :
(a) Inside class definition
(b) Outside class definition using scope resolution operator (::)
The code of the function is same in both the cases, but the function header is different as
explained below :
The syntax for a member function definition outside the class definition is :
Here the operator::known as scope resolution operator helps in defining the member function
outside the class.
will create two objects ob1 and ob2 of largest class type. As mentioned earlier, in C++ the
variables of a class are known as objects. These are declared like a simple variable i.e., like
fundamental data types.
In C++, all the member functions of a class are created and stored when the class is defined
and this memory space can be accessed by all the objects related to that class. Memory space
is allocated separately to each object for their data members. Member variables store different
values for different objects of a class.
Object_name.function_name (arguments);
Example program
#include<iostream>
using namespace std;
class complex
{
int real;
int imag;
public:
complex()
{
real=0;
imag=0;
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
};
int main()
{
complex cnum;
cnum.display();
return 0;
}
Program 1.10 : Example for accessing member function and Default Constructor
1.11 Constructor
A constructor is a special member function whose task is to initialize the object of a class. Its
name is same as the class name. A constructor does not have a return value. A constructor is
called or invoked when the object of its associated class is created. It is called constructor
because it constructs the values of data members of the class
Types of constructors:
(i) Default Constructor
(ii) Parameterized Constructor
(iii) Copy Constructor
Program 1.10 is an example for the default constructor . In the above program class name and
member function name is same ie complex.
#include<iostream>
using namespace std;
class complex
{
int real;
int imag;
public:
complex(int r, int i)
{
real=r;
imag=i;
}
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
};
int main()
{
complex cnum(10,5);
cnum.display();
return 0;
}
Program 1.11 : Example for accessing member function and Default Constructor
A copy constructor is a like a normal parameterized Constructor, but which parameter is the
same class object. Copy constructor uses to initialize an object using another object of the
same class.
1.12 Destructor
Destructors are the functions that are complimentary to constructors. These are
used to deinitialize objects when they are destroyed. A destructor never takes
any argument nor does it return any value. A destructor is called when an object
of the class goes out of scope, or when the memory space used by it is de
allocated with the help of delete operator. Like constructor, the destructor is a
member function whose name is the same as the class name but is preceded by a
tilde.
~ integer ( ) { }
Sample Questions
Explain basic structure of a C++ program with help of a simple program to display your
name in first line and roll no in second line
i. cout <<”x=” x;
If you are a software programmer , which high level language prefer for design a software
for your client ? why?
What are the procedure to call a member function from main function?. Discuss with an
example
What are the two type of polymorphism explain with simple example
5. Write a C++ to print and find the sum of first 20 prime numbers.
6. Write a program to find square and cube of a number