Firstly, Functions Have Unrestricted Access To Global Data. This Causes A Program's Structure
Firstly, Functions Have Unrestricted Access To Global Data. This Causes A Program's Structure
Introduction:
Firstly, functions have unrestricted access to global data. This causes a program’s structure
difficult to conceptualize. In addition, it makes the program difficult to modify. Any change
made in a global data item may necessitate rewriting all the functions that access that item.
Secondly, the arrangement of separate data and functions does a poor job of modeling things in
the real world. In the real world we deal with objects such as people and cars. Such objects
aren’t like data and they aren’t like functions. Complex real-world objects have both data and
functions. Therefore there was a need for a new programming approach which is called Object-
Oriented Programming.
There are some points that make programming to use OOP program:
1. OOP incorporates the best features of structured programming with several powerful new
concepts.
2. It provides a new way of thinking, organizing and developing programs.
3. OOP allows decomposition of a problem into a number of entities called objects and then
builds data and functions around these objects.
4. OOP treats data as a critical element in the program development and does not allow it to
flow freely around the system. The data of an object can be accessed only by the functions
associated with that object. However, functions of one object can access the functions of
other objects.
1
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
1. Class: is like a blueprint (general form) used to create objects. A class also is an abstract
data type that can be treated like any other built-in data type. A class defines the attributes
of its objects and the methods that can be applied to its objects.
2. Object: is the basic run-time entity in an object-oriented system. It may represent a
person, a place, a bank account, etc. An object is also called an instance of a class.
3. Encapsulation: is the mechanism that binds together data and functions into a single unit
(called class). The data is not accessible to the outside world, and only the functions
which are wrapped in the class can access it. This insulation of data from direct access is
called data hiding or information hiding.
4. Inheritance: is the process by which objects of one class acquire the properties of objects
of another class. It supports the concept of hierarchical classification. It also provides the
idea of reusability. This means that we can add additional features to an existing class
without modifying it.
5. Polymorphism: is the ability for objects of different classes related by inheritance to
respond differently to the same message. The same message sent to many different types
of objects takes on “many forms”--hence the term polymorphism.
2
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Classes:
The keyword class indicates that an abstract data type called class_name will be
specified.
The class body is enclosed within braces and terminated by a semicolon.
The variables declared inside the class are known as data members and the functions
are known as member functions.
These functions and variables are usually grouped under two sections: namely,
private and public.
Only the member functions can have access to the private data members and private
functions. However, the public members can be accessed from outside the class.
Note that declaring data variables as public defeat the idea of data hiding and therefore
should be avoided.
3
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Example:
Here, we have defined a class called item. This name is used to declare objects of that class.
The data members (number and cost) are declared as private, while the member functions
(getdata() and putdata()) are declared as public. As mentioned earlier, these two functions
provide the only access to the two data members from outside the class. Note that, at this stage
class definition does not allocate any memory space.
Objects:
Once a class has been defined, we can create objects of that type as follows:
item x ;
This statement creates an object x of type item. At this stage, memory space is allocated to the
object x. We may also declare more than one object in one statement, for example
item x, y, z ;
Another way to create objects is by placing their names immediately after the closing brace in
the class definition, for example:
4
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
As previously mentioned, the public member functions can be accessed only from outside
the class. This can be done using the dot operator, for example
x.getdata(100, 75.5);
This function call statement is valid and assigns the value 100 to number and 75.5 to cost of the
object x by implementing the getdata() function. Similarly, the statement
x.putdata();
would display the values of number and cost. While the statement
x.number =100;
is illegal because number is a private member and cannot be accessed from outside the class.
Example:
5
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
However, the public declaration of data conflicts with the OOP concept data hiding and
therefore should be avoided.
There are two ways to define the member functions, and there are;
Here, the membership label item :: tells the compiler that the function getdata() belongs
to the class item. That is, the scope of the function getdata() is restricted to the class item
specified in the header line. The symbol :: is called the scope resolution operator.
Note the statements number = a ; cost = b ; show that the member functions
can have direct access to private data items. Similarly, the function putdata() is defined
outside the class item as follows:
6
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Note: Normally, only small functions are defined inside the class definition.
#include <iostream>
using namespace std;
class item
{
private:
int number; float cost;
public:
void getdata(int a, float b);
void putdata()
{
cout << “number :” << number << “\n”;
cout << “cost :” << cost << “\n”;
}
};
7
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
void main()
{
item x; //create object x
cout << “\nObject x “ << “\n;
x.getdata(100, 299.95);
x.putdata();
item y; //create another object y
cout << “\nObject y “ << “\n;
x.getdata(200, 175. 50);
x.putdata();
}
Object x
number :100
cost :299.95
Object y
number :200
cost :175.5
8
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
We have shown that a class member function can be called only by an object of that class
using a dot operator. However, a member function can be called inside another member function
of the same class. This is called nesting of member functions.
Example:
#include <iostream>
using namespace std;
class set
{
private:
int m, n;
public:
void input();
void display();
int largest();
};
9
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
void main()
{
set A;
A.input();
A.display();
}
Largest value = 25
10
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Example:
The function call statement s1.read();is illegal. Instead, the function read() can be called by
the function update() to update the value of m.
void sample :: update()
{
read(); // simple call; no object used
}
11
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Constructors
A constructor is a member function whose name is the same as the class name. The
constructor is used to initialize the objects of its class, i.e. it constructs the values of data
members of the class. The constructor is automatically invoked whenever object of its
associated class is created.
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class Integer
{
private:
int m,n;
public:
Integer();
void indata(int,int);
void outdata();
};
Integer::Integer()
{
m=1;n=1;
}
void Integer::outdata()
{
cout<<"\nM="<<m<<endl;
cout<<"N="<<n<<endl;;
}
12
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
void main()
{
Integer C;
C.outdata();
C.indata(5,8);
C.outdata();
getch();
}
When we create an object (C) at the declaration (Integer C;) not only create an object (C)
but also automatically initializes its data member (m, n) by (1,1).
The output of the above program is:
M=1
N=1
M=5
N=8
Notes:
The constructor should be declared in the public section.
The constructor does not have return type, (not even void), and therefore it cannot return
any value.
There is no need to write any statement to invoke the constructor function because it is
invoked automatically when the object is created.
A constructor that accepts no arguments is called the default constructor.
If no constructor is defined, then the compiler creates an implicit constructor.
The constructor can take arguments like other C++ functions. This is called parameterized
constructor.
13
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Parameterized Constructors
Sometimes we need to initialize the data elements of different objects with different
values when they are created. This can be done by passing arguments to the constructor function
when objects are created. Such constructor is called parameterized constructor.
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class Integer
{
private:
int m;
int n;
public:
Integer(int, int);
void indata(int, int);
void outdata();
};
Integer::Integer(int A, int B)
{
m=A;
n=B;
}
void Integer::outdata()
{
cout<<"\nM="<<m<<endl;
cout<<"N="<<n<<endl;
}
14
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
void main()
{
Integer C(1,5);
C.outdata();
C.indata(11,25);
C.outdata();
getch();
}
M=1
N=5
M=11
N=25
Note that when we use parameterized constructor, we must pass the initial values as arguments
to the constructor function when an object is declared.
In the above program, the following declaration statement (Integer C) may not work, in this
case we need to define multiple constructors.
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class Integer
{
private:
int m;
int n;
public:
Integer();
Integer(int, int);
void indata(int, int);
void outdata();
};
15
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Integer::Integer()
{
m=0;
n=0;
}
Integer::Integer(int A, int B)
{
m=A;
n=B;
}
void Integer::outdata()
{
cout<<"\nM="<<m<<endl;
cout<<"N="<<n<<endl;
}
void main()
{
Integer int1, int2(1,1);
cout<<"int1"<<endl;
int1.outdata();
int1.indata(25,25);
int1.outdata();
cout<<"\nint2"<<endl;
int2.outdata();
int2.indata(50,50);
int2.outdata();
getch();
}
16
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Int1
M=0
N=0
M=25
N=25
Int2
M=1
N=1
M=50
N=50
Note: When more than one constructor function is defined in a class, we say that constructor
overloading.
Destructors
A destructor is a member function whose name is the same as the class name but is
preceded by a tilde ~ . The destructor is used to destroy the objects that have been created by a
constructor.
For example, the destructor for the above program (class integer) is defined as follows:
~integer() { }
Example:
This example shows how the destructor will be invoked implicitly by the compiler upon
exit.
17
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
#include <iostream>
#include <conio.h>
using namespace std;
int co=0;
class alpha
{
public:
alpha()
{
co++;
cout<<"\nObject#"<<co<<" is created.";
}
~alpha()
{
cout<<"\nObject#"<<co<<" is destroyed";
co--;
}
};
void main()
{
cout<<"\n \nEnter The MAIN\n";
alpha A1,A2,A3,A4;
{
cout<<"\n \nEnter the BLOCK\n";
alpha A5,A6;
}
cout<<"\n \nRe-enter the MAIN and EXIT\n";
alpha a7;
getch();
}
18
Object Oriented Programming Lecture 1
2nd class Asst. Lecture Omar Nowfal
Notes:
A destructor never takes any arguments nor does it return any value.
A destructor will be invoked implicitly by the compiler upon exit from the program (or
block or function) to free memory space.
The objects are destroyed in the reverse order of creation.
19