[go: up one dir, main page]

0% found this document useful (0 votes)
95 views40 pages

Practical Oops

The document contains code snippets and output for 4 C++ programming practical assignments completed by Pratham Kangra. The practicals cover basics of C++ including arrays, loops, functions, conditional statements, and data types. The first practical contains code to input 10 numbers, separate evens and odds, and calculate their sums. The second has programs to find the sum and average of 3 numbers, and identify the type of an input character. The third uses loops and conditional statements to check for prime numbers and print patterns. The fourth demonstrates use of switch-case statements.

Uploaded by

Pratham Kangra
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)
95 views40 pages

Practical Oops

The document contains code snippets and output for 4 C++ programming practical assignments completed by Pratham Kangra. The practicals cover basics of C++ including arrays, loops, functions, conditional statements, and data types. The first practical contains code to input 10 numbers, separate evens and odds, and calculate their sums. The second has programs to find the sum and average of 3 numbers, and identify the type of an input character. The third uses loops and conditional statements to check for prime numbers and print patterns. The fourth demonstrates use of switch-case statements.

Uploaded by

Pratham Kangra
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/ 40

BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA

IITM(JANAKPURI) ENROLLMENT NO : 02324402021

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

B) WRITE A PROGRAM TO PRINT FIBONACCI SERIES


CODE:-

OUTPUT:-

C) REVERSE OF A STRING WITHOUT USING STRING OPERATIONS


CODE:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

//TO REVERSE A STRING WITHOUT USING STRING OPERATION


#include <iostream>
using namespace std;
int main() {
int i;
char a[20]; //creating an character array to hold string
cout<<"enter your string = ";
cin>>a; //taking input string in character array (char a[])
for (i=0;a[i]!=NULL;++i);
cout<<”here is the reverse of the string =”;
//checking the length for the total characters entered.
//till the array is not null.
int j=i-1; //total length of the charactered entered assigning to j.
while(j>=0){ //printing the character in reverse order of their index.
cout<<a[j];
j--;}
return 0;}

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 :-

//Accept three numbers, find their sum and average


#include <iostream>
using namespace std;
int main() {
int a,b,c,sum,avg;
cout<<"enter first number = ";
cin>>a;
cout<<"enter second number = ";
cin>>b;
cout<<"enter second number = ";
cin>>c;
sum=a+b+c;
avg=sum/3;
cout<<"the sum is := "<<sum<<endl;
cout<<"the average is := "<<avg<<endl;
return 0;}

OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

B. Program to identify if an input is a symbol, digit or character


CODE:-

// CPP program to find type of input character


#include <iostream>
using namespace std;
void charCheck(char input_char)
{// CHECKING FOR ALPHABET
if ((input_char >= 65 && input_char <= 90)
|| (input_char >= 97 && input_char <= 122))
cout << " Alphabet ";
// CHECKING FOR DIGITS
else if (input_char >= 48 && input_char <= 57)
cout << " Digit ";
// OTHERWISE SPECIAL CHARACTER
else
cout << " Special Character ";}
int main(){
char input_char;
cout<<"enter character = ";
cin>>input_char;
charCheck(input_char);
return 0;}
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:-

//To find whether a number is prime or not;


#include<iostream>
using namespace std;
int main(){
int number,i,flag=0;
cout<<"enter integer := ";
cin>>number;
for(i=1;i<=number/2;i++){
if(number %i==0) //checking the factor using modulus operator.
{flag++;}} //holding all the number of factors in flag variable.
if(flag==1){ //if factor is only 1 then return number is prime
cout<< number<<" is a prime number.\n";}
else if(number==1){
cout<<"1 is neither a prime nor a composite number";}
else{ //if factor is more than 1 then return number is not prime
cout<< number <<" is not a prime number\n"; }
return 0;}

OUTPUT
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

B. WAP To Print this Pattern:


1
23
456
7 8 9 10
CODE:

// TO THE PRINT THE HALF PYRAMID


