Object Oriented Programming approach in C++

 

C++ was developed by Bjarne Stroupstrup by adding OOPs concepts in C and called as C with OOPs which was later changed to C++.

 

What is OOPs?

 

OOPs stands for Object Oriented Programming System which is a system or methodology for Better Project Management using some components called pillars of OOPs

These components are

 

  1. Encapsulation
  2. Abstraction
  3. Polymorphism
  4. Inheritance

 

Encapsulation

Place all the fields and methods related with an entity at one place and none of the members should be accessible outside without permission.

 

Use class keyword to build the encapsulation

 

class <classname>

{

            //members

}

 

All members inside a class are private by default.

 

Abstraction

 

Abstraction provides controlling by define scope on the member of a class. C++ provides three kinds of scopes

 

  1. Private (default)
  2. Public
  3. Protected

 

To define different sections inside a class use private:, public: and protected:

 

 

Members inside a class

 

A class can have different members

 

  1. field      -- data member
  2. method – function member

 

Methods again can be of different types

 

  1. Special methods
  2. General methods

 

Special methods again can be of two types

 

  1. Constructor
  2. Destructor

 

Constructor

 

  • A method inside a class having some features
    • Same name as class
    • No return type
    • Used to initialize fields of an object
    • Constructor can be overloaded

 

Passing data to the fields of an object : Data can be passed using a constructor or using a method.

 

Let's us see that with the help of an example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream.h>  // For input/output operations
#include<conio.h>    // For clrscr() function
class number // We declare a class with name number
{
    int n; // Class variable whose value we will set by method or coonstructor
    public:
        number() //Constructor without any parameters
        {
            n=0;
        }
        number(int num) //Constructor overloading, object will be needing a parameter
        {
            n=num;
        }

        void setNumber(int num) // Setting value of n using method
        {
            if(num<1)
            {
                cout << "Number must be greater than 0" << endl;
                return;
            }
            n=num; // We can set value of n by this way without using constructor
        }

        void square() //Method that calculates the square of  ā€˜nā€™
        {
            if(n==0)
            {
                cout << "Sorry! No data provided" << endl;
                return;
            }

            cout << "Square of " << n << " is " << n*n << endl;
        }

        void cube() //Method that calculates the cube of ā€˜nā€™

        {
            if(n==0)
            {
                cout << "Sorry! No data provided";
                return;
            }
            cout << "Cube of " << n << " is " << n*n*n << endl;
        }

// Note : Both square and cube can be called irrespective of the case how value of n is set.
}; // Notice the semicolon (it is not needed in Java or .net)


void main()  // Here also everything starts from main method
{
    clrscr();
    int a,b;
    cout << "Enter two numbers : ";
    cin >> a >> b;
    number x,y; //objects of number class
    x.setNumber(a);
    x.square();
    x.cube();
    y.setNumber(b); //passing data using method
    y.square();
    y.cube();
    number z(8); //passing data using constructor
    z.cube();
}