0% found this document useful (0 votes)
47 views46 pages

OOPS Practical FYBSc - IT

The document contains multiple C++ programming tasks, including creating a simple calculator, converting seconds to hours, minutes, and seconds, calculating volumes of geometric shapes, finding the greatest of three numbers, and generating prime numbers. It also includes class and object implementations for student and bank employee management, along with various operations such as deposits and withdrawals. Additionally, there are examples of operator overloading, friend functions, and inheritance.

Uploaded by

Marsha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views46 pages

OOPS Practical FYBSc - IT

The document contains multiple C++ programming tasks, including creating a simple calculator, converting seconds to hours, minutes, and seconds, calculating volumes of geometric shapes, finding the greatest of three numbers, and generating prime numbers. It also includes class and object implementations for student and bank employee management, along with various operations such as deposits and withdrawals. Additionally, there are examples of operator overloading, friend functions, and inheritance.

Uploaded by

Marsha Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

1.

a) Write a C++ program to create a simple calculator

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

int main() {
char op;
double a, b, res;
clrscr();

// Read the operator


cout << "Enter an operator (+, -, *, /): ";
cin >> op;

// Read the two numbers


cout << "Enter two numbers: ";
cin >> a >> b;

// Define all four operations in the corresponding


// switch-case
switch (op) {
case '+':
res = a + b;
break;
case '-':
res = a - b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
default:
cout << "Error! Operator is not correct";
res = -DBL_MAX;
}

// Printing the result


if (res != -DBL_MAX)
cout << "Result: " << res;
getch();
return 0;
}
1.b) Write a C++ program to convert seconds into hours, minutes and seconds.

#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.

Volume of cone c++ code

#include <iostream.h> // Including the input-output stream header file


#include <string.h> // Including the string header file (although it's not used in this code)
#include <conio.h>

int main() // Start of the main function


{
clrscr();

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
}

Volume of rectangle c++ code using objects and class.

#include <iostream.h>
#include <conio.h>
#include <math.h>
// create a class
class Room

{ //name of the class is Room

public: //Access Specifier


double length; //Data Member
double breadth; //Data Member
double height; //Data Member Double is the data type
double calculate_volume() //one function
{
return length * breadth * height; //logic
}
};

int main() //Main Program

{
// 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();

// calculate and display the volume of the rectangle


cout << "Volume of Rectangle = " << room1.calculate_volume() << endl;
getch();
return 0;
}

Volume of sphere is home work


2a).Write a C++ program to find the greatest of three numbers.

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

int main() {
int a = 1, b = 2, c = 11;
clrscr();

// Finding the largest by comparing using


// relational operators with if-else
if (a >= b) {
if (a >= c)
cout << a;
else
cout << c;
}
else {
if (b >= c)
cout << b;
else
cout << c;
}
getch();
return 0;
}

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;

for(number = 1; number <= maximum; number++)


//initilize number with 1 and take till the n value decided
//by user
{
if ( number % 2 == 0 )
//if number is divided by 2 and remender is zero then even
//else odd
{
evenSum = evenSum + number;
}
else
{
oddSum = oddSum + number;
}
}

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);
};

//member function definition, outside of the class


void student::getDetails(void)
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;

perc = (float)total / 500 * 100;


}

//member function definition, outside of the class


void student::putDetails(void)
{
cout << "Student details:\n";
cout << "Name:" << name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" <<
perc;
}

int main()
{
student std[MAX]; //array of objects creation
int n, loop;
cout << "Enter total number of students: ";
cin >> n;

for (loop = 0; loop < n; loop++) {


cout << "Enter details of student " << loop + 1 << ":\n";
std[loop].getDetails();
}

cout << endl;

for (loop = 0; loop < n; loop++) {


cout << "Details of student " << (loop + 1) << ":\n";
std[loop].putDetails();
}
getch();
return 0;

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;

bank b1(acc_no, name, acc_type, balance); //object is created


b1.deposit(); //
b1.withdraw(); // calling member functions
b1.display(); //
getch();
return(0);
}
3.c) Design the class Demo which will contain the following methods: readNo(), factorial() for
calculating the factorial of a number, reverseNo() will reverse the given number, isPalindrome() will
check the given number is palindrome, isArmstrong() which will calculate the given number is
armStrong or not. WherereadNo() will be private method.

#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;

complex sum(complex a,complex b)


{
complex t;
t.rel=a.rel+b.rel;
t.img=a.img+b.img;
return t;
}

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:

// constructor to initialize numA to 12

ClassA() : numA(12)
{
}

private:

int numA;

// friend function declaration

friend int add(ClassA, ClassB);

};

class ClassB {

public:

// constructor to initialize numB to 1

ClassB() : numB(1) {}

private:

int numB;

// friend function declaration

friend int add(ClassA, ClassB);

};

// access members of both classes

int add(ClassA objectA, ClassB objectB) {

return (objectA.numA + objectB.numB);

int main() {
clrscr();
ClassA objectA;
ClassB objectB;

cout << "Sum: " << add(objectA, 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.

Overloading unary and binary operators.


6.b) Overload the operator + for adding the timings of two clocks, And also pass objects as an
argument.
6.c) Overload the + for concatenating the two strings. For e.g “Py”
+“thon” =Python
7.b) Show the use of virtual function
7.c) Show the implementation of abstract class.
8. a. Write a C++ Program that illustrate single inheritance.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class electronicDevice // base class


{
public:
electronicDevice() // constructor of the base class
{
cout << "I am an electronic device.\n\n";
}
};
class Computer: public electronicDevice // derived class
{
public:
Computer() // constructor of the derived class
{
cout << "I am a computer.\n\n";
}
};
int main()
{
// create object of the derived class
Computer obj; // constructor of base class and derived class will be
called
getch();
return 0;
}

8. b. Write a C++ Program that illustrate multiple inheritance.


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

class electronicDevice // class_A


{
public:
electronicDevice() // constructor of the base class 1
{
cout << "I am an electronic device.\n\n";
}
};
class Computer // class_B
{
public:
Computer() // constructor of the base class 2
{
cout << "I am a computer.\n\n";
}
};
// now class_C inheriting class_A and class_B
class Linux_based : public electronicDevice, public Computer
{}; //no logic at the moment so curly braces are only to declare inheried
class
int main()
{
// create object of the derived class
Linux_based obj; // constructor of base class A,
// base class B and derived class
// will be called
return 0;
getch();
}
8.c Write a C++ Program that illustrate multi-level inheritance.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>

// 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;
}

8.d) Write a C++ Program that illustrate Hierarchical inheritance.

You might also like