Sie sind auf Seite 1von 10

OOPS LAB

NOTE: PLEASE WRITE THE ANSWERS IN SEPARATE SHEET & BRING IT TO OOPS INTERNAL LAB EXAM.
OOPS programs 1. Write a C++ program to perform linear search. 2. Write a C++ program to find whether given triangle is isosceles or not. 3. Write a C++ program which calculates grade of every student. 4. Write a C++ program to calculate area & perimeter of rectangle by getting length & breath as input using classes. 5. Write a C++ program to add two complex nos to produce resultant using classes. 6. Write a C++ program to design a calculator using class. 7. Calculating Payroll of all employees using proper usage of constructors 8. Add, Sub (Calculator using classes). 9. Add two distance object using class & object.

Answers 1. Write a C++ program to perform linear search. #include<iostream.h> #include<conio.h> Void main() { Int a[100],n,ser; Clrscr(); Cout<<\n Enter The Size of the value:; Cin>>n; Int i; For(i=0;i<n;i++) {

Cout<<\n Enter A[<<i<<] : ; Cin>>a[i]; } Cout<<\n Enter The value to be search in the given array :; Cin>>ser; Int b=0; For(i=0;i<n;i++) { If(a[i]==ser) { b=1; } } If(b==0) { Cout<<\n value not found; }else{ Cout<<\n Value found ; } Getch(); }

2. Write a C++ program to find whether given triangle is isosceles or not. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); int a,b,c,angle; cout<<"\n Enter the three sides="; cin>>a>>b>>c; if(a!=b&&b==c) OR (a==b||b==c||c==a) { cout<<"\n Isoceles Triangle."; } Else { cout<<"\n Given triangle is not a Isoceles Triangle."; } Getch(); }

Explanation : Isosceles property:

In an isosceles triangle, two sides are equal in length. (Traditionally, only two sides equal, but sometimes at least two.) An isosceles triangle also has two equal angles: the angles opposite the two equal sides.

Figure;

3. Write a C++ program which calculates grade of every student. Range >90 90<to>80 80<to>70 70<to>60 <60 #include<iostream.h> #include<conio.h> Void main() { Cout<<\n Enter the name of the student :; Cin>>name; Cout<<\n Enter English Mark : ; Cin>>m1; Cout<<\n Enter Tamil Mark : ; Cin>>m2; O C B A E Grade

Cout<<\n Enter Maths Mark : ; Cin>>m3; Cout<<\n Enter Science Mark : ; Cin>>m4; Cout<<\n Enter Social Science Mark : ; Cin>>m5; Tot=m1+m2+m3+m4+m5; Avg=tot/5; If(avg>90) { Cout<<\n Grade is O; } If(avg<90||avg>80) { Cout<<\n Grade is C; } If(avg<80||avg>70) { Cout<<\n Grade is B; } If(avg<70||avg>60) { Cout<<\n Grade is A; } If(avg<60) { Cout<<\n Grade is C; } Getch(); }

4. Write a C++ program to calculate area & perimeter of rectangle by getting length & breath as input using classes. Explanation If a rectangle has length l and width b

it has area A = lb perimeter P = 2l +

2b = 2(l + b)

program :

#include<iostream.h> #include<conio.h> Class rect { Int l,b; Int area; Int peri; Public: Void get_rect() { Cout<<\n Enter length and breath of the rectangle :; Cin>>l>>b; } Void calc() { area=l*b; peri=2*l+2*b; // 2(l*b); } Void display() { Cout<<\n The area of the rectangle is :<<area; Cout<<\n The perimeter of the rectangle is :<<peri; } }; Void main() { Clrscr(); rect a; cout<<\n Area and perimeter of the rectangle:; a.get_rect(); a.calc(); a.display(); getch(); } 5. Write a C++ program to add two complex nos to produce resultant using classes. Explanation Source: http://www.analyzemath.com/complex/complex_numbers.html #include<iostream.h> #include<conio.h> Class complex

{ Float real1, ima1,real2,ima2; Float res_real, res_ima; Public: Void get_complex() { Cout<<\n Enter 1st complex no (2+3i should given as 2 3):; Cin>>real1>>ima1; Cout<<\n Enter 2nd complex no (2+3i should given as 2 3):; Cin>>real2>>ima2; } Void calc() { res_real=real1+real2; res_ima=ima1+ima2; } Void display() { Cout<<\n Resultant complex no : Cout<<\n Real part is : <<res_real; Cout<<\n Imaginary part is :<<res_ima; Cout<<\n Complex no is : <<real<<+<<ima<<i; } }; Void main() { Clrscr(); Complex c; c.get_complex(); c.calc(); c.display(); } 6. Write a C++ program to design a calculator using class. Source : #include <iostream> #include <iomanip> #include <cmath> using namespace std; class calc { double newEntry;

double displayedVal; public: double doDivideZero(double &) { newEntry =0; displayedVal = 0.0;

if (newEntry !=0) { displayedVal = displayedVal / newEntry; } else cout << "Wrong Operation, Cannot Divide by Zero" << endl;

return 0; } };

Void main() { double displayedVal; double newEntry; char command_character ; displayedVal = 0.0; calc c; cout << " Enter accepted Operator:" ; cin >> command_character; while (command_character != 'Q' || command_character != 'q') { switch(command_character) { case 'c': case 'C': displayedVal = 0.0; break; case '+': cout << " Enter Number:";

cin >> newEntry; displayedVal = displayedVal + newEntry; break; case '-': cout << " Enter Number:"; cin >> newEntry; displayedVal = displayedVal - newEntry; break; case '*': cout << " Enter Number:"; cin >> newEntry; displayedVal = displayedVal * newEntry; break; case '/': cout << " Enter Number:"; cin >> newEntry; displayedVal = displayedVal / newEntry; if (newEntry == 0) { c.doDivideZero(double &); } break; case '^': cout << " Enter Number:"; cin >> newEntry; displayedVal = pow (displayedVal,newEntry); break; default : cout << " Unacceptable Operator(" << command_character << ")" << endl; } cout << " The result so far is: " <<displayedVal<< endl; cout << " Enter Operator:"; cin >> command_character; }

Getch(); } Calculate area & Perimeter of circle using all kinds of constructor & destructor. Explanation The area enclosed by a circle is multiplied by the radius squared

Area = *r*r Perimeter of circle C=

2r

#include<iostream.h> #include<conio.h> Class circle { Float r; Float area,peri; Public: Circle() { r=10; } Circle(int a) { r=a; } Void calc() { Area=3.14*r*r; Peri=2*3.14*r; } Void display() { Cout<<\n area of cricle :<<area; Cout<<\n Perimeter of circle :<<peri; } ~circle() { Delete r; Delete area; Delete peri; } }; Void main() { Circle c; Cout<<\n Constructors Without Parameters ;; c.calc(); c.display(); Circle c2(6.5); Cout<<\n Constructors with parameters :;

C2.calc(); C2.display(); } These are the specific programs given by madam. Remaining 3 programs are derived in conduct classes.

Das könnte Ihnen auch gefallen