Home Website & Development Oops(object-oriented programming)Concepts in C++ Programming

Oops(object-oriented programming)Concepts in C++ Programming

0

C ++ Programming was developed by Bjarne Stroustrup in bell labs

Which supports high level and low-level languages. it is a combination of features of oop and pop. C ++ is a middle-level language.

C ++ Programming Language This is OOP (Object-Oriented Programming) Language, even here this ‘Simula’ was created oop Language.

C++ general-purpose programming language  C ++ Programming is run on Windows, Linux, Mac OS, etc. Operating Systems. C ++ Programming Language is used to create Computer Softwares.

INDEX –

  1. Oops in c++ programming
  2. Oops concepts in c++ programming
  3. Class
  4. Object
  5. Encapsulation and data abstraction
  6. Inheritance
  7. Single Inheritance
  8. Multilevel Inheritance
  9. Multiple Inheritance
  10. Hierarchical Inheritance
  11. Hybrid Inheritance
  12. Polymorphism
  13. Compile-time Polymorphism
  14. Function Overloading
  15. Operator Overloading
  16. Run-time Polymorphism
  17. virtual function
  18. Dynamic Binding
  19. Message passing
  20. Conclusion
  21. Frequently asked questions

Oops in C++ programming

OOP (object-oriented programming) is a programming paradigm that uses the new concepts of structured programming and eliminates the limitations of procedural programming approach.

In OOP, data is considered as an important element and you cannot access and modify data in a program.

OOP breaks down a problem in some entities, called objects, again creates data and functions around these entities.

An object binds data and functions into a single unit to protect the data from modifications from outside functions.

Difference between procedure-oriented programming (POP) and object-oriented programming (OOP) –

POPOOP
In POP, the program is divided into smaller parts called functions. OOP, the program is divided into objects 
 POP uses the Top-down approach.  OOP uses the bottom-up approach. 
Data in POP is not hidden, that is, we cannot hide data properly in it, so it is less secure. OOP the data is hidden which means we can easily hide the data in it so it is more secure. 
POP does not have access specifiers.OOP has access specifiers like; – public, private, etc.
If we have to insert new data or function in POP, then we have to revise the program.  OOP you can insert data and functions without revising the program. 
In POP it is difficult to handle large size programs  OOP  can easily handle large programs. 
 POP uses procedure abstraction OOP uses data abstraction 
POP does not have overloadingOOP has function overloading and operator overloading.
In POP inheritance facility is not available,OOP has an inheritance concept. 
Examples of POP: – C is visual basic, fortan and Pascal examples of OOP: – Java, C ++ and .net etc

Oops concepts in C++ programming

1. Class

A class is a collection of similar types of objects that share common attributes and behavior. Objects are the variables of a user-defined type called class.

Class Is a keyword.  In an OOP program, you create classes to develop an application.

Each class has some properties and functions.

A class consists of member variables or properties and member functions.

It is only a template or design to be used by the objects of the class.

Class is a user-defined data type. It contains data and code is used by the object.

A class is a structure that defines the object’s working. Many objects can be created after creating a class.

Class is a blueprint of objects.

Example –

#include<iostream.h>
#include<conio.h>
//class definition
class tossi
{
          private:
                   int a;
                   int b;
          public:
                   //member function declaration
                   void t1(void);
                   void t2(void);
                   int Addition(void);
};
//member function definitions
void tossi::t1(void)
{
          cout<<“Enter first number: “;
          cin>>a;
          cout<<“Enter second number: “;
          cin>>b;
}
void tossi::t2(void)
{
          cout<<“a= “<<a<<“,b= “<<b<<endl;
}
int tossi::Addition(void)
{
          return (a+b);
}
//main function
void main()
{
clrscr();
          //declaring object
          tossi num;
          int add; //variable to store addition
          //take input
          num.t1();
          //find addition
          add=num.Addition();
          //print numbers
          num.t2();
          //print addition
          cout<<“Addition/sum= “<<add<<endl;
          getch();
}
note:- this program run in Turbo C++

Output –

2. Object

An object is a real-world run-time entity.

An object contains data variables and functions to store and manipulate the data.

The functions associated with an object are called its member functions.

In an object, the data variables can only be accessed by its member functions.

An object is the variable of the class that executes the class and processes the data using the methods available in it.

An object is created. it takes place in memory like other variables.

Example –

