Pages

Friday 27 August 2021

BASIC CONCEPTS OF OOP’S

 BASIC CONCEPTS OF OOP’S

  • CLASS
  • OBJECT
  • DATA ABSTRACTION
  • DATA HIDING
  • ENCAPSULATION
  • INHERITANCE
  • POLYMORPHISM
  • DYNAMIC BINDING
  • MESSAGE COMMUNICATION 

CLASSES AND OBJECTS

Class: class is a user defined data type that represents an entity, which is a collection of members and methods.

The entire set of data and code of an object can be made the user define data type with help of a class. Infact objects are variables of type class. Once a class has been defined, we can create any number of objects belonging to that class.

Syntax:  

class <class name>
          {
            access specifier/visibility mode:
            variable declarations/data members;
            access specifier/visibility mode:
            function declarations/member functions;
         };
 

The keyword class specifies that what follows is abstract data of type class. The class body contains the declarations of variables and functions, which are called as class members. The variables declare inside the class are known as data members and functions are known as member functions.

C++ supports three types’ access specifiers

a) public

b) private

c) protect

 These three keywords are also known as visibility modes.

Public: It indicates the accessibility of a member to the outside class i.e., the data members can be accessed by any function from the outside. These members are used to provide an interface for the outside classes.

Private: The members can be accessed only by the member functions. The class members that have been declared as private can accessed only from within the class. By default, the members of class are private. Data hiding is possible with the help of private keyword.

Protected: The member data can be accessed by member functions of that class. The member is also accessible to member functions and the friend functions that are derived from this class. But they are not available to any one of the outside function. This specifier is most recently in the concept of Inheritance. 

DIFFERENCES BETWEEN THE THREE ACCESS SPECIFIERS 

Public

Private

Protected

Accessible from own class members

Accessible from own class members

Accessible from own class members

Accessible from derived class members

Not Accessible from derived class members

Accessible from derived class members

Accessible from objects outside the class

Not Accessible from objects outside the class

Not Accessible from objects outside the class


The declaration of an object is similar to that of a variable of any basic type.
Syntax: classname obj_name;
Example: student s;
where s is an object of class student.
 
Class: is a collection of data & functions. Class is an entity, which may represent a person, place, and location anything which we can feel it in real-life.
 
Object: is a functionable element that is used to access the members and methods of a class. Simply, object is an instance of class.
 
Example Program on Class and Object
 
#include<iostream.h>
#include<conio.h>
class examp
{
int a,b,t;
public:
void getdata();
void swap();
void disp();
};
void examp::getdata()
{
cout<<"enter a and b values"<<endl;
cin>>a>>b;
}
void examp::swap()
{
t=a;
a=b;
b=t;
}
void examp::disp()
{
cout<<"After swapping";
cout<<a<<"\t"<<b;
}
void main()
{
examp e;
clrscr();
e.getdata();
e.swap();
e.disp();
getch();
}
Output:
enter a and b values
2 3
After swapping 3 2

DATA ABSTRACTION

Abstraction refers to an act of representing only external features without including the background details or explanations. The process of extracting the essential features without exploring non-essential things is part of object. Abstraction permits the programmer to look at something without being concerned with its internal details

Example


Driver is an implementer here. If he was about to stop he has to stop he is not responsible for the rest of the non-essential things. Creating new data types using encapsulated data item, which suits to an application to be programmed is known as data abstraction.

DATA HIDING AND ENCAPSULATION

Data hiding is an insulation of the data from the direct access of the program is called as Data Hiding. It is a process of attaching the data closely to the system by protecting the data from other parts of the system. It means that data is concealed within a class, so that it cannot be accessed by functions outside the class even by mistake. The mechanism used to hide data is to put it in a class and make it private.

Encapsulation it is a method of wrapping up of data and functions into a single unit. This is the central idea of OOP. The data cannot be accessed by the outside functions but the data can only be accessed by only those functions, which are wrapped in that single unit.



/* DATA HIDING AND ENCAPSULATION */

#include<iostream.h>
#include<conio.h>
class hiding
{
private:
     int age; /* data hiding */
public:
     void setData(int);
     void print();
};
void hiding::setData(int x)
{
if(age>0)
age=x;
}
void hiding::print()
{
cout<<age<<endl;
}
void main()
{
clrscr();
hiding h1;
h1.setData(13);
h1.print();
getch();
}
Output:
13

 INHERITANCE

The mechanism of deriving a new class from an old one is called as Inheritance. The old class is called as base class and the new one is called as derived class.  The Derived class inherits some or all the features of the base class. A class can also inherit properties from more than one class or from more than one class.


Example : 
Single inheritance Program using the visibility mode of public

