Object-oriented
programming (OOP) is
a programming model using objects. In Object-oriented programming language
model organized around "objects" and an object is instances of a class – consisting of data
fields and methods together.
In previous post, I
already given the notes on C++ and other OPPs principle like abstraction,
polymorphism, encapsulation and inheritance. Today I am giving some small
example of it.
A simple Example of OPPs program.
#include <stdio.h>
#include<iostream.h>
class
Sum
{
private:
int
a,b; //Private Data Members
public:
void
Input() //Methods
{
cout<<"\n\tEnter
two No : ";
cin>>a>>b;
}
void
Add()
{
cout<<"\n\tsum
of "<<a<<" and "<<b<<"
= "<<a+b<<endl;
}
}; //end of class
void
main()
{
Sum
S1; //Creating the Object
S1.Input();
//Calling the methods
S1.Add();
}
Example of Default Construtor
#include <stdio.h>
#include <iostream.h>
class
Sum
{
private:
int
a,b; //Private Data Members
public:
Sum() //using default constructor
{
a=50;
b=60;
}
void
Add()
{
cout<<"\n\tsum
of "<<a<<" and "<<b<<"
= "<<a+b<<endl;
}
};
void
main()
{
Sum S1; //Creating the Object that invoking
default constructor
S1.Add(); //Calling the methods
}
Example of Parameterize
Constructor
#include <stdio.h>
#include<iostream.h>
class
Sum
{
private:
int
a,b; //Private Data Members
public:
Sum() //using default constructor
{
a=50;
b=60;
}
Sum(int
x, int y) //Parameterize constructor
{
a=x;
b=y;
}
void
Add()
{
cout<<"\n\tsum
of "<<a<<" and "<<b<<"
= "<<a+b<<endl;
}
};
void
main()
{
Sum
S1; //Creating the Object that invoking default constructor
S1.Add(); //Calling the methods
Sum
S2(120,350); // Creating the Object for parameterize constructor
S2.Add(); //Calling the methods
}
No comments:
Post a Comment