#include <iostream>
using namespace std;
int main() {
int i,j,k=1,number;
cout<<"enter number of rows = ";
cin>>number;
for(i=0;i<=number;i++){
for(j=0;j<i;j++)
{
cout<<k++<<"\t";
}
cout<<endl;
}
return 0;
}

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:-

//finding whether the year is leapyear or not


#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if(year>=1800){
//CONDITION TO TAKE CORRECT INPUT FROM THE USER
if (year % 400 == 0 ||year % 4 == 0 ) {
//IF THE YEAR IS EVENLY DIVISIBLE BY 400 OR 4 THEN,THE YEAT IS LEAP YEAR.
cout << year << " is a leap year.";}
else if (year % 100 == 0 && year % 4 == 0) {
//IF THE YEAR IS EVENLY DIVISIBLE BY 100 AND 4 THEN,THE YEAT IS LEAP YEAR.
cout << year << " is a leap year.";}
else {
cout << year << " is not a leap year.";}}
else {
cout<<"WRONG INPUT GIVEN!!! (TRY AGAIN)";}
return 0;}

OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

D. WAP to accept a digit & display it in word using switch-case.


CODE:-

#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<< "ENTER THE EMPLOYEE NUMBER RECORD["<<i+1<<"]: \n";


cin >> number;
cout << "ENTER THE EMPLOYEE SALARY RECORD["<<i+1<<"]: \n";
cin >> salary;
cout <<endl;
emp[i].number=number;
emp[i].salary=salary;

}
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

A. WAP to create a function init() having following parameters :-[id,


name, course.]
Pass parameters to this function through user & if user does not give
parameters provide default value to id as 1 & name as “abc’.
CODE:-
#include <iostream>
using namespace std;
void init( int id=1,char name[20]="abc",char course[8]="unknown"){
/*above we have provided default parameters to the arguments using '=' Assignment operator*/
cout<<"THE STUDENT ID IS :"<<id<<endl;
cout<<"THE STUDENT NAME IS :"<<name<<endl;
cout<<"THE COURSE TAKEN :"<<course<<endl;}
int main(){
//calling init() function using default values
cout<<"the output after calling init() function using default values\n";
init();
//calling init() function and passing parameter;
cout<<"\nthe output after calling init() function by passing arguments\n";
init(23,"PRATHAM KANGRA","BCA");
return 0;}

OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

B. WAP to create Inline function to find greatest of two


numbers.
CODE:-
#include <iostream>
using namespace std;
//using inline keyword we are making a new inline function;
inline void greatest(int a,int b){
if(a>b)
cout<<a<<" is greater than "<<b;
else
cout<<b<<" is greater than "<<a;
}
int main() {
int a,b;
cout<<"enter first integer = ";
cin>>a;
cout<<"enter second integer = ";
cin>>b;
greatest(a,b);
return 0;
}

OUTPUT:-
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

C. WAP to sum the digit of a no. using recursion

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

B. Write a class called CAccount which contains two private data


