July 2018

Difference Between Procedure Oriented Programming (POP) & Object-Oriented Programming (OOP) | C++ Programming


Procedure Oriented ProgrammingObject-Oriented Programming
Divided IntoIn POP, the program is divided into small parts called functions.In OOP, the program is divided into parts called objects.
ImportanceIn POP, Importance is not given to data but to functions as well as the sequence of actions to be done.In OOP, Importance is given to the data rather than procedures or functions because it works as a real world.
ApproachPOP follows Top-Down approach.OOP follows Bottom-Up approach.
Access SpecifiersPOP does not have any access specifier.OOP has access specifiers named Public, Private, Protected, etc.
Data MovingIn POP, Data can move freely from function to function in the system.In OOP, objects can move and communicate with each other through member functions.
ExpansionTo add new data and function in POP is not so easy.OOP provides an easy way to add new data and functions.
Data AccessIn POP, the Most function uses Global data for sharing that can be accessed freely from function to function in the system.In OOP, data can not move easily from function to function,it can be kept public or private so we can control the access of data.
Data HidingPOP does not have any proper way for hiding data so it is less secure.OOP provides Data Hiding so it provides more security.
OverloadingIn POP, Overloading is not possible.In OOP, overloading is possible in the form of Function Overloading and Operator Overloading.
ExamplesExample of POP are : C, VB, FORTRAN, Pascal.Example of OOP are : C++, JAVA, VB.NET, C#.NET.

C++ Program For Banking System Using Class

#include <iostream>
using namespace std;
#include <iomanip>

class bank
{
char name[20];
int acno;
char actype[20];
int bal;
public :
void opbal(void);
void deposit(void);
void withdraw(void);
void display(void);
};

void bank :: opbal(void)
{
cout<<endl<<endl;
cout<<"Enter Name :-";
cin>>name;
cout<<"Enter A/c no. :-";
cin>>acno;
cout<<"Enter A/c Type :-";
cin>>actype;
cout<<"Enter Opening Balance:-";
cin>>bal;
}

void bank :: deposit(void)
{
cout<<"Enter Deposit amount :-";
int deposit=0;
cin>>deposit;
deposit=deposit+bal;
cout<<"\nDeposit Balance = "<bal=deposit;
}

void bank :: withdraw(void)
{
int withdraw;
cout<<"\nBalance Amount = "<<bal;
cout<<"\nEnter Withdraw Amount :-";
cin>>withdraw;
bal=bal-withdraw;
cout<<"After Withdraw Balance is "<<bal;
}

void bank :: display(void)
{
cout<<endl<<endl<<endl;
cout<<setw(50)<<"DETAILS"<<endl;
cout<<setw(50)<<"name "<<name<<endl;
cout<<setw(50)<<"A/c. No. "<<acno<<endl;
cout<<setw(50)<<"A/c Type "<<actype<<endl;
cout<<setw(50)<<"Balance "<<bal<<endl;
}

int main()
{
bank o1;
int choice;
do
{
cout<<"\n\nChoice List\n\n";
cout<<"1) To assign Initial Value\n";
cout<<"2) To Deposit\n";
cout<<"3) To Withdraw\n";
cout<<"4) To Display All Details\n";
cout<<"5) EXIT\n";
cout<<"Enter your choice :-";
cin>>choice;
switch(choice)
{
case 1: o1.opbal();
break;
case 2: o1.deposit();
break;
case 3: o1.withdraw();
break;
case 4: o1.display();
break;
case 5: goto end;
}
}while(1);
end:
return 0;
}

Output



Choice List

1)  Create a New Account
2)  To Deposit
3)  To Withdraw
4)  To Display All Details
5)  EXIT
Enter your choice :-1


Enter Name :-John
Enter A/c no. :-0012003322
Enter Opening Balance:-5000


Choice List

1)  Create a New Account
2)  To Deposit
3)  To Withdraw
4)  To Display All Details
5)  EXIT
Enter your choice :-2
Enter Deposit amount :-6000

Deposit Balance = 11000

Choice List

1)  Create a New Account
2)  To Deposit
3)  To Withdraw
4)  To Display All Details
5)  EXIT
Enter your choice :-3

Balance Amount = 11000
Enter Withdraw Amount :-4000
After Withdraw Balance is 7000

Choice List

1)  Create a New Account
2)  To Deposit
3)  To Withdraw
4)  To Display All Details
5)  EXIT
Enter your choice :-4



                                           DETAILS
                                        name      John
                                     A/c. No.     12003322
                                      Balance     7000


Choice List

1)  Create a New Account
2)  To Deposit
3)  To Withdraw
4)  To Display All Details
5)  EXIT
Enter your choice :-5

Constructor Overloading

When more than one constructor function is defined in a class, then it is called constructor overloading or the use of multiple constructors in a class. It is used to increase the flexibility of a class by having a greater number of constructors for a single class. Overloading constructors in C++ programming gives us more than one way to initialize objects in a class.

The Class Constructor

A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have the exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
Following example explains the concept of constructor −

