Saturday 15 November 2014

OOP THROUGH C++ ASSIGNMENT 3 QUESTIONS

1. Explain Classes in c++?
2. Explain Defining Member functions?
3. Explain Data Hiding?
4. Explain the following?
    a) static Member Variables
    b) static Member Functions
    c) static Object
5. Explain friend Functions?
6. Explain friend Classes?

Friday 7 November 2014

Write a C++ program to check whether the given number is even or odd (using ? : ternary operator )

#include<iostream>

int main()

{

int a;

cout<<"Enter a Number : ";

cin>>a;

(a%2==0)?cout<<"Number is even":cout<<"Number is odd";

cin.ignore();

cin.get();

return 0;

}

Write a C++ Program illustrating Function Overloading.



#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

#define pi 3.14

class fn

{

public:

void area(int); //circle

void area(int,int); //rectangle

void area(float ,int,int); //triangle

};

void fn::area(int a)

{

cout<<"Area of Circle:"<<pi*a*a;

}

void fn::area(int a,int b)

{

cout<<"Area of rectangle:"<<a*b;

}

void fn::area(float t,int a,int b)

{

cout<<"Area of triangle:"<<t*a*b;

}

void main()

{

int ch;

int a,b,r;

clrscr();

fn obj;

cout<<"\n\t\tFunction Overloading";

cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;

cout<<”Enter your Choice:";

cin>>ch;

switch(ch)

{

case 1:

cout<<"Enter Radius of the Circle:";

cin>>r;

obj.area(r);

break;

case 2:

cout<<"Enter Sides of the Rectangle:";

cin>>a>>b;

obj.area(a,b);

break;

case 3:

cout<<"Enter Sides of the Triangle:";

cin>>a>>b;

obj.area(0.5,a,b);

break;

case 4:

exit(0);

}

getch();

}

Write a C++ Program illustrating to sort integer numbers.

#include <iostream.h>
void main(void)
{
int t,i,j;

int n;

cout<<"Enter the number of values to be sorted ";

cin>>n;

int s[20];

cout<<"\nEnter  values ";

for(i=0;i<n;i++)

cin>>s[i];

cout<<endl;

cout<<"sorted list is ";

for(i=0;i<n;i++)

{

for(j=0;j<n-i;j++)

{

if (s[j]>s[j+1])

{

t=s[j+1];

s[j+1]=s[j];

s[j]=t;

}

}

cout<<endl;

cout<<"Pass "<<i+1<<" ";

for(int k=0;k<n;k++)

cout<<s[k]<<" ";

}

cout<<endl;

}

Write a C++ program illustrating interactive program for computing the roots of a quadratic equation by handing all possible cases use streams to perform I/O operations

#include<iostream.h>
#include<math.h>
int main()
{
float a,b,c,d,root1,root2;
cout<<"Enter value of a, b and c : ";
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d==0)
{
root1=(-b)/(2*a);
root2=root1;
cout<<"Roots are real & equal";
}
else if(d>0)
{
root1=-(b+sqrt(d))/(2*a);
root2=-(b-sqrt(d))/(2*a);
cout<<"Roots are real & distinct";
}
else
{
root1=(-b)/(2*a);
root2=sqrt(-d)/(2*a);
cout<<"Roots are imaginary";
}
cout<<"\nRoot 1= "<<root1<<"\nRoot 2= "<<root2;
getch();
return 0;
}