Object Oriented Programming using C++
Class and Objects
The most important features of C++ is the "Class". Bjarne Stroustrup
initially gave the name "C with classes" to his new language.
A class is an extension of the idea of structure used in C.
We know that one of the unique features of C language is structure.
To understand good about Class, we need a little knowledge of Structure and its some limitations.
Structure is a user defined data-type that contains number of data of different types. But it has some limitations.
To overcome the limitations of C structure "Class" comes in existence.
Limitation of C structure is that they do not permit data hiding. Structure members can be directly accessed by any function anywhere in their scope.
Another reason is that, in C++, by default, the members of a class are Private, while in C language, by default, the members of a structure are Public.
The general form of Class declaration:
{
Private:
variable declaration;
function declaration;
Public:
variable declaration;
function declaration;
};
The variable declared inside the class are known as data members and the
functions are known as members functions.
Only the member functions can have access to the Private data member and
private functions. Public member can be accessed from outside the class.
when defining a class we are creating a new abstract data type that can be treated like any other built in data type.
Creating Objects
Once a class has been declared, we can create variables of that type by using the class name for creates a variable x of type item.
created
In C++ the class variables are known as objects. Therefore, x is called an object of type item.
The declaration of an object is similar to that of variable of any basic type. The necessary memory space is allowed to any object at this stage.
Encapsulation
The wrapping up or binding of data and functions into a single unit (called class) is known as encapsulation.
Data hiding
Insulation of data from direct access by the program is called data hiding.
Attributes & Methods
Attributes can be defined as a property of an object. It cannot exist independently of the object. Attributes may take other objects as values.
Methods
Basically, method is a way by which an object receives and responds to a particular kind of message. In C++, a method is a member function.
For example, In real life, a car is an object, color and weight of this car is its 'attibutes' and methods such as drive and brake.
Abstract Data Type
An abstraction that describe a set of objects in terms of an encapsulated or a hidden data and operation on that data.
Constructors and Destructor
A constructor is a 'special' member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. The constructor is invoked whenever an object of its associated class is created. It is called constructed because it constructs the value of the data members of the class.
A constructor is declared and defined as follows:
class integer
{
int m, n;
public:
integer(void);
......
......
};
integer : : integer(void)
m=0; n=0;
}
When a class contain a constructor means that an object is created by the class will be initialized automatically.
Special Characteristics of constructor functions:
They should be declared in the public section.
They are invoked automatically when the objects are created.
They cannot return any values.
They cannot be inherited, though a derived class can call the base class constructor.
Constructors cannot be virtual.
An object with a constructor (or destructor) cannot be used as a member of union.
There are three constructor introduced in C++
Default Constructor
Parameterized Constructors
Copy Constructors
Default Constructor
A constructor that accepts no parameters is called default constructor.
The default constructor of class integer is integer().
Some important points should be remember before creating a constructor
The name of constructor should be match (same) with class name.
A constructor should always be declare in public section.
A default constructor does not contain any argument.
Every program in C++, has a default constructor.
A default
constructor can contain block of code but, does not contain
parameteres.
If a programmer forget to declare default constructor during the program
and creates an object such as
integer int1;
then, a C++ compiler invokes default constructor automatically when the
objects are created such as integer() { }.
The given example will make it clear.
Parameterized Constructors
C++ permits us to achieve this objective by passing arguments to the
constructor function when the objects are created.
The consteuctors that can take arguments are called parameterized
constructors.
The constructor integer() may be modified to take arguments as shown below:
Some important points should be remember before creating parameterized constructor
when a constructor has been parameterized the object declaration statement
such as
integer int1;
may not work. We must pass the initial values as arguments to the constructor function when an object is declared.
This can be done in two ways:
By calling the constructor
explicitly.
By calling the constructor implicitly.
The following declaration illustrates the first method:
This statement creates an integer object int1 and passes the value 1 and 100 to it.
The second method is implemented as follows:
The method, sometimes called the shorthand method is used very often as it is shorter, look better and is easy to implement.
Copy Constructor
In c++, a copy constructor is declared like this
integer(integer &i);
A copy constructor is used to declare and initialize an object from another
object. We can initialize an object bu following methods:
integer int1(int2);
and
integer int1 = int2;
The
process of initializing through a copy constructor is known as copy
initialization.
Point to remember
A constructor takes a reference to an object of the same class as itself as an argument.
A reference variable has been used as an argument to the copy constructor. We cannot pass the argument by value to a copy constructor.
When no copy constructor is defined, the compiler supplies its own copy constructor.
id of A: 100
id of B: 100
id of C: 100
id of D: 100
Above in the program, some codes are rounded by red color that indicates wrong way to construct a copy constructor because to construct a copy constructor, we have to declare and initialize an object at the same time and place.
The statement
D = A;
shows that the object A assign
value(100) in object D. Here, the rule to make a copy constructor is not
followed by the statement.
Destructors
A destructor, as the name implies, is used to destroy the objects
that have been created by a constructor. Like a constructor, the destructor
is a member function whose name is the same as the class name but is
preceded by a tilde'~'.
For example,
~integer() { }
A destructor never takes any argument nor does it return any value.
It will be invoked implicitly by the compiler upon exit from program.
It helps to clean up storage to destroy object that is no longer accessible.
It is a good practice to declare destructors because it releases memory space for the future.
ENTER MAIN
No.of object created 1
No.of object created 2
No.of object created 3
No.of object created 4
ENTER BLOCK1
No.of object created 5
No.of object destroyed 5
ENTER BLOCK2
No.of object created 5
No.of object destroyed 5
RE-ENTER MAIN
No.of object destroyed 4
No.of object destroyed 3
No.of object destroyed 2
No.of object destroyed 1
Abstract Classes
An abstract class is one that is not used to create objects. An abstract class
is designed only to act as a base class (to be inherited by other classes).
It is a design concept in program development and provides a base upon
which other classes may be built.