OOPS Practical FYBSc - IT
OOPS Practical FYBSc - IT
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <float.h>
int main() {
char op;
double a, b, res;
clrscr();
#include <iostream.h>
#include <conio.h>
int main()
{
int h,m,s,ts;
clrscr();
cout<<"Enter total seconds ";
cin>>ts;
h=ts/3600;
m=(ts%3600)/60;
s=(ts%3600)%60;
cout<<"Hours="<<h<<endl;
cout<<"Minutes="<<m<<endl;
cout<<"Seconds="<<s;
getch();
return 0;
}
1.c) Write a C++ program to find the volume of a square, cone, and rectangle.
const float pi = 3.14159; // Declaring a constant variable 'pi' to store the value of pi
float R, H, V; // Declaring variables to store the radius, height, and volume of the cone
cout << "Input Cone's radius: "; // Outputting a message to prompt the user to input the cone's
radius
cin >> R; // Reading the cone's radius input from the user
cout << "Input Cone's height: "; // Outputting a message to prompt the user to input the cone's
height
cin >> H; // Reading the cone's height input from the user
// Calculating the volume of the cone using the formula: V = (1/3) * π * R^2 * H
V = (1.0 / 3.0) * pi * (R * R) * H;
cout << "The volume of the cone is: " << V; // Outputting the calculated volume of the cone
getch();
return 0; // Return statement indicating successful completion of the program
}
#include <iostream.h>
#include <conio.h>
#include <math.h>
// create a class
class Room
{
// create object of Room class
Room room1; //Room is class and room1 is the object
// assign values to data members
room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;
clrscr();
#include <iostream.h>
#include <conio.h>
int main() {
int a = 1, b = 2, c = 11;
clrscr();
2.b) Write a C++ program to find the sum of even and odd n natural numbers.
#include<iostream.h>
#include<conio.h>
int main()
{
int number, maximum, evenSum = 0, oddSum = 0;
clrscr();
cout << "\nPlease Enter the Maximum Limit for Even & Odd Numbers = ";
cin >> maximum;
cout << "\nThe Sum of All Even Numbers upto " << maximum << " = " << evenSum;
cout << "\nThe Sum of All Odd Numbers upto " << maximum << " = " << oddSum;
getch();
return 0;
}
2.c) Write a C++ program to generate all the prime numbers between 1 and n, where n is a value
supplied by the use.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, j, count;
clrscr();
printf("Prime no.series\n");
printf("Enter any number\n");
scanf("%d", &n);
printf("The prime numbers between 1 to %d\n",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <=i; j++)
if(i % j == 0)
{
count++;
}
if(count == 2)
{
printf("%d\t", i);
}
}
getch();
}
3.a) Write a C++ program using classes and object Student to print name of the student, roll_no.
Display the same.
#include <iostream.h>
#include <conio.h>
#define MAX 10
class student {
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};
int main()
{
student std[MAX]; //array of objects creation
int n, loop;
cout << "Enter total number of students: ";
cin >> n;
3.b) Write a C++ program for Structure bank employee to print the employee's name, account_no.
& balance. Display the same also display the balance after withdraw and deposit.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class bank
{
int acno;
char nm[100], acctype[100];
float bal;
public:
bank(int acc_no, char *name, char *acc_type, float balance) //Parameterized Constructor
{
acno=acc_no;
strcpy(nm, name);
strcpy(acctype, acc_type);
bal=balance;
}
void deposit();
void withdraw();
void display();
};
void bank::deposit() //depositing an amount
{
int damt1;
cout<<"\n Enter Deposit Amount = ";
cin>>damt1;
bal+=damt1;
}
void bank::withdraw() //withdrawing an amount
{
int wamt1;
cout<<"\n Enter Withdraw Amount = ";
cin>>wamt1;
if(wamt1>bal)
cout<<"\n Cannot Withdraw Amount";
bal-=wamt1;
}
void bank::display() //displaying the details
{
cout<<"\n ----------------------";
cout<<"\n Accout No. : "<<acno;
cout<<"\n Name : "<<nm;
cout<<"\n Account Type : "<<acctype;
cout<<"\n Balance : "<<bal;
}
int main()
{
int acc_no;
char name[100], acc_type[100];
float balance;
cout<<"\n Enter Details: \n";
cout<<"-----------------------";
cout<<"\n Accout No. ";
cin>>acc_no;
cout<<"\n Name : ";
cin>>name;
cout<<"\n Account Type : ";
cin>>acc_type;
cout<<"\n Balance : ";
cin>>balance;
#include<iostream.h>
#include<conio.h>
class abc
{
int a1,a2,a3,a4;
void readno_fact()
{
cout<<"enter the number to find the factorial:";
cin>>a1;
}
void readno_rev()
{
cout<<"\nenter the number to find the reverse number:";
cin>>a2;
}
void readno_pal()
{
cout<<"\nenter the number to find the palindrome number :";
cin>>a3;
}
void readno_ams()
{
cout<<"\nenter the number to find the amstrong number :";
cin>>a4;
public:
void factorial();
int reverseno();
void palindrome();
void amstrong();
};
void abc::factorial()
{ readno_fact();
double x,fact=1;
x=a1;
for(int i=1;i<=x;i++)
{
fact=fact*i;
}
cout<<"\nthe factorial of number is :"<<fact<<endl;
}
int abc::reverseno()
{ readno_rev();
int y,rem,temp=0;
y=a2;
while(y!=0)
{
rem=y%10;
temp=10*temp+rem;
y=y/10;
}
return temp;
}
void abc::palindrome()
{ readno_pal();
int pp,temp2=0,rem2=0;
pp=a3;
while(pp!=0)
{
rem2=pp%10;
temp2=10*temp2+rem2;
pp=pp/10;
}
if(temp2==a3)
{
cout<<"\nthe number is palindrome\n";
}
else
{
cout<<"\nthe number is not palindrome\n";
}
}
void abc::amstrong()
{ readno_ams();
int z,sum=0,r;
z=a4;
while(z!=0)
{
r=z%10;
sum=sum+r*r*r;
z=z/10;
}
if(sum==a4)
cout<<"\nthe number is amstrong";
else
cout<<"\nthe number is not amstrong";
}
void main()
{
clrscr();
abc anynum;
anynum.factorial();
int r=0;
r=anynum.reverseno();
cout<<"\nreverse of the number is :"<<r<<endl;
anynum.palindrome();
anynum.amstrong();
getch();
}
3.d)Write a program to demonstrate function definition outside class and accessing class members
in function definition.
#include <iostream>
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata();
};
// function definition
void car::getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void car::showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
4.a)Write a friend function for adding the two complex numbers, using a single class.
#include<iostream.h>
#include<conio.h>
class complex
{
public:
int img,rel;
void getno()
{
cout<<"enter the real and imaginary number:";
cin>>rel>>img;
}
friend complex sum(complex,complex);
void display();
};
void complex::display()
{
cout<<"the sum of complex number is "<<rel<<"+i"<<img;
void main()
{
clrscr();
complex a,b,c;
a.getno();
b.getno();
c=sum(a,b);
c.display();
getch();
}
4.b) Write a friend function for adding the two different distances and display its sum, using two
classes.
#include <iostream.h>
#include<conio.h>
// forward declaration
class ClassB;
class ClassA {
public:
ClassA() : numA(12)
{
}
private:
int numA;
};
class ClassB {
public:
ClassB() : numB(1) {}
private:
int numB;
};
int main() {
clrscr();
ClassA objectA;
ClassB objectB;
getch();
return 0;
}
4.c) Write a friend function for adding the two matrix from two different classes and display its
sum.
4.d) Write a Program to find Maximum out of Two Numbers using friend function.
5.a) Design a class Complex for adding the two complex numbers and also show the use of
constructor.
5.b)Design a class Geometry containing the methods area() and volume() and also overload the
area()function.
5.c) Design a class StaticDemo to show the implementation of static variable and staticfunction
5.d. Write a C++ program to overload new/delete operators in a class.
5.e) Write a C++ Program to generate Fibonacci Series by using Constructor to initialize the Data
Members.
6.a) Overload the operator unary(-) for demonstrating operator overloading.
// class_A
class electronicDevice
{
public:
// constructor of the base class 1
electronicDevice()
{
cout << "I am an electronic device.\n\n";
}
};
// class_B inheriting class_A
class Computer: public electronicDevice
{
public:
// constructor of the base class 2
Computer()
{
cout << "I am a computer.\n\n";
}
};
// class_C inheriting class_B
class Linux_based : public Computer
{
public:
// constructor of the derived class
Linux_based()
{
cout << "I run on Linux.\n\n";;
}
};
int main()
{
// create object of the derived class
Linux_based obj; // constructor of base class 1,
// base class 2, derived class will be called
return 0;
}