#include<iostream.h>
#include<conio.h>
class Add {
public:
    int addition(int x, int y) {
        return x + y;
    }
};
void main() {
       clrscr();
    int x, y, t;
    cout << “Enter two numbers :”;
    cin >> x >> y;
    Add obj;
    t = obj.addition(x, y);
    cout << “\n sum of two numbers is :” <<t;
    getch();
}

Output –

note:- this program run in Turbo C++

3. Encapsulation and data abstraction

Binding data and functions together are called Encapsulation.

In this, the data cannot be accessed outside the class, only the functions of the class can access it. 

Encapsulation helps secure the data and functions from outside interference as data is accessible only to the member functions of the object.

The data abstraction is from input and output the data without background process details in which it is performed by the functions of the class.

These member functions provide an interface between the data of an object and the program.

It protects data from direct access.

To implement this technique, the data variables and methods are defined as private or public.

The private data is accessible only to the member functions of the class and the public data represents the information that can be accessible to the external users of the class.

Example –

// c++ program to explain
// Encapsulation
#include<iostream.h>
#include<conio.h>
class Encapsulation
{
          private:
                   // data hidden from outside world
                   int a;
          public:
                   // function to set value of
                   // variable x
                   void set(int x)
                   {
                         a =x;
                   }
                   // function to return value of
                   // variable x
                   int get()
                   {
                             return a;
                   }
};
// main function
void main()
{
      clrscr();
          Encapsulation ab;
          ab.set(7);
          cout<<ab.get();
getch();
}
note:- this program run in Turbo C++

Output –

4. Inheritance

This is an important feature of OOP that allows One class to use the features of other class.

In this, the object of one class can access and use the properties of the other class as well.

Inheritance is the process of defining new classes by extending the properties of other classes. The class that acquires the properties of other classes is called derived class and the class from which the properties are inherited is called a base class.

The derived class needs to define only those properties which are unique to it.

Derived class

The derived class called ‘child or subclass’

Inherits the properties of the Base class.

Base Class

The base class called ‘parent or superclass’.

The Base class consists of data members and member functions.

1. Single Inheritance –

Single Inheritance This is the first and easiest type of Inheritance.

In Single Inheritance, the properties of a Base Class are inherited from a derived class.

Inheritance supports the concept of classification.

Example –


#include<iostream.h>
#include<conio.h>
class base    //single base class
{
   public:
     int x;
   void get()
   {
     cout << “Enter the value of x = “;
      cin >> x;
   }
 };
class derive : public base    //single derived class
{
   private:
    int y;
   public:
   void read()
   {
cout << “Enter the value of y = “;
      cin >> y;
   }
   void pro()
   {
     cout << “value of x * y = ” << x * y;
   }
 };
 
 void main()
 {
 clrscr();
    derive a;     //object of derived class
    a.get();
    a.read();
    a.pro();
    getch();
 }         //end of program
note:- this program run in Turbo C++

Output –

2. Multilevel Inheritance –

Multilevel Inheritance consists of one base class and two derived classes.

In multilevel inheritance, the derived class has a base class.

The derived class more has a derived class.

Example –


#include<iostream.h>
#include<conio.h>
class base //single base class
{
          public:
          int x;
          void get()
          {
          cout << “Enter value of x= “;
          cin >> x;
          }
};
class derive1 : public base // derived class from base class
{
          public:
          int y;
          void read()
          {
              cout << “\nEnter value of y= “;
               cin >> y;
          }
};
class derive2 : public derive1   // derived from class derive1
{
          int z;
          public:
          void in()
          {
          cout << “\nEnter value of z= “;
           cin >> z;
          }
          void pro()
          {
              cout << “\n x * y * z = ” << x * y * z;
          }
};
void main()
{
clrscr();
     derive2 a;      //object of derived class
     a.get();
     a.read();
     a.in();
     a.pro();
    getch();
}

Output –

3. Multiple Inheritance –

various class is inherited by a class.

That inheritance is called multiple inheritance.

The property of various class is inherited by one class.


Example –


#include<iostream.h>
#include<conio.h>
class A
{
          public:
          int x;
          void getx()
    {
              cout << “enter value of x: “;
              cin >> x;
    }
};
class B
{
          public:
          int y;
          void gety()
          {
              cout << “enter value of y: “;
              cin >> y;
          }
};
class C : public A, public B   //C is derived from class A and class B
{
          public:
          void sum()
          {
              cout << “Sum = ” << x + y;
          }
};
 