#include<iostream.h>
#include<conio.h>
class stdbaseclass
{
 private:
 int sno;
 char sname[22];
 public:
void getstd()
{
cout<<"enter sno and sname";
cin>>sno>>sname;
}
void putstd()
{
cout<<"std  no is" <<sno<<endl;
cout<<"std name is" <<sname<<endl;
}
};

class phyderivedclass:public stdbaseclass
{
 int h,w;
 public:
void getphy()
{
cout<<"enter height and weight";
cin>>h>>w;
}
void putphy()
{
cout<<"std  height is" <<h<<endl;
cout<<"std weight is" <<w<<endl;
}
};

void main()
{
 phyderivedclass p;
 clrscr();
 p.getstd();
 p.putstd();
 p.getphy();
 p.putphy();
 getch();
}

Example : 
Multiple inheritance Program using the visibility mode of private 
#include<iostream.h>
#include<conio.h>
class stdbc
{
 private:
 int sno;
 char sname[22];
 public:
void getstd()
{
cout<<"enter sno and sname";
cin>>sno>>sname;
}
void putstd()
{
cout<<"std  no is" <<sno<<endl;
cout<<"std name is" <<sname<<endl;
}
};

class markbc
{
 protected:
 int m1,m2,m3;
 public:
void getmarks()
{
cout<<"enter m1 m2 m3 marks ";
cin>>m1;
cin>>m2;
cin>>m3;
}
void putmarks()
{
cout<<"marks m1 is" <<m1<<endl;
cout<<"marks m2 is" <<m2<<endl;
cout<<"marks m3 is" <<m3<<endl;
}
};

class resultdc : public stdbc ,public markbc
{
 int tot;
 float avg;
 public:
void show()
{
tot = m1+ m2+ m3;
cout<<"total marks is ";
cout<<tot<<endl;
avg=tot/3.0;
cout<<"average marks is ";
cout<<avg<<endl;
}
};

void main()
{
 clrscr();
 resultdc r;
 r.getstd();
 r.getmarks();
 r.putstd();
 r.putmarks();
 r.show();
 getch();
}

Program on Access specifier 
(public,private,protected) 

 

WITHIN

CLASS

OUTSIDE

CLASS

DERIVED

CLASS

PUBLIC

YES

YES

YES

PRIVATE

YES

NO

NO

PROTECTED

YES

NO

YES


Program on Access specifier (public)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
 getch();
}

Program on Access specifier (private)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if 
               it is inside the class private:"<<y<<endl;
 }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */         we will get error if we remove comment
 as.display();
 getch();
}

Program on Access specifier (protected)
#include<iostream.h>
#include<conio.h>
class accspe
{
 public:
 int x;
 private:
 int y;
 protected:
 int z;
 public:
 void display()
 {
   y=20;
   cout<< "y value can be seen if it is inside the class private:"<<y<<endl;
 }
};

class derived : public accspe
{
  public:
  void show()
  {
  z=30;
  cout<< "z value Potected is "<<z<<endl;
  }
};

void main()
{
 clrscr();
 accspe as;
 cout<<"enter x public value:"<<endl;
 cin>>as.x;
 cout<<"x public value:"<<as.x<<endl;
/* cout<<"enter y private value:"<<endl;
 cin>>as.y;
 cout<<"y private value:"<<as.y<<endl;
 */
 as.display();
 derived d;
 d.show();
 getch();
}


Polymorphism:
        poly means many
        morphism means form

Example:

I.Static /compile/early binding
  1. Function Overloading: 2 or more functions can have same name with different parameters
II.Dynamic/Runtime/late binding
  1. Function overriding:2 or more functions can have same name with same parameters
  2. Inheritance should be there if you are using function overriding. It can not be done with in a class for this we required derived class and base class

Program on Function Overloading:

// Function overloading Program using c++
#include<iostream.h>
#include<conio.h>
class addition
{
 public:
 void sum(int a,int b)
 {
 cout<<"a+b :"<< a+b;
 }
 void sum(int a,int b,int c)
 {
 cout<<"a+b+c :"<< a+b+c;
 }
};

void main()
{
 clrscr();
 addition a;
 a.sum(11,22);
 a.sum(1,2,3);
 getch();
}

Program on Function Overriding:

// Function overriding Program using c++
#include<iostream.h>
#include<conio.h>
class base
{
 public:
 void show()
 {
 cout<<"Base class"<<endl;
 }
};

class derived : public base
 {
 public:
  void show()
  {
  cout<<"derived class"<<endl;
  }
};

void main()
{
 clrscr();
 base b;
 derived d;
 b.show();
 d.show();
 getch();
}


No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...