Object Oriented Programming��With��C++
Book : E Balagurusamy
Basic terminology
- usually a person, place or thing (a noun)
- an action performed by an object (a verb)
- description of objects in a class
- a category of similar objects (such as automobiles)
- does not hold any values of the object’s attributes
3
Example for attributes and methods
Attributes:
Methods:
4
Why OOP?
once an object class is created it can be used in other applications
classes can be tested independently
reused objects have already been tested
5
Building blocks of OOP
Specifying a Class
Class specification has two parts:
Class declaration describes the type and scope of its members.
Class function definitions describe how the class function are implemented.
The general form of a class declaration is:�class class_name�{�private:�variable declaration;�function declaration;�public:�variable declaration;�function declaration;�};
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces, terminated by a semicolon.
�
Define C++ Objects�
A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. following statements declare two objects of class Box −
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.
Attributes
Methods
Two Versions of C++
11
#include <iostream.h>��int main()�{� /* program code */� return 0;�}
Two Versions of C++ (cont.)
12
#include <iostream>�using namespace std;��int main()�{� /* program code */� return 0;�}
Namespaces
13
C++ Program
C++ Program
Design Principles of OOP
Four main design principles of Object-Oriented Programming(OOP):
17
Encapsulation
Analogy:
18
Abstraction
Analogy:
19
Polymorphism
Method Overriding
Runtime polymorphism uses method overriding. In method overriding, a child class can provide a different implementation than its parent class
Method Overloading
Compile Time polymorphism uses method overloading. Methods or functions may have the same name, but a different number of parameters passed into the method call. Different results may occur depending on the number of parameters passed in.
Analogy:
20
Inheritance
21
Constructors
Integer c1; not only creates the object c1 but also intializes its data member.
A constructor is declared and defined as follows:
class integer
{
int m,n;
public:
integer();
...............
};
Integer::integer()
{
m=0;n=0;
}
How constructors are different from a normal member function?�
Types of Constructors
Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: "<< c.a << endl << "b: "<< c.b;
return 1;
}
Note: Even if we do not define any constructor explicitly, the compiler will automatically
provide a default constructor implicitly. The default value of variables is 0 in case of
automatic initialization.
#include <iostream>
using namespace std;
class Test
{
static int x;
public:
Test() { x++; }
static int getX() {return x;}
};
int Test::x = 0;
int main()
{
cout << Test::getX() << " ";
Test t[5];
cout << Test::getX();
}
What will be the output
Parameterized Constructors
A constructor integer() may be modified is declared and defined as follows:
class integer
{
int m,n;
public:
integer(int x, int n);
...............
};
integer::integer(int x, int y)
{
m=x;n=y;
}
When object is intialized, we must pass intials values as arguments to the constructor function.
By calling constructor explicitly: integer int1=integer(0,100);
By calling constructor implicitly: integer int1(0,100);
// CPP program to illustrate
parameterized constructors
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = “
<< p1.getY();
return 0;
}
Copy Constructor
ClassName (const ClassName &old_obj);
Following is a simple example of copy constructor.
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p2) {x = p2.x; y = p2.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15);
Point p2 = p1;
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = “
<< p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = “
<< p2.getY();
return 0;
}
When is copy constructor called?
In C++, a Copy Constructor may be called in following cases:�1. When an object of the class is returned by value.�2. When an object of the class is passed (to a function) by value as an argument.�3. When an object is constructed based on another object of the same class.�4. When compiler generates a temporary object.
Destructors in C++�
class String
{
private:
char *s;
int size;
public:
String(char *); // constructor
~String(); // destructor
};
String::String(char *c)
{
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
}
String::~String()
{
delete []s;
}
Can there be more than one destructor in a class?�No, there can only one destructor in a class with classname
preceded by ~, no parameters and no return type.