void main()
{
           clrscr();
           C obj1; //object of derived class C
           obj1.getx();
           obj1.gety();
           obj1.sum();
           getch();
}        //end of program

Output –

4. Hierarchical Inheritance –

Hierarchical Inheritance consists of a base class and multiple derived classes.

Example –


#include <iostream.h>
#include<conio.h>
 
class A //single base class
{
    public:
          int x, y;
          void get()
          {
              cout << “\nEnter value of x and y:\n”;
               cin >> x >> y;
          }
};
class B : public A //B is derived from class base
{
    public:
          void pro()
          {
              cout << “\nmul= ” << x * y;
          }
};
class C : public A //C is also derived from class base
{
    public:
          void sum()
          {
          cout << “\nSum= ” << x + y;
          }
};
void main()
{
clrscr();
    B obj1;          //object of derived class B
    C obj2;          //object of derived class C
    obj1.get();
    obj1.pro();
    obj2.get();
    obj2.sum();
    getch();
}  //end of program

Output –

note:- this program run in Turbo C++

5. Hybrid Inheritance –

It is ‘Virtual Inheritance’.

This inheritance is a combination of many inheritance.

It consists of two or more inheritance.

Example –


 #include <iostream.h>
#include<conio.h>
 
class A
{
          public:
          int x;
};
class B : public A
{
          public:
          B()      //constructor to initialize x in base class A
          {
             x = 10;
          }
};
class C
 {
          public:
          int y;
          C()   //constructor to initialize y
          {
              y = 4;
          }
};
class D : public B, public C   //D is derived from class B and class C
{
          public:
          void sum()
          {
              cout << “Sum= ” << x + y;
          }
};
 
void main()
{
             clrscr();
           D obj1;          //object of derived class D
          obj1.sum();
    getch();
}                  //end of program

Output –

note:- this program run in Turbo C++

5. Polymorphism –

polymorphism is a word from the Greek language in which poly means many and morphism means forms. polymorphism means many forms.

it means multiple forms that allow you to use a single interface for performing multiple actions in a program.

Polymorphism helps reduce complexity in programming by allowing you to perform common operations using the same function name.

The compiler selects the type of function that needs to be called to perform the specific task.

There are two types of polymorphism

  1. Compile-time Polymorphism
  2. Run-time Polymorphism
There are two types of compile-time Polymorphism.
  1. Function Overloading
  2. Operator Overloading
1.Function Overloading

Compile-time/Static Binding In C ++.

If any class has multiple functions with the same name

Different parameters they called overloaded.

Function overloading allows you to use the same name for different functions, to perform the same or different functions in the same class.

Example –

#include<iostream.h>
#include<conio.h>
class over
{
public:
void sum(int A, int B);
/* three different sequence of parameters */
void sum(int A, int B, int C);
void sum(int A, int B, int C, int D);
};
void over::sum(int A, int B)
{
cout<< “\n SUM is : “<< A+B;
}
void over::sum(int A, int B, int C)
{
cout<<“\n SUM is : “<< A+B+C;
}
void over::sum(int A, int B, int C, int D)
{
cout<<“\n SUM is : “<< A+B+C+D;
}
void main()
{
clrscr();
over ob; ob.sum(1,2); ob.sum(1,2,3); ob.sum(1,2,3,4);
getch();
}
note:- this program run in Turbo C++

Output –

2. Operator Overloading –

Compile-time/Static Binding/Early Binding C ++ Operator has types of Arithmetic, Logical, Conditional, etc. Operators.

Every operator of C ++ has different work.

example –

+: Addition of two numbers.

*: Multiplication of two numbers.

In Operator Overloading, these operators can be defined in some other way.

These operators are used as a function name with the keyword ‘operator’.

If the user overloads the + Operator, he can use his first definition of the same program.

Example –

