Research Area

Ashutosh Dubey

Monday, May 17, 2010

Class and object

1: Write a program to create a class CIRCLE enter the radius of a circle and calculate the Area using two functions Input and Output.
#include<iostream.h>
#include<conio.h>
class circle


{
private:
float rad,area;
public:
void input(){
cout<<"Enter the radius: ";
cin>>rad;
}
void output()
{
area=2*(22.7*rad);
cout<<"Area of circle= "<<area;
}};
void main(){
clrscr();
circle o1;
o1.input();
o1.output();
getch();
}


2. Write a program to create a class STUDENT enter name, age and salary in Input function and print the output in output function.
#include<iostream.h>
#include<conio.h>
class student{
private:
char name[20];
int age,salary;
public:
void input()
{
cout<<"Enter Name,Age & Salary\n";
cin>>name;
cout<<endl;
cin>>age>>salary;
}
void output()
{
cout<<name<<"\n";
cout<<"Age:"<<age;
cout<<"\nSalary:"<<salary;
}};
void main(){
clrscr();
student o1;
o1.input();
o1.output();
getch();
}


3. Write a program to create a class STUDENT enter the name, age and phone no of 3 student in input function and print the output in output function.
#include<iostream.h>
#include<conio.h>
class student2
{private:
char name[20];
int age;
long phone;
public:
void input()
{
cout<<"Enter the Name,age&phone no.\n";
cin>>name;
cout<<endl;
cin>>age>>phone;
cout<<endl;
}
void output()
{
cout<<endl<<name;
cout<<"\nAge:"<<age;
cout<<"\nPhone No."<<phone;
}};


void main()
{
clrscr();
student2 o1[20];
int i;
for(i=0;i<3;i++)
{o1[i].input();
}
for(i=0;i<3;i++)
{
o1[i].output();
}
getch();
}


4. Write a program to create a class SUM enter 3 numbers in Input function, prints the sum in add function and print the average in avg function.


#include<iostream.h>
#include<conio.h>
class sum
{
private:
int a,b,c,total,average;
public:
void input()
{
cout<<"Enter the no's\n";
cin>>a>>b>>c;
}
void add(){
total=a+b+c;
cout<<"\nSum of no's:"<<total;
}
void avg()
{
average=total/3;
cout<<"\nAverage of no's:"<<average;
}};
void main()
{
clrscr();
sum o1;
o1.input();
o1.add();
o1.avg();
getch();
}


5. Write a program to create a class EMPLOYEE enters as many employee name and salary as user wants and print the values.


#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[20];
int sal;
public:
void input()
{
cout<<"\n\nEnter Employee Name:";
cin>>name;
cout<<"\nEnter the Salary:";
cin>>sal;
}
void output()
{cout<<"\n\nEmployee Name:"<<name;
cout<<"\nSalary ="<<sal;}};
void main()
{
clrscr();
int i,n;
employee o1[100];
cout<<"Enter the No. of Employee's:";
cin>>n;
for(i=0;i<n;i++)
{
o1[i].input();
}
for(i=0;i<n;i++)
{
o1[i].output();
}
getch();}

6. Write a program to create a class MAX input three values in Input function and print the output in output function.



#include<iostream.h>

#include<conio.h>

class max

{

private:
int a,b,c;
public:

void input()

{
cout<<"Enter the no's:\n";
cin>>a>>b>>c;
}
void compare()
{

if(a>b&&a>c)
cout<<"\nA is greater = "<<a;
else if(b>a&&b>c)

cout<<"\nB is greater = "<<b;
else
cout<<"\nC is greater = "<<c;
}};


void main()
{

clrscr();
max o1;

o1.input();
o1.compare();

getch();

}

7. Write a program to create a class FACT enter a number and print the factorial of the no.


#include<iostream.h>
#include<conio.h>

class fact

{
private:
int n,factorial,i;

public:
void input()

{
cout<<"Enter the no.:";
cin>>n;

}
void cal(){
factorial=1;
for(i=1;i<=n;i++)

factorial=factorial*i;

cout<<"Factorial of no. is="<<factorial;

}};

void main()

{

clrscr();

fact o1;

o1.input();
o1.cal();
getch();

}



8: Write a program to create a class FACTORIAL enter a number if the number is less than 5 print the factorial of the number if it is greater than 5 then print the value from 1 to that number(e.g1,2,3……..8).Use appropriate function required.


#include<iostream.h>
#include<conio.h>

class fact
{
private:

int n,factorial,i;

public:

void input()

{
cout<<"Enter the no.:";

cin>>n;
}

void cal()

{

if(n<=5)

{

factorial=1;

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

factorial=factorial*i;

cout<<"Factorial of no. is="<<factorial;

}
else

{

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

cout<<i;

}}};
void main()
{

clrscr();

fact o1;

o1.input();
o1.cal();
getch();
}

9: Write a program to create a class NUM enter a number, if the number is less than 10 print the table of the number otherwise check it is even or odd.
#include<iostream.h>

#include<conio.h>

class num

{
int no,tab ;
public:
void input()

{

cout<<"enter the number";
cin>>no;
}
void cal()
{

int i;
if(no<10)

{
for(i=1;i<=10;i++)
{
tab=no*i;
cout<<no<<"*"<<i<<"="<<tab<<"\n";

}
}
else

{
if(no%2==0)
{
cout<<"the no is even";

}
else
cout<<"no is odd";

}}

};
void main()

{
num o;
clrscr();

o.input();

o.cal();
getch();
}


10: Write a program to create a class SORTING ,create an integer Array accept 10 values in input function sort them and display in ascending order in output function.[use BUUBLE or SELECTION Sort].


#include<iostream.h>

#include<conio.h>

class sorting

{

int a[10],t,n;
public:
void input();
void output();
};

void sorting::input()
{cout<<"Enter the range of array:";
cin>>n;
cout<<"Enter the array\n";

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

cin>>a[j];

}
void sorting::output()
{
for(int i=0;i<n-1;i++)

{for(int j=i;j<n-i;j++)

if(a[j]>a[j+1])
{

t=a[j];
a[j]= a[j+1];
a[j+1]=t;}}}
cout<<"Array in increasing order (using selection sort)\n";
for(int y=0;y<n;y++)
cout<<a[y]<<"\t";
}
void main()

{ clrscr();

cout<"\t\t\t vijay verma";

sorting s;
s.input();
s. output();
getch();
}


By
Vijay  Verma
CSE
TITR Bhopal

No comments: