Object Oriented Programming
Object Oriented Programming
Object Oriented Programming
BSE II
Lecture 06
Or
A car is a vehicle
Polymorphism
Morph=faces
Classes
The data and methods that operate on this data are encapsulated in a single
package called “calss”.
Classes are generally declared using the keyword class, with the following
format:
class class_name
access_specifier_1:
member1;
access_specifier_2:
member2;
... } object_names;
The body of the declaration can contain members, that can be either data or
function declarations, and optionally access specifiers.
private members of a class are accessible only from within other members
of the same class or from their friends.
protected members are accessible from members of their same class and
from their friends, but also from members of their derived classes.
public members are accessible from anywhere where the object is visible.
N.B: All members of a class declared with the class keyword have private
access for all its members.
class CRectangle
int x, y;
public:
} rect;
This class contains four members: two data members of type int (member x
and member y) with private access and two member functions with public
access: set_values() and area(), we have only included their declaration, not
their definition.
#include <iostream>
class CRectangle
{
int x, y;
public:
{
x = a;
y = b;
}
} int area ()
{
return (x*y);
}
};
int main ()
{
CRectangle rect;
rect.set_values (3,4);
return 0;
}
#include <iostream>
class CRectangle
{
int x, y;
public:
int area ()
{
return (x*y);
}
};
{
x = a;
y = b;
}
int main ()
{
CRectangle rect,rect1;
rect.set_values (3,4);
rect.set_values (5,6);
return 0;
}
the definition of the member function area() has been included directly within
the definition of the CRectangle class.
set_values() has only its prototype declared within the class, but its definition
is outside it.
In outside declaration, we must use the operator of scope (::) to specify that
we are defining a function that is a member of the class CRectangle and not a
regular global function.
The scope operator (::) specifies the class to which the member being
declared belongs, granting exactly the same scope properties as if this
function definition was directly included within the class definition.
One of the greater advantages of a class is that, as any other type, we can
declare several objects of it. e.g.
CRectangle rect,rect1;
rect and rect1has its own member variables and member functions.
Call to rect.area() does not give the same result as the call to rect1.area().
The also have their own function members set_value() and area() that
each uses its object's own variables to operate.
That is the basic concept of object-oriented programming:
Data and functions are both members of the object. We no longer use sets of
global variables that we pass from one function to another as parameters,
but instead we handle objects that have their own data and functions
embedded as members.
N.B: We have not had to give any parameters in any of the calls to
rect.area() or rect1.area(). Those member functions directly used the data
members of their respective objects rect and rect1.
BSE II
Lecture 07
In C++ two different functions can have the same name if their parameter
types or number are different.
#include <iostream>
{
return (a*b);
}
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
return 0;
}
#include <iostream>
class CRectangle
{
int x, y;
public:
int area ()
{
return (x*y);
}
};
{
x = a;
y = b;
}
int main ()
{
CRectangle rect,rect1;
rect.set_values (3,4);
rect.set_values (5,6);
return 0;
}
This constructor function must have the same name as the class, and
cannot have any return type; not even void.
“A class method having the same name as the class name is called the
class constructor.”
#include <iostream>
class CRectangle
{
public:
CRectangle (int,int);
int area ()
{
return (width*height);
}
};
{
width = a;
height = b;
}
int main ()
{
return 0;
}
It initializes the values of width and height with the parameters that are
passed to it.
The memory allocation for the CRectangle object recta is done at compile
time, at certain times you will wish to create objects at run time and delete
them as they are no more required by application. This results in better
memory usage.
N.B: Neither the constructor prototype declaration (within the class) nor the
latter constructor definition include a return value; not even void.
Like any other function, a constructor can also be overloaded with more than
one function that have the same name but different types or number of
parameters.
include <iostream>
class CRectangle
{
public:
CRectangle ();
CRectangle (int,int);
{
return (width*height);
}
};
CRectangle::CRectangle ()
{
width = 5;
height = 5;
}
{
width = a;
height = b;
}
int main ()
{
CRectangle rectb;
return 0;
}
rectb was declared without any arguments, so it has been initialized with the
constructor that has no parameters, which initializes both width and height
with a value of 5.
BSE II
Lecture 08
When you create an object, if you do not declare a constructor, the compiler
would create one for your program; this is useful because it lets all other
objects and functions of the program know that this object exists. This
compiler created constructor is called the default constructor.
#include <iostream>
class Book
{
public:
Book();
};
Book::Book()
{
}
int main()
{
Book B;
return 0;
}
Copy constructor is needed when you want to initialize one object with
another, this is done in 3 ways:
For initialization create a constructor in your class definition that takes the
object to be copied as an argument.
class Point
{
public:
int x,y;
public:
{
x=x1;
y=y1;
cout<<“constructor called”;
}
{
x=p.x;
y=p.y;
}
};
void main()
{
Point p1(5,5);
cout<<“Point p1 (“<<p1.x<<“,”<<p1.y<<“)”<<endl;
Point p2(p1);
cout<<“Point p2 (“<<p2.x<<“,”<<p2.y<<“)”<<endl;
p1.x=10; p1.y=10;
p2.x=20;p2.y=20;
cout<<“Point p1 (“<<p1.x<<“,”<<p1.y<<“)”<<endl;
cout<<“Point p2 (“<<p2.x<<“,”<<p2.y<<“)”<<endl;
}
It is automatically called
The destructor must have the same name as the class, but preceded with a
tilde sign (~) and it must also return no value.
#include <iostream>
class CRectangle
{
public:
CRectangle (int,int);
~CRectangle ();
int area ()
{
}
};
{
*width = a;
*height = b;
}
CRectangle::~CRectangle ()
{
delete width;
delete height;
}
int main ()
{
return 0;
}
It is especially suitable when an object assigns dynamic memory during its
lifetime and at the moment of being destroyed we want to release the
memory that the object was allocated.
The function can be invoked without the use of an object. The friend function
has its argument as objects.
#include<iostream>
#include<conio.h>
class Bank
{
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int backdoorCall(Bank b1);
};
{
return b1.a;
}
main()
{
Bank b1;
b1.test();
backdoorCall(b1);
getch(); return 0;
}
BSE II
Lecture 09
This existing class is called the base class, and the new class is referred to
as the derived class.
A direct base class is the base class from which a derived class explicitly
inherits. An indirect base class is inherited from two or more levels up in
the class hierarchy.
// derived classes
#include <iostream>
class CPolygon
protected:
public:
width=a; height=b;
} };
public:
int area ()
} };
} };
int main ()
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
return 0;
Every derived-class object is an object of its base class, and one base class
can have many derived classes.
The set of objects represented by a base class typically is larger than the set
of objects represented by any of its derived classes.
A base class's public members are accessible within the body of that base
class and anywhere that the program has a handle (i.e., a name, reference or
pointer) to an object of that base class or one of its derived classes.
A base class's private members are accessible only within the body of that
base class and the friends of that base class.
class A
{
int x,y;
protected:
{
x=x1;
}
int getx();
{
return x;
}
public:
int gety();
};
class B: public A
{
public:
int getXCoordinate()
{
return getx();
}
void setXCoordinate(int x)
{
setx (x);
}
}
void main()
{
B objectB;
objectB.setXCoordinate(10);
int x=objectB.getXCoordinate();
}
class A
int x,y;
protected:
void setx (int x1)
x=x1;
int getx();
return x;
public:
int gety();
};
class B: public A
};
class C: public B
};
Kinds of Inheritance
Public
Protected
Private
For example, if we have vehicle as a base class and car as a derived class,
then all cars are vehicles, but not all vehicles are cars.
When deriving from a protected base class, public and protected members
of the base class become protected members of the derived class.
When deriving from a private base class, public and protected members of
the base class become private members of the derived class.
class Student
{
private: firstName;
protected: lastName;
public: rollNumber;
};
{
};
{
};
{
};
BSE II
Lecture 10
Arrays of Objects
The syntax for declaring and using an object array is exactly the same as it is
for any other type of array.
#include <iostream>
class cl {
int i;
public:
};
int main()
{
cl ob[3];
int i;
return 0;
}
OUTPUT: 1 2 3
The exact form of the initialization list will be decided by the number of
parameters required by the object's constructor function.
For objects whose constructors have only one parameter, you can simply
specify a list of initial values, using the normal array-initialization syntax.
As each element in the array is created, a value from the list is passed to the
constructor's parameter.
#include <iostream>
class cl {
int h;
int i;
public:
};
int main()
{
cl ob[3] = {
cl(3, 4),
cl(5, 6)
};
int i;
}
return 0;
}
Pointers to Objects
Just like other variables you can have pointers to objects. When accessing
members of a class given a pointer to an object, use the arrow(->)Operator
instead of the dot operator.
#include <iostream>
class cl {
int i;
public:
cl(int j) {
i=j;
}
int get_i() {
return i;
}
};
int main()
{
cl ob(99);
cl *p;
p=&ob;
cout<<p->get_i();
}
The this pointer is used as a pointer to the class object by the member
function.
It is a common knowledge that C++ keeps only one copy of each member
function and the data members are allocated memory for all of their
instances. This kind of various instances of data are maintained using this
pointer.
class student
{
int rollNo;
public:
student(int roll_no)
{
rollNo=roll_no;
}
void display()
{
cout<<this->rollNo;
}
};
main()
{
student s1 (50),s2(30);
s1.display();
s2.display();
getch();
}
These operators are used to allocate and free memory at run time.
The new operator allocates memory and returns a pointer to the start of it.
type *p_var;
delete p_var;
#include <iostream>
#include <new>
int main()
int *p;
try {
p = new int; // allocate space for an int
return 1;
*p = 100;
delete p;
return 0;
• The type of the initializer must be compatible with the type of data for which
memory is being allocated.
• You can allocate arrays using new by using this general form:
delete [ ] p_var;
• int main()
• {
• int *p, i;
• try {
• p = new int [10]; // allocate 10 integer array
• return 1;
• }
• p[i] = i;
• return 0;
• }
• You can allocate objects dynamically by using new. When you do this, an
object is created and a pointer is returned to it.
• The dynamically created object acts just like any other object. When it is
created, its constructor function (if it has one) is called.
• MCS II
• Lecture 10
• e.g
if you have to write a function for printing different data types, you need to
write many different functions and user will be required to remember such functions
during using applications in C like languages, but in C++ you will use only one
function name for all such functions. Each function will have its own independent
implementation. When you call this function with common name, the runtime
decided which implementation to call depending on the current context.
Static Polymorphism
In this case the compiler resolves at the compile time which implementation
to call for a method having common name. That is why it is called early
binding.
Normal Function
In C++ two different functions can have the same name if their parameter
types or number are different.
In C++ two different functions can have the same name if their parameter
types or number are different.
#include <iostream>
{
return (a*b);
}
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
return 0;
}
In this case, the run time environment decides which implementation to call
during the program execution.
Base Class
{…
MyMethod()
{
….
}
…
};
Derived: Base
{…
MyMethod()
{
….
}
…
};
class Base{
public:
void show()
{
}};
public:
void show()
{
void main()
{
Base base;
Derived derived;
Base *baseptr=&base;
baseptr->show();
baseptr=&derived;
baseptr->show();
cout<<endl;
Derived *derivedptr=&derived;
derivedptr->show();
derivedptr->show();
cout<<endl;
}
Virtual Functions
OUTPUT
• The methods declared in the base class are overridden in derived classes.
The derived classes provide the implementation for these methods.
• Rather than providing an empty implementation for the base class methods,
methods are declared as “pure virtual functions”.
• A pure virtual function does not provide the implementation and is declared
as
• class Base{
• public:
• };
• public:
• void show()
• {
• cout<<"In Derived1 Class Show method" << endl;
• }};
• public:
• void show()
• {
• }};
• void main()
• {
• Derived1 derived1;
• Derived2 derived2;
• baseptr->show();
• baseptr=&derived2;
• baseptr->show();
• cout<<endl;
• }
• Class Base
• {
• public:
• }
• {
• };
• Void main()
• {
• Derived derived;
• }
• Compiler will flag an error as our derived class does not provide any
implementation for the inherited Show method.
• Or, you should not attempt to instantiate the Derived class in your
program code.