OOP Program Answers
OOP Program Answers
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();
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<<"Enter Name: ";
cin>>name;
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()
}
};
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;
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();
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()
void putruns()
};
int no_of_wickets;
public:
void getno_of_wickets()
cin>>no_of_wickets;
void putno_of_wickets()
};
class allrounder : public Batsman, public Bowler
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();