OBJECT ORIENTED PROGRAMMING WITH C++



1.     // Write a C++ program to illustrate reference variable //
#include <iostream.h>
#include<conio.h>
Void main()
{
int total=100;
int &sum=total;
clrscr();
cout<<”total=”<<total<<”\n”;
cout<<”sum=”<<sum<<”\n”;
total=total+100;
cout<<”total=”<<total<<”\n”;
cout<<”sum=”<<sum<<”\n”;
sum=sum+100;
cout<<”total=”<<total<<”\n”;
cout<<”sum=”<<sum<<”\n”;
getch();
}


Output:
total=100
sum=100
total=200
sum=200
total=300
sum=300

 2.      //Write a C++ program to create a dynamic array of type int and of any size to store and access elements of it and to find sum and to deallocate or release memory created//

#include<iostream.h>
#include<conio.h>
{
int size;
int *ptr=new int[size];
int s=0,i;
clrscr();
cout<<”Enter the size of array”<<”\n”;
cin>>size;
cout<<”Enter”<<size<<”elements”<<”\n”;
for(i=0;i<size;i++)
{
cin>>*(iptr+i);
}
for(i=0;i<size;i++)
{
s=s+*(iptr+i);
}
cout<<”array elements are:”<<”\n”;
for(i=0;i<size;i++)
{
cout<<*(iptr+i)<<” “;
}
cout<<”sum of array elements=”<<s<<”\n”;
delete[]iptr;
getch();
}

Output:
Enter the size of array
4
Enter 4 elements
20 40 60 80
Array elements are:
20 40 60 80 sum of array elements=200


3.      //Write a C++ program to read a number and to check whether it is palindrome or not//


#include<iostream.h>
#include<conio.h>
void main()
{
int  n,rev=0,d,m;
clrscr();
cout<<”Enter a number:”<<endl;
cin>>n;
m=n;
do
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
While(n>0);
if(rev==m)
cout<<”Given number is palindrome”<<endl;
else
cout<<”Given number is not palindrome”<<endl;
getch();
}
 
          Output:
Enter a number
3443
Given number is palindrome

Enter a number
143
Given number is not palindrome

4.     //Write a C++ program to find sum of N natural numbers using while statement//

#include<iostream.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
cout<<”Enter the value of n”<<”\n”;
cin>>n;
i=1;
while(i<=n)
{
sum=sum+i;
i++;
}
cout<<”The sum of first N natural number is=”<<sum<<”\n”;
getch();
}

Output:

Enter the value of n
5
The sum of first N natural numbers is=15


Enter the value of n
9
The sum of first N natural number is=45

5.     // Write aC++ program to interchange the values of two variables without using a third variable. //

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<”Input First number:”<<endl;
cin>>a;
cout<<”Input Second number:”<<endl;
cin>>b;
cout<<”Values before swap”<<endl;
cout<<”Values  of First number=”<<a<<endl;
cout<<”values of Second number=”<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<”Values after swap”<<endl;
cout<<”Values of First number”<<endl;
cout<<”Values of Second number”<<endl;
getch();
}

Output:

Input First number: 25
Input Second number: 9
Values before swap
Value of First number=25
Value of Second number=9
Values after swap
Value of First number=9
Value of Second number=25


6.     //Write a C++ program to calculate simple interest and compound interest //

#include<iostream.h>
#include<math.h>
Class Interest
{
private:
float principal, time,rate;
public:
void getdata()
{
cout<<”Input principal amount:”<<principal;
cout<<”Input number of years:”<<time;
cout<<”Input rate of interest:”<<rate;
}
float simpleInteres()
{
return(principal*time*rate/100)
}
float compoundInterest()
{
float amount=principal*pow(1+rate/100,time);
return(amount-principal);
}
void displayInfo()
{
cout<<”Simple Interest=”<<simpleInterest()<<endl;
cout<<”Compound Interest=”<<compoundInterest()<<endl;
}
};
Void main()
{
Interest sici
sici.getdata();
sici.displayInfo();
}
Output:
Input principal amount: 1000
Input number of years: 2
Input rate of interest: 12
Simple Interest = 2400
Compound Interest = 2544


7.     //Write a C++ program to  check a number is even or odd using friend function //

#include<iostream.h>
Class Number
{
private: int a;
public: void ReadData()
{
Cout<<”Input anumber”;
Cin>>a;
}
friend int IsEven(Number);
};
int IsEven(Number N)
{
If(N.a%2==0)
return 1;
else
return 0;
}
void main()
{
Number Num1;
Num1.ReadData();
if(IsEven(Num1))
cout<<”Number is even”;
else
cout<<”Number is odd”;
}


Output:

Input a number  13
Number is odd


Input a number 12
Number is even



8.     //Write a C++ program to generate a series of Fibonacci numbers using the constructor.//

#include<iostream.h>
#include<conio.h>
class Fibonacci
{
private: int FFirst,FSecond,FNext;
public: Fibonacci()
{
FFirst=0;
FSecond=1;
Cout<<FFirst<<”\t”<<FSecond;
}
void NextFiboNum()
{
FNext=FFirst+FSecon;
Cout<<”\t”<<FNext;
FFirst=FSecond;
FSecond=FNext;
}
};
Void main()
{
int n,i;
clrscr();
cout<<”Input number of elements in the series:”;
cin>>n;
cout<<”\n\nFibonacci numbers are:”<<endl;
Fibonacci  f1;
for(i=3;i<=n;i++)
f1.NextFiboNum();
getch();
}



Output:

