General
Concept of OOP
8.1
Introduction
The object-oriented programming
(OOP) is a different approach to programming.
Object oriented technology
supported by C++ is considered the latest technology
in software development. It is
regarded as the ultimate paradigm for the modelling
of information, be that data or
logic.
8.2
Objectives
After going through this lesson,
you would be able to:
l
learn the basic
concepts used in OOP
l
describe the
various benefits provided by OOP
l
explain the
programming applications of OOP.
8.3
Object-Oriented Programming
The object-oriented programming
is a different approach to programming. It has
been created with a view to
increase programmer's productivity by overcoming the
weaknesses found in procedural
programming approach. Over the years many
object-oriented programming
languages such as C++ and small talk have come up
and are becoming quite popular
in the market. The major need for developing such
languages
was to manage the ever-increasing size and complexity of programs.
Application of oops
1. Real time system
2. Simulation & modeling
3. Object orated data base
4. Hyper text, hypermedia and
expert text
5. Artificial inlet base and
expert system
6. Work and palatal
programming
7. Decision support and
office automation system
Benefits
of OOP
OOP provides lot of benefits to
both the program designer and the user. Objectoriented
approach helps in solving many
problems related to software development
and quality of software product.
The new technology assures greater programmer
productivity, better quality of
software and lesser maintenance cost. The major
benefits are :
l
Software complexity
can be easily managed
l
Object-oriented
systems can be easily upgraded
l
It is quite easy to
partition the work in a project based on objects.
8.6
Programming Applications of OOP
OOP has become one of the
programming buzzwords today. There appears to be
a great deal of excitement and
interest among software programmers in using OOP.
Applications of OOP are gaining
importance in many areas. OOP has been
extensively used in the
development of windows and word based systems such as
MS-Windows, x-Windows etc. The
promising application areas of OOP are:
(i) Multiple data structure:
This is an application where the same data
structure is used many times.
For example a window data structure is
used multiple-times in a
windowing system.
(ii) Data in multiple programs:
This is an application where the same
operations are performed on a
data structure in different programs. For
example, record validation in an
accounting system.
The other application areas of
OOP are parallel programming, simulation and
modeling,
AI and Expert systems, Neural Networks and CAD systems.
I/O in C++
The identify fire cin is
predefined object in c++ that cross pounds to standard input stream. hear is
stream represent that key words. The operator >> is none as extraction or
get operator its extract the value from keyword and assign it to the ruble on
it right
•
Since cin and
cout are C++ objects, they are somewhat "intelligent":
– They do not require the usual format strings and
conversion specifications.
– They do automatically know what data types are
involved.
– They do not need the address operator, &.
– They do require the use of the stream extraction (
>> ) and insertion ( << ) operators.
•
The next slide
shows an example of the use of cin and cout.
Structure of program-
1
include file
2
class declaration
3
member function definition
4
main function program
This
approach is based on the concept of as show to you
Member function
|
Class function
|
Main function
|
Relational Operators
C++
provides six relational operators for comparing numeric quantities. These are
summarized
in Table 2.3. Relational operators evaluate to 1 (representing the true
outcome)
or 0 (representing the false outcome).
Table 2.3 Relational operators.
Operator Name Example
== Equality 5 == 5 // gives 1
!= Inequality 5 != 5 // gives 0
< Less Than 5 < 5.5 // gives 1
<= Less Than or Equal 5 <= 5 // gives 1
> Greater Than 5 > 5.5 // gives 0
>= Greater Than or Equal 6.3 >= 5 // gives 1
Note
that the <= and
>= operators
are only supported in the form shown. In
particular,
=< and => are both
invalid and do not mean anything.
The
operands of a relational operator must evaluate to a number. Characters
are
valid operands since they are represented by numeric values. For example
(assuming
ASCII coding):
'A'
< 'F' // gives 1 (is like 65 < 70)
Memory management operator= it’s
a pointer *
Manipulators
Manipulators
are operators that are uses format the data display the most commonly
manipulator are
1
endl
2
setw
Its
uses to break to line
Function prototype
The
giving details such as the number and types of argument and types of return
value
Call by reference
The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument.
To pass the value by reference, argument pointers are passed to the functions just like any other value. So accordingly you need to declare the function parameters as pointer types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.// function definition to swap the values. void swap(int *x, int *y) { int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
return;
} |
Inline function
Inline function header
{
Function body
}
Example
Inline double cube(double a)
{
return(a*a*a);
return(a*a*a);
}
The above in the function can we involved by statement line
Const argument
Int strlen(const char *p);
Int length(const string &s);
Function overloading
//Declaration
Int add(int a ,int b);
Int add(int a , int b, int c);
Int add(double x, double y);
Int add(int p, double q);
//function call
Cout<<add(5,10);
Cout<<add(15,10,5);
Cout<<add(12,75,8.92);
Cout<<add(15,22,95);
A function call first matches the prototype having the some
no and type of argument then call the function
Class
A class
definition consists of two parts: header and body. The class header
specifies
the class name and its base classes. The class body defines
the class members. Two types of members are supported:
1 class declaration
2 class function definition
· Data
members have the syntax of variable definitions and specify the
representation
of class objects.
· Member
functions have the syntax of function prototypes and specify the
class
operations, also called the class interface.
Class
members fall under one of three different access permission categories:
· Public
members are accessible by all class users.
· Private
members are only accessible by the class members.
· Protected
members are only accessible by the class members and the
members
of a derived class.
The
data type defined by a class is used in exactly the same way as a built-in type
class Point {
int xVal, yVal;
public:
void SetPt (int, int);
void OffsetPt (int, int);
};
Friend function
A friend function is used for accessing the non-public members
of a class. A class can allow non-member functions and other classes to access
its own private data, by making them friends. Thus, a friend function is an
ordinary function or a member of another class.
Class por
{
Private
1…..
2…..
3…..
Public:
1
2
3
Friend void xyz(void);
};
Copy constructor
The copy constructor lets you
create a new object from an existing one by initialization. A copy constructor
of a class A is a non-template
constructor in which the first parameter is of type A&, const A&,
volatile A&, or const volatile A&, and the rest of its
parameters (if there are any) have default values.
int n=10;
copy x(n);
copy ad=x;
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Copy with data
members and member functions.
STEP 3: The constructor Copy() with argument to assign
the value.
STEP 4: To cal the function calculate() do the
following steps.
STEP 5: For i=1 to var do
STEP 6: Calculate fact*i to assign to fact.
STEP 7: Increment the value as 1.
STEP 8: Return the value fact.
STEP 9: Print the result.
STEP 10: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
for(int i=1;i<=var;i++)
{
fact = fact * i;
}
return fact;
}
};
void main()
{
clrscr();
int
n;
cout<<"\n\tEnter the Number : ";
cin>>n;
copy
obj(n);
copy
cpy=obj;
cout<<"\n\t"<<n<<" Factorial
is:"<<obj.calculate();
cout<<"\n\t"<<n<<" Factorial
is:"<<cpy.calculate();
getch();
Output:
Enter the Number: 5
Factorial is: 120
Factorial is: 120
Operator Overloading
#include<iostream.h>
#include<conio.h>
class
space
{
int
x;
public:
void
data(int a);
void
operator-();
};
void
space::data(int a)
{
x=a;
cout<<"number
is="<<x<<endl;
}
void
space::operator-()
{
x=-x;
cout<<x<<endl;
}
void
main()
{
space
s;
s.data(10);
-s;
s.data(10);
getch();
}
Virtual class
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class abc
{
protected:
int nu;
public:
void add(int a, int b)
{
int c;
c=a+b;
cout<<"yout sum="<<c<<endl;
}
};
class mno:virtual public abc
{
protected:
int x;
public:
void sub(int a,int b)
{
int c=a-b;
cout<<"your substract"<<c<<endl;
}
};
class xyz:public virtual abc
{
protected:
int c;
public:
void mul(int a, int b)
{
int c;
c=a*b;
cout<<"your multiplay is
"<<c<<endl;
}
};
class mix:public mno,public xyz
{
};
void main()
{
mix a;
a.add(10,2);
a.sub(10,2);
a.mul(10,2);
getch();
}