elements, an integer accountNumber and a floating point
accountBalance, and three member functions:
CODE:-
#include <iostream>
using namespace std;
class cAccount{ //DEFINING CLASS USING KEYWORD.
private : //DECLARING PRIVATE DATA-MEMBERS USING PRIVATE KEYWORD.
int acnumber;
float acbalance;
public: //DECLARING MEMBER FUNCTION AS PUBLIC USING PUBLIC KEYWORD.
void registering(){
cout<<"ENTER YOUR ACCOUNT NUMBER :";
cin>>acnumber;
cout<<"HOW MUCH BALANCE YOU WANT TO DEPOSIT :- ";
cin>>acbalance;}
void update(){
int i;float c;
cout<<"\tDo you want to update your balance? :\n\ Press 0 for Yes and 1 for No.\n";
cin>>i;
if(i==0){
cout<<"how much you want to add ";
cin>>c;
acbalance=acbalance+c;
cout<<" YOUR UPDATE IS SUCCESFULL ";}
else{
cout<<"thankyou have a nice day\n";}}
void show(){
cout<<"your account number := "<<acnumber<<endl;
cout<<"your account balance := "<<acbalance<<endl;}
};
int main() {
cAccount c1;
c1.registering();
c1.update();
c1.show();
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

D. A function called inputTransaction, which reads a character value for


transactionType(‘D’ for deposit and ‘W’ for withdrawal), and a floating point
value for transactionAmount, which updates accountBalance.
A function called printBalance, which prints on the screen the accountNumber
and accountBalance.

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

F. WAP to implement Parameterized Constructor, Copy Constructor and


Destructor.
CODE:-
#include <iostream>
using namespace std;
class rectangle{
private :
int x,y;
public:
rectangle(int a,int b){ //parameteristed construtor
x=a;
y=b;}
rectangle(rectangle &obj){ //copy constructor
x=obj.x;
y=obj.y;}
void area(){
cout<<"THE AREA OF RECTANGLE = "<<x*y<<endl;}
~rectangle(){
cout<<"\n Destructor executed";} //destructor of rectangle class
int main() {
rectangle r1(20,30);
cout<<"THE OUTPUT FROM THE PARAMETERISED CONSTRUCTOR\n";
r1.area();
cout<<"\nTHE OUTPUT FROM THE COPY CONSTRUCTOR\n";
rectangle r2(r1);
r2.area();
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

B. Write program to overload Binary + to add two similar types of


objects. (Both with and without using friend functions)

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

Using friend function


#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;
}
friend binary operator + (binary a,binary b){
binary c;
c.real=a.real+b.real;
c.imaginary=a.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:-

OUTPUT USING FRIEND FUNCTION:-


BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

C. WAP to implement ‘Function Overloading’

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

A. Implement the following class hierarchy considering appropriate data


members and member functions

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

cout<<"The circumference of circle = "<<c<<endl;


}
void area()
{
float c=pi*r*r;
cout<<"The Area of circle = "<<c<<endl;}
};
int main(){
square s1;
triangle t1;
circle c1;
cout<<"\n SHOWING THE RESULTS FOR SQUARE CLASS\n";
s1.get();
s1.area();
s1.parimeter();
cout<<"\n\nSHOWING THE RESULTS FOR TRIANGLE CLASS\n";
t1.input();
t1.area();
t1.parimeter();
cout<<"\n\nSHOWING THE RESULTS FOR CIRCLE CLASS\n";
c1.get();
c1.parimeter();
c1.area();
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

B. Implement the following hierarchy considering appropriate data


members and member functions (use Virtual functions).

CODE : (USING VIRTUAL FUNCTIONS OVER-RIDING)


#include<iostream>
#include "bits/stdc++.h"
using namespace std;
class shape {
protected:
long ar, prmtr;
public:
virtual void area()=0;
virtual void parimeter()=0;
};
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(){
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

cout<<"\nenter the radius for the circle = ";


cin>>r;
}
void parimeter()
{
float c=2*pi*r;
cout<<"The circumference of circle = "<<c<<endl;
}
void area()
{
float c=pi*r*r;
cout<<"The Area of circle = "<<c<<endl;}
};
int main(){
square s1;
triangle t1;
circle c1;
cout<<"\n SHOWING THE RESULTS FOR SQUARE CLASS\n";
s1.get();
s1.area();
s1.parimeter();
cout<<"\n\nSHOWING THE RESULTS FOR TRIANGLE CLASS\n";
t1.input();
t1.area();
t1.parimeter();
cout<<"\n\nSHOWING THE RESULTS FOR CIRCLE CLASS\n";
c1.get();
c1.parimeter();
c1.area();
return 0;

}
BCA E-1 C++ PRACTICAL FILE PRATHAM KANGRA
IITM(JANAKPURI) ENROLLMENT NO : 02324402021

You might also like