Input number of elements in the series: 5
Fibonacci numbers are:
0
1
1
2
3

9.     // Write a C++ program to find GCD and LCM of two numbers using recursion. //

#include<iostream.h>
#include<conio.h>
void main()
{
int gcd(int,int)
int a,b,G,L;
clrscr();
cout<<”Enter two numbers”<<”\n”;
cin>>a;
cin>>b;
G=gcd(a,b);
L=(a*b)/G;
cout<<”GCD of “<<a<<”,”<<b<<”=”<<G<<”\n”;
cout<<LCM of”<<a<<”,”<<b<<”=”<<L<<”\n”;
}
int gcd(int a, int b)
{
int r;
if(b==0)
return(a);
else
{
r=a%b;
return(gcd(b,r));
}
getch();
}

Output:

Enter two numbers
7 4
GCD of 7,4 =1
LCM of 7,4=28

10.                        //Write a C++ program to illustrate virtual function.//

#include<iostream.h>
#include<conio.h>
class  bc
{
publis: int l,b;
int l,b;
virtual void read(int fl,int fb)
{
Cout<<”READING OF BASE  VERSION”<<”\n”;
l=fl;
b=fb;
}
Virtual void display()
{
Cout<<”BASE VIRTUAL FUNCTION DISPLAY<<”\n”;
Cout<<”the value of l=”<<l<<”\n”;
Cout<<”the value of b=”<<b<<”\n”;
}
};
Class dc: public bc
{
Public:
Void display()
{
Cout<<”DERIVED VERSION DISPLAY”<<”\n”;
}
};
Void main()
{
bc obc;
dc odc;
clrscr();
bc*bptr=&obc;
bptr->read(10,20);
bptr->display();
bptr=&odc;
bptr->read(30,40);
bptr->display();
getch();
}


Output:

READING OF BASE VERSION
BASE VIRTUAL FUNCTION DISPLAY
The value of l=10
The value of b=20
READING OF BASE VERSION
DERIVED VERSION DISPLAY


11.                        //Write  a C++ program to overload decrement operator. //

#inlcue<iostream.h>
#include<conio.h>
class numb_count
{
private: int count;
public: void get_data(int cval)
{
count=cval;
}
void display()
{
cout<<”count=”<<count<<”\n”;
}
void operator—()
{
count--;
}
};
void main()
{
numb_coutn num1;
clrscr();
num1.get_data(10);
cout<<”before overloading --…..”<<”\n”;
num1.display();
--num1;
cout<<”after overloading--…..”<<”\n”;
num1.display();
getch();
}

Output:

before overloading--….
count=10
after overloading--….
count=9

12.// Write a C++ program to computes the square of a given number using inline function. //

#include<iostream.h>
#include<conio.h>
inline float square(const float x)
{
return(x*x);
}
void main()
{
float num, sqr;
clrscr();
cout<<”Enter a number”<<endl;
cin>>num;
sqr=square(num);
cout<<”square of”<<num<<”is “<<sqr<<endl;
}


Output:

Enter a Number
25
Square of 25 is 625


13.// Write a C++ program to illustrate  static data members//

#include<iostream.h>
#include<conio.h>
class Item
{
private: static int count;
int numb;
public: void readData(int ival)
{
numb=ival;
count=count+l;
}
void displayCount(void)
{
cout<<”count=”;
cout<<”count<<endl;
}
};
int Item::count;
void main()
{
clrscr();
Item x,y,z;
x.displayCount();
y.displayCount();
z.displayCount();
x.readData(43);
y.readData(47);
z.readData(32);
cout<<”After Inputing values”<<endl;
x.displayCount();
y.displayCount();
z.displayCount();
}


Output:

count=0
count=0
count=0
After Inputting values
count=3
count=3
count=3

14.// Write a C++ program to illustrate class templates.//

#include<iostream.h>
Template<class T>
class numbers
{
private: T n1,n2,total;
public:void getdata();
void sum2();
};
template<class T>
void numbers<T>::getdata()
{
cout<<”enter two numbers”<<endl;
cin>>n1>>n2;
}
Template<class T>
Void numbers<T>::sum2()
{
T total;
total=n1+n2;
cout<<n1<<”+”<<n2<<”=”<<total<<endl;
}
void main()
{
numbers<int>iob;
numbers<float>fob;
cout<<”Integer case___”<<endl;
iob.getdata();
iob.sum2();
cout<<”Floating case___”<<endl;
fob.getdata();
fob.sum2();
}
Output:

Integer case___
Enter two numbers
45
20
45+20=65
Floating case___
Enter two numbers
3.2
6.7
3.4+6.7=10.1

 15.// Write a C++ program  to find the sum and average of “N” numbers. //

#include<iostream.h>
#include<conio.h>
void main()
{
int n,a[100], i, sum;
float average;
clrscr();
cout<<”Input number of elements”;
cin>>n;
cout<<”Input array elements”<<endl;
for(i=0;i<n;i++)
cin>>a[i];
sum=0;
for(i=0;i<n;i++)
sum=sum+a[i];
average=sum/n;
cout<<”endl<<”Sum of”<<n<<”numbers=”<<sum;
cout<<”endl<<”Average of”<<n<<numbers=”<<average;
getch();
}
Output:


Input number of elements 6
Input array elements
23    6   11    24   5   30
Sum of 6 numbers=99
Average of 6 numbers=16.5


Comments

Popular posts from this blog

Chemical test for Tragacanth

Chemical test for Benzoin

Chemical test for Agar/Agar-Agar / Japaneese Isinglass