Sunteți pe pagina 1din 2

OOPs Concepts Lesson-1

Object Oriented programming (OOP) is a programming style that is associated with the concept of
Class, Objects and various other concepts revolving around these two (Class and Objects), like
Inheritance, Polymorphism, Abstraction, Encapsulation, etc.
Objects are the basic unit of OOP. They are instances of class, which have data members and uses
various member functions to perform tasks.
Class: It is similar to structures in C language. Class can also be defined as user defined data type
but it also contains functions in it. So, class is basically a blueprint for object. It declares and defines
what data variables the object will have and what operations can be performed on the class's object.
Abstraction refers to showing only the essential features of the application and hiding the details. In
C++, classes provide methods (functions) to the outside world to access and use the data variables,
but the variables are hidden from direct access. This can be done by access specifiers (private,
public, protected).
Encapsulation is all about binding the data variables and functions together in a class.
Inheritance is a way to reuse once written code again and again. The class which is inherited is
called base class & the class which inherits is called derived class. So when, a derived class inherits
a base class, the derived class can use all the functions which are defined in base class, hence
making code reusable.

Polymorphism: It is a feature, which lets us create functions with same name but different
arguments, which will perform differently. That is function with same name, functioning in different
way. Or, it also allows us to redefine a function to provide its new definition.
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at
runtime.

#include <iostream.h> // Example-1


class Abc
{ private: int x, y, r; In this encapsulation example, the integer
public: values "x, y, r" of the class "Abc" can be
int Addition(int x, int y) accessed only through the function "Addition".
{ r = x + y; These integer values are encapsulated inside
return r; the class "Abc".
}
void show( )
{ cout << "The sum is " << r << endl;}
};
int main()
{ Abc obj; // Abc class creates ‘obj’ object
obj.Addition(10, 4);
obj.show();
}

Classes and Objects


The fundamental idea behind OOPs is to combine both data and the functions that operate on the
data into a single unit. Such a unit is called an object. Data members of an object can be accessed
only by using the functions of the object. Therefore, the data is hidden and is safe from accidental
alteration. Data and functions are stored in a single entity. The functions of an object are called the
member functions.
class: Rectangle
Class
The important element of an object-oriented instance data
language is class. instance functions
An Object in C++ is an instance of a class.
For example, in the following statement:
int a, b, c; object: rect1 object: rect2
the objects: a, b, c are three variables of type int member data member data
class. member functions member functions
#include <iostream.h> // Example-2 Lesson-1 Contd...
class Rectangle
{ int length, width; // data members (private)
public:
Rectangle(int a, int b) // constructor function
{ length = a; width = b; }
public: int area() // member function
{ return (length * width); }
};
int main()
{ Rectangle r1(5,3); // r1 is an object of class Rectangle
Rectangle r2(12,10); // r2 is another object of class Rectangle
cout << "area of r1 is: " << r1.area() << endl;
cout << "area of r2 is: " << r2.area() << endl;
return 0;
}
Constructors
C++ allows objects to initialize themselves as and when they are created. This automatic initialization
is performed through the use of constructor functions. A constructor function is a special function that
is a member of the class and has the same name as that of the class. Notice that the constructor
Rectangle() had no return type specified. In C++, constructors cannot return values.
Destructors
Destructors are functions that are complimentary to constructors. They de-initialize objects when they
are destroyed. A destructor is invoked when an object of the class goes out of scope, or when the
memory occupied by it is de-allocated using the delete operator. A destructor is a function that has
the same name as that of the class but is prefixed with a ~ (tilde).
Access Control in Classes
Access specifiers in C++ class define the access control rules. C++ has three new keywords
introduced, namely, public, private and protected.
These access specifiers are used to set boundaries for availability of members of class be it data
members or member functions. Access specifiers in the program, are followed by a colon.
Public, means all the class members declared under public will be available to everyone. The data
members and member functions declared public can be accessed by other classes too. Hence there
are chances that they might change them. So the key members must not be declared public.
Private keyword means that no one can access the class members declared private outside that
class. If someone tries to access the private member, they will get a compile time error. By default
class variables and member functions are private.
Protected, is the last access specifier, and it is similar to private, it makes class member inaccessible
outside the class. But they can be accessed by any subclass of that class. (If class A is inherited by
class B, then class B is subclass of class A. We will learn this later).
Defining Class and Declaring Objects
When we define any class, we are not defining any data; we just define a structure or a blueprint, as
to what the object of that class type will contain and what operations can be performed on that object.
Note: See the example-1 and -2 above.
class Rectangle // Example-2
Member Functions in Classes { int length, width; // data members (private)
The definition of member functions can be public:
inside or outside the definition of class. If the Rectangle(int a, int b) // constructor function
member function is defined inside the class int area() // member function
definition it can be defined directly, but if its };
defined outside the class, then we have to Rectangle :: Rectangle(int a, int b)
use the scope resolution “::” operator along
{ length = a; width = b; }
with class name along with function name.
int Rectangle :: area()
See the Example-2 
{ return (length * width); }
Prof. V.V. Muniswamy

S-ar putea să vă placă și