Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Monday, September 5, 2011

Inheritance in C++ with example


Inheritance is the process by which the object of a derived or sub class can acquire the properties of an existing class. Suppose we want to change the above program so that it’s object will contain another long type variable to store the phone number. It is not always possible to modify the class. So we inherits the properties of the object of the above class (here person) to another class Phone and add new features in the class Phone. Ultimately we will create objects of class Phone.

Here is the sample C++ program on Inheritance.

#include< iostream.h>
#include< stdio.h>
#include< conio.h>
class person
{
protected:
char name [40];
public:
void setName ()
{
puts ("Enter Name: -");
gets (name);
}
};
class Phone: public person /* class Phone is a derived class of person*/
{
private:
long phone_no;
public:
void getPhone ()
{
cout<< "Enter the Phone Number: -";
cin >>phone_no;
}
void display ()
{
cout<< "\nName is: -"<< name;
cout<< "\t"<< phone_no;
}
};
void main ()
{
clrscr ();
Phone p;
Phone *ptr [10];
int n=0;
char choice;
do
{
ptr [n]=new Phone;
ptr [n]->setName ();
ptr [n]->getPhone ();
n++;
fflush (stdin);
cout<< "Another (y/n): -";
cin >>choice;
} while (choice=='y');
for (int i=0;i< n; i++)
{
ptr [i]->display ();
}
getch ();
}

Saturday, September 3, 2011

Encapsulation in C++ programs


In my last post, encapsulation is discussed. Encapsulationis very much important for cbse students. In most of the cbse C++ sample paper, you will find at least 2-3 programs purely based on encapsulation. Here is a program demonstrating encapsulation in C++.


#include< iostream.h >
#include< conio.h >
class Rect
{
private:
int len, br;
public:
void getData ()
{
cout<< endl<< "Enter Length and Breadth: -";
cin>>len>>br;
}
void setData (int l, int b)
{
len=l;
br=b;
}
void displayData ()
{
cout<< endl<< "Length="<< len;
cout<< endl<< "Breadth="<< br;
}
void area ()
{
int a, p;
a=len*br;
p=2*(len+br);
cout<< endl<< "Area="<< a;
cout<< endl<< "Perimeter="<< p;
}
};
void main ()
{
clrscr ();
Rect r1, r2, r3;
cout<< endl<< "For object number 1: -";
r1.setData (10,20);
r1.displayData ();
r1.area ();
cout<< endl<< "For object number 2: -";
r2.setData (3,5);
r2.displayData ();
r2.area ();
cout<< endl<< "For object number 3: -";
r3.getData ();
r3.displayData ();
r3.area ();
getch ();
}

The class Rect specified in the above C++ program contains two data members’ len and br and four function members getData (), setData (), displayData () and area (). The first function getData (int, int) takes two integer values from the user and set them in the two data members of the object of Rect class. setData () function sets constant values in the data items of the object. displayData () function displays the values of the data members of an object of class Rect while area () function calculates and displays the area and perimeter.

The body of the class Rect contains two unfamiliar keywords private and public. They are used in C++ programs to implement a concept known as data hiding, encapsulation in C++.  private data and function members can be accessed from within the class where public members are accessible from outside the class. The function members provide controlled access to the data members of an object. Usually the data members within a class are private and the function members are public. When the class is created let’s have a look how main () function makes use of it.

Rect r1, r2, r3; defines three objects r1, r2 and r3 of class Rect. Remember that the specifications of the class does not create any object, it only describes how the objects will look when they are created. It is the definition that actually creates objects, which can be used by the program. Thus, defining an object is similar to defining a variable of any data type- space is set-aside for it in memory.

Next is r1.setData (10, 20); this statement does not look like a normal function call. The name of the object r1 is associated to the function using a dot (.). This syntax is used to call a function member that is associated with a specific object.

Thursday, September 1, 2011

Basic concepts of Object Oriented Programming


In Object Oriented Programming the problem is divided into objects and then build data and functions around these objects. The combination of data and functions make up an object.

The data of an object can be accessed only by the functions associated with the object.

What is class

Most languages offer primitive data types like: - int, long, float etc. Their data representations and response to different operators are defined as a part of the language. The language does not know user defined data types. The programmer defines its format and behavior by defining a class. So, a class is a user defined new data type, which serves as a template, and an object is an instance of a class. It specifies what data and functions will be included in objects of the class. A class may be thought of a data type and an object as a variable of that data type. Once a class has been defined we can create any number of objects of that class.

Main features of object oriented programming

1. Encapsulation: - The wrapping up of data and function into a single unit (called object) is called encapsulation. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it.

2. Inheritance: - This is a mechanism by which one object acquires the properties of another object of different class. Inheritance is probably the most powerful feature of object-oriented programming. Inheritance is the process of creating new classes, called derived or sub classes from existing classes. The existing classes are called base or super classes. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one.

3. Polymorphism: - This is another OPP concept. It means ‘many forms’ i.e. the ability to take more than one form. For example, an operation can exhibit different behavior in different situation.

Subscribe via email

Enter your email address:

Delivered by FeedBurner