Oops Lab Manual I Scheme Programs
Oops Lab Manual I Scheme Programs
Oops Lab Manual I Scheme Programs
1 Operator Precedence
#include <iostream.h>
Int main() {
e = (a + b) * c / d;
e = ((a + b) * c) / d;
e = (a + b) * (c / d);
e = a + (b * c) / d;
return 0;
Output
Value of (a + b) * c / d is :90
Value of (a + b) * (c / d) is :90
Value of a + (b * c) / d is :50
#include <iostream.h>
#include <math.h>
int main()
{
return 0;
Output
x1 = -0.25
x2 = -1
#include <iostream.h>
int main()
int year;
cout << "Enter a year: ";
if (year % 4 == 0)
if (year % 100 == 0)
if (year % 400 == 0)
else
else
else
return 0;
Output
int main()
cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " <<
endl;
sum = 0;
num = i;
while(num > 0)
num=num/10;
if(sum == i)
{
return 0;
Output
153
370
371
#include <iostream.h>
int main()
n = num;
do
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
else
return 0;
Output
#include <iostream.h>
int main()
int n;
cin >> n;
cout << n << " * " << i << " = " << n * i << endl;
return 0;
Output
Enter an integer: 5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
#include <iostream.h>
int main()
int n;
return 0;
23
456
7 8 9 10
#include <iostream.h>
int main()
{
int rows, number = 1;
number++;
return 0;
**
* * *
* * * *
* * * * *
#include<iostream.h>
#include<conio.h>
int main()
int r,c,k,rows;
clrscr();
cin>>rows;
cout<<endl;
for(r=1;r<=rows;r++)
for(k=1;k<=rows-r;k++)
cout<<" ";
for(c=1;c<=r;c++)
cout<<" * ";
cout<<endl;
getch();
return 0;
}
#include <iostream.h>
#include<conio.h>
void main()
clrscr();
cin>>m;
cin>>a1[i];
cin>>n;
{
cin>>a2[i];
i = 0;
j = 0;
a3[k] = a1[i];
i++;
else
a3[k] = a2[j];
j++;
k++;
if (i >= m)
while (j < n)
{
a3[k] = a2[j];
j++;
k++;
if (j >= n)
while (i < m)
a3[k] = a1[i];
i++;
k++;
cout<<" "<<a3[i];
int len=m+n;
if (len % 2)
median = a3[mid] ;
cout << "\nThe median is: " << median << endl;
else
median = (a3[mid]+a3[mid-1])/2.0;
cout << "\nThe median is: " << median << endl;
getch();
#include<iostream.h>
#include<conio.h>
void main()
int i,arr[20],j,no;
clrscr();
for(i=0;i<no;i++)
cin>>arr[i];
for(j=i+1;j<no;j++)
if(arr[i]==arr[j])
cout<<"\n"<<arr[i];
getch();
Output
54523
EXP 4.3 program to find smallest and second smallest element in a given array
#include <iostream.h>
int main()
int arr[50], n;
cin>>n;
cin>>arr[i];
int small,next_small=INT_MAX;
small = arr[0];
for(int i=1;i<len;i++)
next_small = small;
small = arr[i];
next_small = arr[i];
}
cout << "Smallest and the second smallest numbers are respectively "<< small
<< " and " << next_small <<endl;
return 0;
#include <iostream.h>
int main()
int n=5,i;
int arr[4];
cin>>arr[i];
int temp;
temp -= arr[i];
return 0;
EXP6.1 Write a c++ program to declare a class book having data members
book_name, author and price. Accept and display data for book having maximum
price.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Book
int no_of_pages;
char book_name[50];
public:
float price;
void getdata()
cin>>book_name;
cin>>price;
cin>>no_of_pages;
}
void display()
cout<<"\nBook Price:->"<<price;
};
void main()
Book b1,b2;
clrscr();
b1.getdata();
b2.getdata();
if(b1.price>b2.price)
b1.display();
else
b2.display();
getch();
}
EXP 6.2 Write a c++ program to declare a class staff having data members name,
salary, DA, HRA and calculate gross salary. Accept and display data for one staff.
#include<iostream.h>
#include<conio.h>
class staff
float basic,gross_sal;
public:
void accept()
cin>>basic;
};
gross_sal=(basic*DA/100)+(basic*HRA/100)+basic;
return(gross_sal);
void main()
clrscr();
staff s1;
s1.accept();
getch();
#include<iostream.h>
#include<conio.h>
class Employee
int emp_id;
char emp_name[20];
public:
float emp_salary;
void accept()
cin>>emp_id;
cin>>emp_name;
cin>>emp_salary;
}
void display()
};
void main()
int i;
clrscr();
Employee e[5];
for(i=0;i<5;i++)
e[i].accept();
for(i=0;i<5;i++)
if(e[i].emp_salary>25000)
e[i].display();
getch();
}
#include<iostream.h>
#include<conio.h>
class City
char city_name[20];
public:
void accept()
cin>>city_name;
cin>>population;
void display()
cout<<"Population=> "<<population<<endl;
}
};
void main()
int i;
clrscr();
City c[10];
for(i=0;i<10;i++)
c[i].accept();
for(i=0;i<10;i++)
c[i].display();
getch();
#include<iostream.h>
#include<conio.h>
class swap
int a,b;
public:
void getab()
cin>>a;
cin>>b;
};
{ int t;
t=s1.a;
s1.a=s1.b;
s1.b=t;
void main()
clrscr();
swap s;
s.getab();
swapping(s);
getch();
EXP 8.2
#include<iostream.h>
#include<conio.h>
class DM
public:
DM()
cin>>distm;
};
void main()
clrscr();
DM d1;
DM d2
totaldist(d1,d2);
getch();
#include<iostream.h>
#include<conio.h>
class Number
int x,y;
public:
Number()
x=15;
y=5;
Number(int a,int b)
x=a;
y=b;
void display()
{
cout<<"\nx= " <<x<<"\t"<<"y= "<<y;
cout<<endl<<"Addition= "<<x+y<<endl;
cout<<"Subtraction= "<<x-y<<endl;
cout<<"Multiplication= "<<x*y<<endl;
cout<<"Division= "<<x/y<<endl;
~Number()
cout<<"\nMemory released";
};
void main()
clrscr();
Number n1;
Number n2(45,15);
n1.display();
n2.display();
getch();
}
EXP10.2 Product Details using Overloaded Constructors
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Product
int prod_id;
float prod_price;
char prod_name[25];
public:
Product()
prod_id=001;
prod_price=500;
strcpy(prod_name,"Photo Frame");
prod_id=i;
prod_price=p;
strcpy(prod_name,n);
Product(Product &pc)
prod_id=pc.prod_id;
prod_price=pc.prod_price;
strcpy(prod_name,pc.prod_name);
void displayprod()
~Product()
cout<<"\nMemory released";
};
void main()
clrscr();
Product p1;
Product p2(002,1000,"Fan");
p1.displayprod();
p2.displayprod();
cout<<"\nFor Product 3:";
Product p3(p1);
p3.displayprod();
getch();
Exp11.1
#include<iostream.h>
#include<conio.h>
class student
protected :
int rollno;
char name[25];
public:
void get();
void put();
};
cin>>rollno;
cout<<"\nName: "<<name;
protected:
int m1,m2,m3,total;
float average;
public:
void getmarks();
void putmarks();
};
cin>>m1;
cin>>m2;
cin>>m3;
}
put();
total=m1+m2+m3;
average=total/3;
void main()
marks S1;
clrscr();
S1.get();
S1.getmarks();
S1.putmarks();
getch();
}
Exp11.2
#include<iostream.h>
#include<conio.h>
class Employee
int emp_id;
char emp_designation[20];
char emp_name[25];
public:
void accept()
cin>>emp_id;
cin>>emp_name;
cin>>emp_designation;
void display()
};
class Salary:public Employee
float basic,gross_sal;
public:
void accept_salary()
accept();
cin>>basic;
void display_salary();
};
gross_sal=(basic*DA/100)+(basic*HRA/100)+basic;
display();
void main()
clrscr();
Salary s1;
s1.accept_salary();
s1.display_salary();
getch();
//Exp 12.1
#include<iostream.h>
#include<conio.h>
class Person
protected :
int age;
char name[25];
char gender;
public:
void get_info();
void put_info();
};
cout<<"Enter Name:-\t";
cin>>name;
cout<<"Enter age:-\t";
cin>>age;
cout<<"Enter Gender:-\t";
cin>>gender;
cout<<"\nName:-\t"<<name;
cout<<"\nAge:-\t"<<age;
cout<<"\nGender:-\t"<<gender;
protected:
int emp_id;
float salary;
char company[25];
public:
void get_emp_details();
void put_emp_details();
};
cin>>emp_id;
cin>>company;
cout<<"Enter Salary:-\t";
cin>>salary;
cout<<"\nEmployee Id:-\t"<<emp_id;
cout<<"\nCompany Name:-\t"<<company;
cout<<"\nSalary:-\t"<<salary;
int no_of_prog_lang_known;
public:
void accept()
cin>>no_of_prog_lang_known;
void display();
};
put_info();
put_emp_details();
cout<<"\nNo. of programming languages known:-
\t"<<no_of_prog_lang_known;
void main()
clrscr();
Programmer r1;
r1.get_info();
r1.get_emp_details();
r1.accept();
cout<<"\nDetails of programmer:\n";
r1.display();
getch();
//Exp 12.2
#include<iostream.h>
#include<conio.h>
class Car
protected :
char car_type[25];
public:
void get_car_type();
void put_car_type();
};
cout<<"Enter car_type:-\t";
cin>>car_type;
cout<<"\nCar Type:-\t"<<car_type;
protected:
int speed;
char brand_name[25];
public:
void get_brand_details();
void put_brand_details();
};
cin>>brand_name;
cout<<"Enter Speed:-\t";
cin>>speed;
cout<<"\nSpeed:-\t\t"<<speed;
char model_name[10];
public:
void get_model_details()
cin>>model_name;
cout<<"Enter Price:-\t";
cin>>price;
void put_model_details();
};
put_car_type();
put_brand_details();
cout<<"\nModel Name:-\t"<<model_name;
cout<<"\nPrice:-\t"<<price;
void main()
clrscr();
Model r1;
r1.get_car_type();
r1.get_brand_details();
r1.get_model_details();
cout<<"\nCar Details:\n";
r1.put_model_details();
getch();
//Exp 13.1
#include <iostream.h>
#include<conio.h>
class Area
public:
{
return l*b;
};
class Perimeter
public:
return 2*(l+b);
};
private:
public:
void get_data( )
cin>>length;
cin>>breadth;
}
void show()
cout<<"Area = "<<area(length,breadth);
cout<<"\nPerimeter = "<<perimeter(length,breadth);
};
int main()
clrscr();
Rectangle r;
r.get_data();
r.show();
getch();
return 0;
//Exp 13.2
#include<iostream.h>
#include<conio.h>
class Cricketer
char name[15];
int no_of_matches;
public:
void getinfo()
cin>>name;
cin>>no_of_matches;
void putinfo()
};
public:
void getruns()
cin>>no_of_runs;
}
void putruns()
};
int no_of_wickets;
public:
void getno_of_wickets()
cin>>no_of_wickets;
void putno_of_wickets()
};
int total;
public:
void display()
putinfo();
putruns();
putno_of_wickets();
};
void main()
allrounder obj;
clrscr();
obj.getinfo();
obj.getruns();
obj.getno_of_wickets();
obj.display();
getch();
//EXP14.1
#include<iostream.h>
#include<conio.h>
#include<string.h>
class Book
{
char author[10];
char book_name[10];
float price;
public:
void getdata()
{
};
void main()
{
Book b1,*bptr;
clrscr();
bptr=&b1;
bptr->getdata();
(*bptr).display();
getch();
}
//EXP142.2
#include<iostream.h>
#include<conio.h>
class box
{
float height,width,breadth;
public:
void get()
{
cout<<"\n (1).Enter height =";
cin>>height;
cout<<"\n (2).Enter width =";
cin>>width;
cout<<"\n (3).Enter breadth =";
cin>>breadth;
}
void area();
void volume();
};
void box::area()
{
cout<<endl<<"\n Area of box = "<<(width*height)<<"cm^2";
}
void box::volume()
{
cout<<endl<< "\nVolume of box ="<<(width*breadth*height)<<"cm^3";
}
int main()
{
clrscr();
cout<<"*** calculation of volume and area of box ***"<<endl;
box b;
box *ptr;
ptr=&b;
ptr->get();
(*ptr).area();
(*ptr).volume();
getch();
return 0;
}
//EXP14.3
#include<iostream.h>
#include<conio.h>
class birthday
{
char name[25];
int day;
int month;
int year;
public:
void get()
{
cout<<"\nenter the name : ";
cin>>name;
cout<<"\nenter the birth day : ";
cin>>day;
cout<<"\nenter the birth month : ";
cin>>month;
cout<<"\nenter the birth year : ";
cin>>year;
}
void display()
{
cout<<"\n NAME ="<<name;
cout<<"\n BIRTH DATE ="<<day<<"/"<<month<<"/"<<year;
}
};
int main()
{
clrscr();
int i=0;
birthday b[5];
birthday *B;
B=&b[0];
for(i=0;i<5;i++)
{
B->get();
B++;
}
cout<<" * * * BIRTHDATE INFORMATION * * * "<<endl;
B=&b[0];
for(i=0;i<5;i++)
{
B->display();
B++;
}
getch();
return 0;
}
EXP 15.1
#include<iostream.h>
#include<conio.h>
class polygon
{
int width,height;
public:
void get()
{
cout<<"\n Enter width & height:";
cin>>width>>height;
}
int wid()
{
return width;
}
int heig()
{
return height;
}
};
class triangle:public polygon
{
public:
void area()
{
float a;
int w=wid();
int h=heig();
a=0.5*w*h;
cout<<"\n Area of triangle: "<<a;
}
};
class rectangle:public polygon
{
public:
void area()
{
int b=wid();
int l=heig();
float a=l*b;
cout<<"Area of rectangle: "<<a;
}
};
int main()
{
triangle t, *tptr;
rectangle r, *rptr;
tptr=&t;
tptr->get();
tptr->area();
rptr=&r;
rptr->get();
rptr->area();
getch();
return 0;
}
EXP 16.1
#include <iostream.h>
#include<conio.h>
class Check
private:
int i;
public:
Check()
i=0;
Check operator ++ ()
Check temp;
temp.i = i++;
return temp;
Check operator -- ()
{
Check temp;
temp.i = i--;
return temp;
void Display()
};
int main()
clrscr();
obj.Display();
obj1.Display();
cout<<"Postincrement \n";
obj++;
obj.Display();
cout<<"Postdecrement \n";
obj--;
obj.Display();
getch();
return 0;
#include<iostream.h>
class complex
{
public:
int real;
int img;
complex()
{
real=img=0;
}
complex(int x,int y)
{
real=x;
img=y;
}
void show()
{
cout<<"\n"<<real<<"+"<<img<<"i";
}
friend complex operator+(complex c,complex d);
};
//operator+ is not part of complex class so have 2 args for + operator overload.
complex operator+(complex c, complex f)
{
complex ans;
ans.real=c.real+f.real;
ans.img=c.img+f.img;
return(ans);
}
int main()
{
complex x(1,2), y(0,7);
complex c=x+y; //overloaded + is called here
c.show();
}
#include<iostream.h>
#include<conio.h>
class BINARY
{
float no;
public:
BINARY(){}
void getdata()
{
cout<<"\n ENTER AN FLOATING NUMBER :";
cin>>no;
}
void putdata()
{
cout<<"\n\nANSWER IS :"<<no;
}
BINARY operator+(BINARY);
BINARY operator*(BINARY);
BINARY operator-(BINARY);
BINARY operator/(BINARY);
};
BINARY BINARY::operator+(BINARY a)
{
BINARY temp;
temp.no=no+a.no;
return temp;
}
BINARY BINARY::operator*(BINARY b)
{
BINARY temp;
temp.no=no*b.no;
return temp;
}
BINARY BINARY::operator-(BINARY b)
{
BINARY temp;
temp.no=no-b.no;
return temp;
}
BINARY BINARY::operator/(BINARY b)
{
BINARY temp;
temp.no=no/b.no;
return temp;
}
main()
{
clrscr();
Binary a,b,c;
a.getdata();
b.getdata();
//EXP 17.3 Definesa Class String and Overload == Operator to Compare Two Strings
#include<iostream.h>
#include<stdio.h>
#include<string.h>
class String
{
char str[20];
public:
void getdata() //function to read the string
{
gets(str);
}
int operator==(String s)
{
if(!strcmp(str,s.str))
return 1;
else
return 0;
}
};
void main()
{
String s1,s2;
cout<<“Enter first string:”;
s1.getdata();
cout<<“Enter second string:”;
s2.getdata();
//EXP 18.1 Program to interchange the values of two int , float and char using
function overloading
#include<iostream.h>
#include<conio.h>
void swap(int &,int &);
void swap(float &,float &);
void swap(char &,char &);
main()
{
clrscr();
int I1, I2;
float F1, F2;
char C1, C2;
cout<<"\n Enter the value of I1 & I2 = ";
cin>>I1>>I2;
cout<<"\n Enter the value of F1 & F2 = ";
cin>>F1>>F2;
cout<<"\n Enter the value of C1 & C2 = ";
cin>>C1>>C2;
cout<<"\n ********* Before Swap() **********"<<endl;
cout<<"\n Values of int :"<<endl;
cout<<"\t I1 = "<<I1<<"\t I2 = "<<I2<<endl;
cout<<" Values of float :"<<endl;
cout<<"\t F1 = "<<F1<<"\t F2 = "<<F2<<endl;
cout<<" Values of char :"<<endl;
cout<<"\t Value of C1 = "<<C1<<"\t C2 = "<<C2<<endl;
cout<<"\n ********* After Swap() **********"<<endl;
cout<<"\n Values of int :"<<endl;
swap(I1,I2);
cout<<"\t I1 = "<<I1<<"\t I2 = "<<I2<<endl;
cout<<" Values of float :"<<endl;
swap(F1,F2);
cout<<"\t F1 = "<<F1<<"\t F2 = "<<F2<<endl;
cout<<" Values of char :"<<endl;
swap(C1,C2);
cout<<"\t Value of C1 = "<<C1<<"\t C2 = "<<C2<<endl;
getch();
return 0;
}
void swap(int &v1,int &v2)
{
int temp;
temp=v2;
v2=v1;
v1=temp;
}
void swap(float &v1,float &v2)
{
float temp;
temp=v2;
v2=v1;
v1=temp;
}
void swap(char &v1,char &v2)
{
char temp;
temp=v2;
v2=v1;
v1=temp;
}
//EXP 18.3 C++ program to find area of square,rectangle,circle and triangle by using
function overloadingC++
#include<iostream.h>
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
{
int s,l,b;
float r,bs,ht;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Enter base and height of triangle:";
cin>>bs>>ht;
cout<<"Area of square is"<<area(s);
cout<<"\nArea of rectangle is "<<area(l,b);
cout<<"\nArea of circle is "<<area(r);
cout<<"\nArea of triangle is "<<area(bs,ht);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
//EXP 19.2 Program to merge contents of two files into a third file
#include <iostream.h>
#include <stdlib.h> // For exit()
#include <fstream.h>
#include <conio.h>
int main()
{
clrscr();
ifstream inf1("file1.txt",ios::in);
ifstream inf2;
inf2.open("file2.txt",ios::in);
ofstream outf;
outf.open("file3.txt",ios::out);
char c;
while (!inf1.eof())
{
inf1.get(c);
outf.put(c);
}
while (!inf2.eof())
{
inf2.get(c);
outf.put(c);
}
outf.close();
cout<<"Merged file1.txt and file2.txt into file3.txt\n\n";
inf1.close();
inf2.close();
outf.close();
getch();
return 0;
}
//EXP 19.3 Program to print contents of file
#include <iostream.h>
#include <stdlib.h> // For exit()
#include <fstream.h>
#include <conio.h>
int main()
{
clrscr();
ifstream inf;
char filename[100], c;
inf.open(filename, ios::in);
if (inf.fail())
{
cout<<"Cannot open file \n";
exit(0);
}
while (!inf.eof())
{
inf.get(c);
cout<<c;
}
inf.close();
getch();
return 0;
}