AD

Breaking News

Basic concepts of OOP- Java/C++

Basic concepts of OOP C++/java-Object Oriented Programming



The concept of computer programming languages is highly in demand nowadays the technology is completely based on programming languages and these programming languages are major source of software development and maintenance.

Object oriented programming OOP:

Object oriented programming is a combination of that attributes, functions and methods combine and make an object oriented programming is class based language all the data attributes and functions are defined in the class. There are many languages which are used in object oriented programming for example Java, Python, C++. If you and understand using a simple example for example a class of human will contain attributes to represent information about that human like that persons age, his name, his height, his education. Each class contains a unique name and the attributes of that class are present inside it. Object Oriented programming is used by the developers as a tool to develop he was in classes in capsulation and different here we will discuss the most important basic concepts of object oriented programming OOP. There are four basic concepts of object oriented programming: in capsulation, inheritance, polymorphism and obstruction.

Encapsulation:

In capsulation means to hide or to protect this concept is used in object oriented programming in which by encapsulating your class you can protect your class members attributes and functions. To achieve in capsulation you need to keep your class private so that you would get a numbers and functions remains private and cannot be accessed directly.

For example:

Class human {

Private

Int a;

Public 

Void set (int x)

{a=x;}

Abstraction:

Obstruction is an important feature of object oriented programming obstruction is used for displaying only the important information and hiding the other details that I need to be private. Abstraction refers to give only important information about data to the world and hiding The details that are present in the background implementation.

For example:

If you take in real life examples to understand this concept for example a man is driving a car he pressed the accelerator and the car increases its speed and when he plays the break the car stops here the driver has only the knowledge to press the Accelerator or break but he does not know the mechanism behind the accelerator and the engine that how the speed of car increases when you press the accelerator this is called obstruction.

Example:

class Abstraction {

   private:

   int w,b;

  public:

 void set(int l, int m) {

  w = l;

   b = m; }

  void display()

        {

        cout<<"w= " <<w<< endl;

       cout<<"b= " << b<< endl; } };

  int main() {

    Abstraction ob;

    ob.set(11,22);

    ob.display() ;

    return 0; }

Inheritance:

The ability of a class to derive characteristics and attributes from another class is called inheritance it is a very important feature of programming. Inheritance is the process in which new classes are created from existing classes and new class is called derived class or it is also called child class and the previous class is called base class and parent class the derived class is inherited from the base class. The inheritance of derived class from the parent class means it inherits all the attributes and properties of parent class without any changing and can also add new features according to requirements the new features added into the drive class will not affect the parent class.

Example:

Code:

class man

{

    char name[20];

    public:

        void set_a()

   {

     cout<<"Enter Name:";

       cin.get(name,20);}

       void display_a()

   {

  cout<<endl<<id<<name;} };

class st: private man {

   char course[30];

   int fees;

    public:

    void set_s()

    {  set_s();

            cout<<"Enter book name:";

            cin.getline(book,20);

            cout<<"Enter Fees:";

             cin>>fees;

        }

         void display_s()

        {

            display_l();

            cout<<"s"<< book <<"\t"<<fees;} };

Polymorphism:

Polymorphism means to have different forms in easy language we can define polymorphism as the ability of being displayed in more than one form if you take a real life example of polymorphism like a human is a father at a time he’s also a husband the same time and he is also a brother At the same time. So polymorphism means that a person have different behaviors in different situations polymorphism.

Types:

  • Compile time polymorphism 
  • Run time polymorphism 

Run time polymorphism the is a type of polymorphism which is achieved by using function overloading and operator overloading method.

Example:

Sample code:

class man {

public:

    void function(int a {

 cout << "value of a is " << a << endl;}

   void function(double a)

    {

  cout << "value of a is " << a << endl;}

    void function(int a, int b)

    {

     cout << "value of a and b is " << a << ", " << b << endl;}  };

int main()

    man obj1;

    object1.function(2); 

    object1.function(9.21);  

    object1.function(22,85);

    return  0; }

Dynamic binding:

In dynamic binding the code which is to be executed as a response is decided at the time of compilation i.e. runtime.

 


No comments

Thanks you