Practical Oops
Practical Oops
PRACTICAL -1
OVERVIEW OF C:
A) WRITE A PROGRAM TO ENTER TEN NO. COUNT TOTAL EVEN & ODD NUMBERS &
THEN PERFORM THE SUM OF EVEN & ODD NO SEPARATELY.
CODE:-
#include <iostream>
using namespace std;
int main() {
int sum=0, a, i, j = 0, k = 0, ar[10], even[10], odd[10];
// a is for taking elements' input from the user.
//i is for the index value of the main array
//j is for the index value of the even array
//k is for the index value of the even array
cout << "enter all the 10 elements or number:- ";
for (i = 0; i < 10; i++) {
//using for loop taking the input from the user in an array ar.
cout << endl << "enter element " << i + 1 << "= ";
cin >> a; //fetching all the 10 elements in a
ar[i] = a; //adding values to the array from 0 to last index number
}
cout << "the elements are :- " << endl;
cout << "[";
for (i = 0; i < 10; i++) {
cout << ar[i] << "\t";
if (i == 10 - 1) {
continue; } //to prevent comma at the last element
cout << ",";}
cout << " ]";
for (i = 0; i < 10; i++) {
if (ar[i] % 2 == 0) {
even[k] = ar[i];
k++;
} else {
odd[j] = ar[i];
j++;}}
cout << "\nHERE IS THE LIST OF ALL EVEN ELEMENTS AND THEIR SUM :-" <<
endl << "[";
//HERE K IS COUNT OF TOTAL NUMBER OF EVEN NUMBER.
for (i = 0; i < k; i++) {
sum=sum+even[i];
cout << even[i] << "\t";
if (i == k - 1)
continue; //to prevent comma at the last element
cout << ",";
}
cout << "]= "<<sum << endl;
//HERE J IS COUNT OF TOTAL NUMBER OF THE ODD NUMBER.
cout << "HERE IS THE LIST OF ALL ODD ELEMENTS AND THEIR SUM :-" <<
endl << "[";
sum=0;
for (i = 0; i < j; i++) {
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
sum=sum+odd[i];
cout << odd[i] << "\t";
if (i == j - 1)
continue; //to prevent comma at the last element
cout << ",";
}
cout << "]= "<<sum;
return 0;}
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
OUTPUT:-
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL -2
Implementation of basics of C++
A. Accept three numbers, find their sum and average
CODE :-
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
C. WAP to perform the Arithmetic operation & use the concept of typecasting
Code:-
//To perform the Arithmetic operation & use the concept of typecasting
#include<iostream>
using namespace std;
int main(){
int a, b, add, subtract, multiply, remainder;
float divide;
cout<<"Enter first integer: ";
cin>>a;
cout<<"Enter second integer: ";
cin>>b;
add = a+b;
subtract = a-b;
multiply = a*b;
divide = a/(float)b;
//here above we have done explicit typecasting
remainder = a%b;
cout<<"\n\nAddition of the numbers = "<< add<<endl;
cout<<"\nSubtraction of 2nd number from 1st = "<<subtract<<endl;
cout<<"\nMultiplication of the numbers = "<<multiply<<endl;
cout<<"\nDividing 1st number from 2nd = "<<divide<<endl;
cout<<"\nRemainder on Dividing " <<a<<" by "<<b<< " is = "<<remainder<<endl;
return 0;}
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL -3
Loops and Decisions
A. Write a Program to enter a number &check whether it’s a prime no. or not.
CODE:-
OUTPUT
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
C. WAP to find whether a Year is a leap or Not using logical OR && logical AND.
CODE:-
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
#include <iostream>
using namespace std;
int main(){
int number;
cout<<"ENTER ONLY DIGIT = ";
cin>>number;
switch (number){
case 0:
cout<<"the digit is zero";break;
case 1:
cout<<"the digit is one";break;
case 2:
cout<<"the digit is two";break;
case 3:
cout<<"the digit is three";break;
case 4:
cout<<"the digit is four";break;
case 5:
cout<<"the digit is five";break;
case 6:
cout<<"the digit is six";break;
case 7:
cout<<"the digit is seven";break;
case 8:
cout<<"the digit is eight";break;
case 9:
cout<<"the digit is nine";break;
default:
cout<<"enter only single digit";
break;}return 0;}
OUTPUT:-
PRACTICAL-4.
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
STRUCTURES
•Write a Program to construct a structure called employee that contains:-
two members(employee number of type integer & employee compensation of
type float)
. Now ask the user to fill this data for three employees, store it in three variable
of typed employee & then display the information for each employee.
CODE:-
#include <iostream>
using namespace std;
struct employee{
int number;
float salary; //compensation or salary.
};
int main() {
struct employee emp[20];
int i,n,number;
float salary;
cout<<"enter how many record you want to enter and display for the employee? :";
cin>>n;
cout <<endl;
for(i=0;i<n;i++){
}
cout<<"HERE ARE THE "<<n<<" RECORDS YOU HAVE ENTERED\n";
for(i=0;i<n;i++){
cout<<"ID OF RECORD ["<<i+1<<"] = "<<emp[i].number<<endl;
cout<<"SALARY OF RECORD ["<<i+1<<"] ="<<emp[i].salary<<endl;
cout<<"\n\n";
}
return 0;
}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL-5.
Functions
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
CODE:-
#include <iostream>
using namespace std;
//sum of all digits in a whole digit using recursion;
int sum_of_digit(int n){
if(n==0)
return 0;
else
return (n%10 +sum_of_digit(n/10));}
int main() {
int a,j,i,t;
cout<<"ENTER A NUMBER = ";
cin>>a;
cout<<"THE SUM OF WHOLE DIGIT = "<<a<<" => ";
t=a;
//LOOP FOR SEPARATING THE NUMBERS FOR MORE UNDERSTANDABLE OUTPUT
while(a!=0){
j=a%10;
a=a/10;
cout<<j;
if(a%10==0)
break;
//FOR PREVENTING '+' SYMBOL AT LAST.
cout<<"+";}
cout<<" = "<<sum_of_digit(t)<<"\t";
//DISPLAY THE OUT FROM THE RECURSIVE FUNCTION
return 0;}
OUTPUT
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL -6
.Objects & Classes
Implementation of different types of constructors.
A. Create a class employee which have name, age and address of employee,
include functions getdata() and showdata(), getdata() takes the input from
the user, showdata() display the data in following format:
Name:Age:Address:
CODE:-
#include <iostream>
using namespace std;
//CREATINNG A CLASS EMPLOYEE USING CLASS KEYWORD;
class employee{
public:
//making it members and functions public which can be accessible outsie the class
also
char name[20];
int age;
char add[20];
void getdata(){ //created get data method
cout<<"enter name =";
cin>>name;
cout<<"enter age =";
cin>>age;
cout<<"enter address =";
cin>>add;}
void showdata()
{ //created show data method
cout<<"\n\nEmployee name :"<<name<<endl;
cout<<"Employee age :"<<age<<endl;
cout<<"Employee address :"<<add<<endl;}}; //closing class
int main() {
//creating object of the class
employee e1;
//CALLING THE FUNCTIONS OF THE CLASS
e1.getdata();
e1.showdata(); return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
C. A constructor that allows the user to set initial values for accountNumber
and accountBalance and a default constructor that prompts for the input of
the values for the above data numbers.
CODE:-
#include <iostream>
using namespace std;
class cAccount{
private :
int acnumber;
float acbalance;
public:
cAccount(){ //default constructor or the constructor with no parameter;
cout<<"Enter your account number = ";
cin>>acnumber;
cout<<"Enter your account balance = ";
cin>>acbalance;}
cAccount(int x,float y){ // constructor for setting intial values through
parameterised constructor;
acnumber=x;
acbalance=y;
}
void show(){
cout<<"SHOWING YOUR DETAILS :-"<<endl;
cout<<"YOUR ACCOUNT NUMBER :- "<<acnumber<<endl;
cout<<"YOUR ACCOUNT BALANCE :- "<<acbalance<<endl; }};
int main() {
cAccount c1;
cout<<"THROUGH DEFAULT CONSTRUCTOR WE HAVE :-\n\n";
c1.show(); //DEFAULT CONSTRUCTOR
cout<<"THROUGH PARAMETERISED CONSTRUCTOR WE HAVE :-\n\n";
cAccount c2(12345,56789);
c2.show(); //PARAMETERISED CONSTRUCTOR
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
CODE:-
#include <iostream>
using namespace std;
class cAccount{
private :
int acnumber;
float acbalance;
public:
cAccount(){ //TAKING INPUT FROM USER THROUGH PROMPTING
cout<<"Enter your account number = ";
cin>>acnumber;
cout<<"Enter your account balance = ";
cin>>acbalance;}
void input_transaction(){
char ch;
float c;
cout<<"Press W for withrawal and D for deposit\n=> ";
cin>>ch;
//AFTER READING THE CHARACTER.
// EXCECUTION OF CONDITION THROUGH SWITCH STATEMENT
switch (ch){
case 'W':case 'w':
cout<<"ENTER THE AMOUNT YOU WANT TO WITHDRAW :- ";
cin>>c;
acbalance=acbalance-c;break;
case 'D':case 'd':
cout<<"ENTER THE AMOUNT YOU WANT TO DEPOSIT :- ";
cin>>c;
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
acbalance=acbalance+c;break;
default :
cout<<"WRONG INPUT!!!!!";break;}}
void printbalance(){
//SHOWING THE CURRENT UPDATED DETAILS OF THE ACCOUNT.
cout<<"SHOWING YOUR ACCOUNT UPDATED DETAILS :-"<<endl;
cout<<"YOUR ACCOUNT NUMBER :- "<<acnumber<<endl;
cout<<"YOUR ACCOUNT BALANCE :- "<<acbalance<<endl; }};
int main() {
cAccount c1;
c1.printbalance();
c1.input_transaction();
c1.printbalance();
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
E. Define a class Counter which contains an int variable count defined as static
and a static function Display () to display the value of count. Whenever an
object of this class is created count is incremented by 1. Use this class in
main to create multiple objects of this class and display value of count each
time
CODE:-
#include <iostream>
using namespace std;
class counter{
private :
//creating a static data member to store the count of object;
static int object_count;
public:
counter(){
object_count++; //incremeting count everytime object created;
}
void display(){
cout<<"THE TOTAL NUMBER OF OBJECTS CREATED ARE :- "<<object_count;
}};
//intiliazing objectcount with 0 using scope resolution
int counter::object_count=0;
int main() {
counter c1,c2,c3;
c1.display();
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL-7.
Polymorphism
(Function & Operator Overloading)
A. WAP to add and subtract two complex numbers using classes ( Operator
Overloading).
CODE:-
#include <iostream>
using namespace std;
class complex{
private :
int real,imaginary;
static int count; //FOR OBJECT COUNT
public:
void getdata(){
count++;
cout<<"Enter values for object["<<count<<"]"<<endl;
cout<<"Enter any Real number = ";
cin>>real;
cout<<"Enter any Imaginary number =";
cin>>imaginary;}
void showdata(){
cout<<"HERE IS YOUR OUTPUT :- \n";
cout<<"real number : "<<real<<endl<<"imaginary number : "<<imaginary;}
complex operator + (complex b){
complex c;
c.real=real+b.real;
c.imaginary=imaginary+b.imaginary;
return c;} };
int complex ::count=0;
int main() {
complex a,b,c;
a.getdata();
b.getdata();
c=a+b;
c.showdata();
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
CODE :-
(WITHOUT OPERATOR OVERLOADING):-
#include <iostream>
using namespace std;
class binary{
private :
int real,imaginary;
static int count;
public:
void getdata(){
count++;
cout<<"Enter values for object["<<count<<"]"<<endl;
cout<<"Enter any Real number = ";
cin>>real;
cout<<"Enter any Imaginary number =";
cin>>imaginary;}
void showdata(){
cout<<"HERE IS YOUR OUTPUT :- \n";
cout<<"real number : "<<real<<endl<<"imaginary number : "<<imaginary;
}
binary operator + (binary b){
binary c;
c.real=real+b.real;
c.imaginary=imaginary+b.imaginary;
return c;}}; int binary ::count=0;
int main() {
binary a,b,c;
a.getdata();
b.getdata();
c=a+b;
c.showdata();
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
(SAME_RESULT)
OUTPUT WITHOUT USING FRIEND FUNCTION:-
CODE:-
#include <iostream>
using namespace std;
// function with 2 parameters
void display(int var1, double var2) {
cout << "Integer number: " << var1;
cout << " and double number: " << var2 << endl;}
// function with double type single parameter
void display(double var) {
cout << "Double number: " << var << endl;}
// function with int type single parameter
void display(int var) {
cout << "Integer number: " << var << endl;}
int main() {
int a = 5;
double b = 5.5;
// call function with int type parameter
display(a);
// call function with double type parameter
display(b);
// call function with 2 parameters
display(a, b);
return 0;}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
PRACTICAL-8.
Inheritance & Virtual Functions
(Create a base class Shape & derive classes circle, square & triangle. In base
class write a virtual function “Parimeter and Area” & override it in derived
class.)
CODE:-
#include<iostream>
#include "bits/stdc++.h" //To Use acos method for the PI value
using namespace std;
class shape {
protected:
long ar, prmtr;
};
class square : public shape {
private:
long side;
public:
square(){
ar=prmtr=side=0;
}
void get(){
cout<<"Enter a side of square:";
cin>>side;
}
void area(){
ar=side*side;
cout<<"Area of the square is:"<<ar;
}
void parimeter(){
prmtr=4*side;
cout<<endl<<"The perimeter of the square is:"<<prmtr;
}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
};
class triangle : public shape {
private:
long base, height, hyp;
public:
triangle(){
ar=prmtr=base=height=hyp=0;
}
void input(){
cout<<endl<<"Enter three sides of Right triangle:";
cout<<"\nbase = ";
cin>>base;
cout<<"height = ";
cin>>height;
cout<<"Hypotynuse = ";
cin>>hyp;
}
void area(){
ar=0.5*base*height;
cout<<endl<<"The area of Right Triangle is:"<<ar;
}
void parimeter(){
prmtr=base+height+hyp;
cout<<endl<<"The parimeter of Right Triangle id:"<<prmtr;
}};
class circle :public shape{
private :
float r;
double pi = 2 * acos(0.0);
public :
void get(){
cout<<"\nenter the radius for the circle = ";
cin>>r;
}
void parimeter()
{
float c=2*pi*r;
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
};
class triangle : public shape {
private:
long base, height, hyp;
public:
triangle(){
ar=prmtr=base=height=hyp=0;
}
void input(){
cout<<endl<<"Enter three sides of Right triangle:";
cout<<"\nbase = ";
cin>>base;
cout<<"height = ";
cin>>height;
cout<<"Hypotynuse = ";
cin>>hyp;
}
void area(){
ar=0.5*base*height;
cout<<endl<<"The area of Right Triangle is:"<<ar;
}
void parimeter(){
prmtr=base+height+hyp;
cout<<endl<<"The parimeter of Right Triangle
id:"<<prmtr;
}};
class circle :public shape{
private :
float r;
double pi = 2 * acos(0.0);
public :
void get(){
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021
}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021