#include 
using namespace std; 
class Line { public: 
void setLength( double len ); 
double getLength( void ); 
Line(); // This is the constructor 
private: 
double length;}; // Member functions definitions including constructorLine::Line(void) 
{ 
 cout << "Object is being created" << 
endl;
}
void Line::setLength( double len ) 
{ 
 length = len;}double Line::getLength( void ) 
{ 
return length;
}// Main function for the program
int main() 
{ 
Line line; // set line length 
line.setLength(6.0); 
cout << "Length of line : " << 
line.getLength() <<
endl; 
return 0;
}
When the above code is compiled and executed, it produces the following result −
Object is being created
Length of line : 6

Parameterized Constructor

A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign an initial value to an object at the time of its creation as shown in the following example −

#include 
using namespace std;
class Line 
{ 
public: 
void setLength( double len ); 
double getLength( void ); 
Line(double len); // This is the constructor 
private: 
double length;}; // Member functions definitions including constructorLine::Line( double len) 
{ 
 cout << "Object is being created, length = " << 
len << 
endl;
length = len;
}
void Line::setLength( double len ) 
{ 
 length = len;
}
double Line::getLength( void ) 
{ 
return length;
} 
/Main function for the program
int main() 
{ 
Line line(10.0); // get initially set length. 
 cout << "Length of line : " << 
line.getLength() <<endl; // set line length again line.setLength(6.0); 
cout << "Length of line : " << 
line.getLength() <<
endl; 
return 0;
}
When the above code is compiled and executed, it produces the following result −

Object is being created, length = 10
Length of line : 10
Length of line : 6


C++ Program for Constructor Overloading

#include<iostream>
using namespace std;
class item
{
int code,price;
public:
item()
{
code=0; price=0;
}
item(int c, int p)// Over Loading Constructor
{
code=c; price=p;
}
ite5m(item &x)
{
code=x.code; price=x.price;
}
void display()
{
cout<<"\ncode:"<<code<<"\nPrice:"< }
};
int main()
{
item i1;
item i2(102,300);//Passing Value
item i3(i2);
i1.display();
i2.display();
i3.display();

}

C++ Program To Find Negation Of A Number

#include<iostream>
using namespace std;
class point{
int x,y;
public:
void getdata()
{
cout<<"Enter x and y coordinate:";
cin>>x>>y;
}
void display()
{
cout<<"("<<x<<","<<y<<")";
}
point operator -()
{
point t;
t.x=-x;
t.y=-y;
return t;
}
};
int main()
{
point p,q;
p.getdata();
q=-p;
cout<<"q=";
q.display();
}

OUTPUT


Enter x and y coordinate:
5
4
q=(-5,-4)

Access Modifiers in C++

Access modifiers are constructs that define the scope and visibility of members of a class. There are three access modifiers:


Private

Private members are accessible only inside the class and not from any other location outside of the class.

Protected

Protected members are accessible from the class as well as from the child class but not from any other location outside of the class.

Public

Public members are accessible from any location of the program.
class Test

{ 
private:   int x;  public: int y;
void getdata()
{ 
cout<<”Enter x and y”<<endl; 
cin>>x>>y;
}
void display()
{ 
cout<<”x=”<<x<<”y=”<<y<<endl;
} }

void main()
{ 
clrscr(); 
Test p;
p.getdata(); 
cout<<”Enter  value of x”<<endl; 
cin>>p.x;
cout<<”Enter value of y”<<endl; 
cin>
>
p.y; 
getch();
}


Here the statement “cin>>p.x;” generates error because x is private data member because, x is private data member and is not accessible from outside of the class. But the statement “cin>>p.y;” does not generates error because y is public data member and is accessible from everywhere. Thus we can say that use of private access specifier is used to achieve data hiding in class.

Types of Inheritance


A class can inherit properties from one or more classes and from one or more levels.

On the basis of this concept, there are five types of inheritance.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance

Single Inheritance

In single inheritance, a class is derived from only one base class. The example and figure below show this inheritance.
Example

class A
{
 members of A
};
class B  :  public A
{
 members of B
};

Multiple Inheritance

In this inheritance, a class is derived from more than one base class. The example and figure below show this inheritance.
Implementation Skeleton:

class A
{
members of A
}; class B
{  members of B
};
class C  :
public A,
public B 
{
members of C
};

Hierarchical Inheritance

In this type, two or more classes inherit the properties of one base class. The example and figure below show this inheritance.
Implementation Skeleton:
class A
{
members of A
};
class B
{
members of B
};
class C  : 
public A,
public B 
{
members of C
};

Multilevel Inheritance

The mechanism of deriving a class from another derived class is known as multilevel inheritance. The process can be extended to an arbitrary number of levels. The example and figure below show this inheritance.
Implementation Skeleton:
class A
{
members of A
};
class B :
public A
{
members of B
};
class C  :
public B 
{
members of C
};

Hybrid Inheritance

This type of inheritance includes more than one type of inheritance mentioned previously. The example and figure below show this inheritance.
Example
class A
{
 members of A
};
class B :
public A
{
 members of B
};
class C  :
public A 
{
 members of C
};
class D :
public B, public C
{
 members of D
};