#include<iostream.h>
#include<conio.h>
class demo
{
int a,b,c;
public:
void scan(int x,int y,int z);
/* Operator Overloading */
void dis();
void operator -();
};
void demo::scan(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
void demo::dis()
{
cout<<“a:”<<a;
cout<<“a:”<<b;
cout<<“c:”<<c;
}
void demo::operator -()
{
a=-a;
b=-b;
c=-c;
}
void main()
{
demo d;
d.scan(10,20,30);
-d;
d.dis();
getch();
}
note:- this program run in Turbo C++

Output –

Run-time is the same type of polymorphism.

1. Virtual Function(function overriding) –

Run-time/Dynamic Binding/Late Binding In the given program, we have taken a base class and a new derived class from it and created a pointer of base class and object of a derived class.

Example –

#include<iostream.h>
#include<conio.h>
class manager {
    int id;
public:
    void getnumber() {
          cout << “Enter manager id:”;
          cin>>id;
    }
 
    void putnumber() {
          cout << “\n\n\t manager id:” << id << “\n”;
    }
};
 
class test : virtual public manager {
public:
    int sales, profit;
    void getmarks() {
          cout << “Enter Marks\n”;
          cout << “sales:”;
          cin>>sales;
          cout << “profit:”;
          cin>>profit;
    }
 
    void putmarks() {
          cout << “\tMarks Obtained\n”;
          cout << “\n\tsales:” << sales;
          cout << “\n\tprofit:” << profit;
    }
};
 
class target : public virtual manager {
public:
    int score;
 
    void getscore() {
          cout << “Enter target Score:”;
          cin>>score;
    }
 
    void putscore() {
          cout << “\n\t target Score is:” << score;
    }
};
 
class result : public test, public target {
    int total;
public:
 
    void display() {
          total = sales + profit + score;
          putnumber();
          putmarks();
          putscore();
          cout << “\n\tTotal Score:” << total;
    }
};
 
void main() {
    result obj;
    clrscr();
    obj.getnumber();
    obj.getmarks();
    obj.getscore();
    obj.display();
    getch();
}

Output –

6. Dynamic Binding –

In oops inheritance and polymorphism implement this feature, which is called the binding or linking process. This is called dynamic binding.

Binding is the process of linking a function call to execute code.

read also – data type

Example –

#include<iostream.h>
#include<conio.h>
 
class binding
{
          public:
 
          int sum(int x, int y)
          {
                   return x + y;
          }
 
          int sum(int x, int y, int z)
          {
                   return x + y + z;
          }
};
 
void main()
{
 clrscr();
  binding ob;
          cout << “Sum is ” << ob.sum(10, 20) << “\n”;
          cout << “Sum is ” << ob.sum(10, 20, 30) << “\n”;
 
          getch();
}

Output –

7. Message Passing –

Objects in OOPs communicate. Communicate by sending and receiving objects information.

In which the information is input into the object through a function in the form of a function argument and the method of the object gives it the process to generate the result.

A program consists of objects that communicate with each other. You create classes that consist of properties and functions.

The message passing involves three things- the name of the object,

The name of the Function, and information to be sent.

An object can send and receive a message and it is destroyed.

source –geeksforgeeks

Conclusion –

Oop all concepts class, object, encapsulation, data abstraction, Inheritance, Polymorphism, Dynamic Binding, Message Passing  all of the types, Syntax, examples, outputs I gave you above

Friends, reading this post completely, you must have understood the concept of oop in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently asked questions

1. What is c++?

C ++ is a Medium Level Programming Language. Developed at Bell Laboratories in the early 1980s, the C ++ programming language was developed by Bjarne Stroustrup. This programming language is based on the ‘C Programming Language’. It is an advanced form of the already available C programming language, which we know as the C ++ programming language.

2. Advantages of c++?

In C++ we can write the  Code into the Classes So that it is called  OOP  Languages.
 In  Classes, we can create many Objects.
 C++ is both Compiled and Interpreter Languages.

3. What is oops?

OOP is a programming paradigm that uses the new concepts of structured
programming and eliminates the limitations of procedural programming approach.

4. What is a class?

A class is a collection of similar types of objects that share common attributes and behavior. Objects are the variables of the user-defined type called class.

5. What is an object?

an object is a real-world run-time entity in OOP that has some attributes and behavior such as person, place, and vehicle.

6. What is inheritance?

Inheritance is the process of defining new classes by extending the properties of other classes.

7. Types of inheritance?

Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchical Inheritance
Hybrid Inheritance

8. What is encapsulation?

In C++, the technique of binding the data and functions together in a single unit is called encapsulation.

9. What is Polymorphism?

Polymorphism means multiple forms that allow you to use a single interface for performing multiple actions in a program.

10. Types of Polymorphism?

Compile-time Polymorphism (Early binding)
Run-time Polymorphism (Late binding)

10. What is Dynamic Binding?

Binding is the process of linking a function call to execute code.

11. What is Message Passing?

 The program consists of objects that communicate with each other

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version