Module 1
Module 1
In the procedure oriented approach, the problem is viewed as sequence of things to be done such as
reading , calculation and printing.
Procedure oriented programming basically consist of writing a list of instruction or actions for the
computer to follow and organizing these instruction into groups known as functions.
3. No data hiding
“Object oriented programming as an approach that provides a way of modularizing programs by creating
partitioned memory area for both data and functions that can be used as templates for creating copies of
such modules on demand”.
3. Data structures are designed such that they characterize the objects.
4. Functions that operate on the data of an object are tied together in the data structure.
1. Objects
2. Classes
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
OBJECTS
Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place,
a bank account or any item that the program must handle.
CLASS:
A class is a blueprint or template for creating objects. It defines the properties (data members) and
behaviors (member functions) that the objects of the class will have.
Or
In C ++ a class is a new data type that contains member variables and member functions that operate on
the variables.
Eg: class ClassName
{ public:
int data;
void display()
// Function body }
};
Object
An object is an instance of a class. It is created using the class definition and can access the
class's members (data and functions).
Syntax:
#include <iostream>
// Define a class
class Car {
public:
int speed;
// Member function
void display() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
};
int main() {
car1.speed = 120;
return 0;
DATA ABSTRACTION :
Abstraction refers to the act of representing essential features without including the back ground details
or explanations. Classes use the concept of abstraction and are defined as size, width 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.
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 reusablity. This mean that we can add additional features to an
existing class with out modifying it. This is possible by desining a new class will have the combined
features of both the classes
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.
It is able to express the operation of addition by a single operater say ‘+’. When this is possible you use
the expression x + y to denote the sum of x and y, for many different types of x and y; integers , float and
complex no. You can even define the + operation for two strings to mean the concatenation of the strings.
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 untill 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.
BENEFITS OF OOP:
Oop offers several benefits to both the program designer and the user. Object-oriented contributes to the
solution of many problems associated with the development and quality of software products. The
principal advantages are :
1. Through inheritance we can eliminate redundant code and extend the use of existing classes.
2. 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.
3. This principle of data hiding helps the programmer to build secure programs that can’t be invaded by
code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist with out any interference.
7. Message passing techniques for communication between objects makes the interface description with
external systems much simpler.
APPLICATION OF OOP:
The most popular application of oops up to now, has been in the area of user interface design such as
windows. There are hundreds of windowing systems developed using oop techniques.
Real business systems are often much more complex and contain many more objects with complicated
attributes and methods. Oop is useful in this type of applications because it can simplify a complex
problem. The promising areas for application of oop includes.
Supports
Procedural Programming
5 Paradigm both procedural and object-oriented
Language
programming
Programming
7 Top-Down Approach Bottom-Up Approach
Approach
Information
9 Not supported Supported via encapsulation.
Hiding
Reference
14 Not supported Supported
Variables
Virtual and
15 Not supported Supported
friend functions
Exception
16 Not supported Supported
Handling
Basics of C++
C++ Comments:
C++ introduces a new comment symbol //(double slash). Comments start with a double slash symbol and
terminate at the end of line. A comment may start any where in the line and what ever follows till the end
of line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multi line comments can be written as
follows:
// this is an example of
// c++ program
// thank you
The c comment symbols /* ….*/ are still valid and more suitable for multi line comments.
Output Operator:
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.
Return Statement:
In C++ main ( ) returns an integer type value to the operating system. Therefore every main ( ) in C++
should end with a return (0) statement, otherwise a warning or an error might occur.
Input Operator:
The statement
cin>> number 1;
is an input statement and causes. The program to wait for the user to type in a number. The number keyed
in is placed in the variable number1. The identifier cin is a predefined object in C++ that corresponds to
the standard input stream. Here this stream represents the key board.
The operator >> is known as get from operator. It extracts value from the keyboard and assigns it to the
variable on its right
cout<<”sum=”<<sum<<”\n”;
cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;
cin>>number1>>number2;
Structure Of A Program :
#include <iostream>
int main ()
return 0;
Output:-Hello World!.
This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not
have any effect on the behavior of the program. The programmer can use them to include short
explanations or observations within the source code itself. In this case, the line is a brief description of
what our program is.
#include <iostream>
Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines
with expressions but indications for the compiler's preprocessor. In this case the directive
#include<iostream> tells the preprocessor to include the iostream standard file. This specific file
(iostream) includes the declarations of the basic standard input-output library in C++, and it is included
because its functionality is going to be used later in the program.
int main ()
The main function is the point by where all C++ programs start their execution, independently of its
location within the source code. It does not matter whether there are other functions with other names
defined before or after it – the instructions contained within this function's definition will always be the
first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs
have a main function.
cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a
sequence of characters (in this case the Hello World sequence of characters) into the standard output
stream (which usually is the screen).
return 0;
The return statement causes the main function to finish. return may be followed by a return code (in our
example is followed by the return code 0). A return code of 0 for the main function is generally
interpreted as the program worked as expected without any errors during its execution. This is the most
usual way to end a C++ console program.
Include files
Class declaration
Example :-
# include<iostream.h>
class person
char name[30];
int age;
public:
void getdata()
cout<<”enter name”;
cin>>name;
cout<<”enter age”;
cin>>age;
void display()
cout<<”\n name:”<<name;
cout<<”\n age:”<<age;
};
int main( )
person p;
p.getdata();
p.display();
return 0;
Additional information
CLASS:-
Class is a group of objects that share common properties and relationships .In C++, a class is a new data
type that contains member variables and member functions that operates on the variables. A class is
defined with the keyword class. It allows the data to be hidden, if necessary from external use. When we
defining a class, we are creating a new abstract data type that can be treated like any other built in data
type.
a) Class declaration
the class declaration describes the type and scope of its members. The class function definition describes
how the class functions are implemented.
Syntax:-
class class-name
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only
from with in the class. On the other hand , public members can be accessed from outside the class also.
The data hiding is the key feature of oops. The use of keywords private is optional by default, the
members of a class are private.
The variables declared inside the class are known as data members and the functions are known as
members mid the functions. Only the member functions can have access to the private data members and
private functions. However, the public members can be accessed from the outside the class. The binding
of data and functions together into a single class type variable is referred to as encapsulation.
Syntax:-
class item
int member;
float cost;
public:
The class item contains two data members and two function members, the data members are private by
default while both the functions are public by declaration. The function getdata() can be used to assign
values to the member variables member and cost, and putdata() for displaying their values . These
functions provide the only access to the data members from outside the class.
CREATING OBJECTS:
Once a class has been declared we can create variables of that type by using the class name.
Example:
item x;
creates a variables x of type item. In C++, the class variables are known as objects. Therefore x is called
an object of type item.
x, y ,z also possible.
class item
-----------
-----------
-----------
}x ,y ,z;
The private data of a class can be accessed only through the member functions of that class. The main()
cannot contains statements that the access number and cost directly.
Syntax:
Example:- x. getdata(100,75.5);
It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function .
x. number = 100 is illegal .Although x is an object of the type item to which number belongs , the number
can be accessed only through a member function and not by the object directly.
Example:
class xyz
Int x;
Int y;
public:
int z;
};
---------
----------
xyz p;
p, z=10; ok ,z is public
function is that a member function incorporates a membership.Identify label in the header. The ‘label’
tells the compiler which class the function belongs to.
Syntax:
function-body
The member ship label class-name :: tells the compiler that the function function -name belongs to the
class class-name . That is the scope of the function is restricted to the class-name specified in the header
line. The :: symbol is called scope resolution operator.
Example:
number=a;
cost=b;
cout<<”number=:”<<number<<endl; cout<<”cost=”<<cost<<endl;
The member function have some special characteristics that are often used in the program development.
• Several different classes can use the same function name. The "membership label" will resolve their
scope, member functions can access the private data of the class .A non member function can't do so.
• A member function can call another member function directly, without using the dot operator.
Write a simple program using class in C++ to input subject mark and prints it.
class marks
private :
int ml,m2;
public:
void getdata()
cin>>ml;
cin>>m2;
void displaydata()
};
Int main()
marks x;
x.getdata();
x.displaydata();
return 0;