You are on page 1of 75

MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

OBJECT ORIENTED
PROGRAMMING USING
C++
Practical file submitted in partial fulfillment of the requirement of third semester for degree of
  
Bachelor of Computer Application (B.C.A.)
(2019-2022)

Under the Supervision of


Mrs. Tarunim Sharma
By
NIPUN WALIA
00921202019
BCA(Evening)
To

MAHARAJA SURAJMAL INSTITUTES (MSI)


(Affiliated from G.G.S.I.P University, Delhi)

 
August-December 2020
1
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

2
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

3
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

Question 1 : WAP to generate division on the marks obtained in 4 subjects


#include<iostream>
using namespace std;
int main()
{
int mark[4], i;
float sum=0,avg;
cout<<"\nEnter Marks in 4 subjects : \n";
for(i=0; i<4; i++)
{
cout<<"\nEnter Marks "<<i+1<<" : ";
cin>>mark[i];
sum=sum+mark[i];
}
avg=sum/4;
cout<<"\nYour division : ";
if(avg>60)
{
cout<<" 1 \n";
}
else if(avg>50 && avg<=60)
{
cout<<" 2 \n";
}
else if(avg>40 && avg<=50)
{
cout<<" 3 \n";
}
else
{

4
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout<<" 4 \n";
}
return 0;
}

********************* OUTPUT ***********************

5
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 2: WAP to check whether the given number is prime or not?
#include <iostream>
using namespace std;
int main(){
int num;
bool flag = true;
cout<<"Enter number: ";
cin>>num;

for(int i = 2; i <= num / 2; i++) {


if(num % i == 0) {
flag = false;
break;
}
}
if (flag==true)
cout<<num<<" is a prime number";
else
cout<<num<<" is not a prime number";
return 0;
}

********************* OUTPUT ***********************

6
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 3: WAP to check whether given character is vowel or not?
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Enter an alphabet: ";
cin >> c;

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')


cout << c << " is a vowel.";
else
cout << c << " is a consonant.";

return 0;
}

********************* OUTPUT ***********************

7
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 4: WAP to +,-,*,/ two numbers using switch
# include <iostream>
using namespace std;

int main()
{
char op;
float num1, num2;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op)
{
case '+':
cout << num1+num2;
break;

case '-':
cout << num1-num2;
break;

case '*':
cout << num1*num2;
break;

case '/':
cout << num1/num2;

8
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
break;

default:
cout << "Error! operator is not correct";
break;
}

return 0;
}

********************* OUTPUT ***********************

9
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 5: WAP to find greatest of 3 using nested if else
#include <iostream>
using namespace std;
int main() {
int n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if (n1 >= n2) {
if (n1 >= n3)
cout << "Largest number: " << n1;
else
cout << "Largest number: " << n3;
}
else {
if (n2 >= n3)
cout << "Largest number: " << n2;
else
cout << "Largest number: " << n3;
}
return 0;
}

********************* OUTPUT ***********************

10
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 6: WAP to search a number in an array using linear search
#include<iostream>
using namespace std;
int main()
{ int a[5],i,n;
cout<<"enter the array\n";
for(i=0;i<5;i++)
{
cin>>a[i]; }
cout<<"enter the element you want to search\n";
cin>>n;
for(i=0;i<5;i++)
{
if(a[i]==n)
{
cout<<"element is found\n";
cout<<"location of element is :"<<i;
}
}
return 0;
}

********************* OUTPUT ***********************


11
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

12
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 6: WAP to search a number in an array using Binary search
#include<iostream>
using namespace std;
int main()
{ int a[5],i,n,beg,end,mid;
cout<<"enter the array\n";
for(i=0;i<5;i++)
{ Cin>>a[i]; }
Cout<<"enter the element you want to search\n";
Cin>>n;
beg=1; end=5;
while(beg<=end)
{ mid=(beg+end)/2;
if(n==a[mid])
{ Cout<<"element is found at :"<<mid+1;
break; }
else if(n>a[mid])
beg=mid+1;
else
end=mid-1; }
return 0; }

********************* OUTPUT ***********************

13
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 7: WAP to input month number and print number of days
#include <iostream>
using namespace std;
int count(int month, int year)
{
if( month == 2)
{
if((year%400==0) || (year%4==0 && year%100!=0))
return 29;
else
return 28;
}

else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||month == 10 ||


month==12)
return 31;
else
return 30;
}

int main()
{
int year,month,days;
cout<<"Enter Year: ";
cin>>year;
cout<<"Enter Month: ";
cin>>month;
days = count(month, year);
cout<<"Number of Days : "<<days;
return 0;
}

14
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
********************* OUTPUT ***********************

15
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 8: WAP to find factorial of a number using Recursion.
#include<iostream>
using namespace std;
int factorial(int n);
int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}

********************* OUTPUT ***********************

16
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 9: WAP to area and circumference of circle using classes and
Objects
#include <iostream>
#include <conio.h>
using namespace std;
class Circle
{
private:

int r;

public:

void inputdata();
void area();
void perimeter
};

void Circle::inputdata()
{
cout<<"\n Enter the radius of the circle";
cin<<r;
}

void Circle::area()
{
float ar=3.14*r*r;
cout<<"\n Area of circle="<<ar;
}

void Circle::perimeter(){

17
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
float per=7.28*r;
cout<<"\n the perimeter of circle="<<per;
}

int main()
{
Circle c1;
c1.inputdata();
c1.area();
c1.perimeter();

getch();
return 0;
}

********************* OUTPUT ***********************

18
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 10: WAP to find volume of cone, sphere, cylinder
#include <iostream>
#include <conio.h>
using namespace std;

class Volume
{
private:

int r,h;

public:

void sphere();
void cylinder();
void cone();

};

void Volume::sphere()
{
cout<<"\n Enter the radius of the sphere";
cin>>r;
float vol=4.18*r*r*r;
cout<<"\n the volume of sphere is "<< vol;
}

void Volume::cylinder()
{
cout<<"\n Enter the radius and height of the cylinder";
cin>>r>>h;

19
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
float vol=3.14*r*r*h;
cout<<"\n volume of cylinder="<<vol;
}

void Volume::cone()
{
cout<<"\n Enter the radius and height of the cylinder";
cin>>r>>h;
float vol=1.03*r*r*h;
cout<<"\n volume of cylinder="<<vol;
}

int main()
{
Volume v1;
v1.sphere();
v1.cylinder();
v1.cone();

getch();
return 0;
}

20
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
********************* OUTPUT ***********************

21
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 11: WAP to show inline member function
#include <iostream>

using namespace std;

class check{
private:
int m,n;
public:
void input(void);
void display(void);
int largest(void);//func declaration
};

int check:: largest(void)


{
if(m>=n)
return(m);
else
return(n);
}

inline void check ::input(void)


{
cout<<"input values of m and n"<<"\n";
cin>>m>>n;
}

void check ::display(void){


cout<<"largest value"<<largest()<<"\n"; //nesting
}

22
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

int main()
{
check A;
A.input();
A.display();

return 0;
}

********************* OUTPUT ***********************

23
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 12: WAP to calculate gross salary while demonstrating accessing of
private member function with in a class?
#include <iostream>
using namespace std;

class emp
{
private:
int basicsal;
int da(int bs){
float da=bs*0.5;
return da;
}
int hra(int bs){
float hra=bs*0.15;
return hra;
}
public:
void input(void);
void display(void);
int grosssalary(void);
};

int emp:: grosssalary(void)


{
return da(basicsal)+ hra(basicsal)+ basicsal;
}

inline void emp :: input(void)


{
cout<<"input value of basic salary:"<< "\n";

24
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cin>>basicsal;
}

void emp :: display(void)


{
cout<<"grosssalary = "<<grosssalary()<<"\n";
}

int main()
{
emp A;
A.input();
A.display();

return 0;
}

********************* OUTPUT ***********************

25
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 13: WAP to demonstrate array of object
#include <iostream>

using namespace std;

class Rectangle
{
public:
int length;
int breadth;
Rectangle( int l, int b )
{
length = l;
breadth = b;
}
int printArea()
{
return length * breadth;
}
};

int main()
{
Rectangle rt1( 7, 4 );
Rectangle rt2( 4, 5 );
cout << "Area of first rectangle " << rt1.printArea() << endl;
cout << "Area of second rectangle " << rt2.printArea() << endl;
return 0;
}

26
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
********************* OUTPUT ***********************

27
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 14: WAP to demonstrate the use of Static data members and static
member functions in a class.
#include <iostream>
using namespace std;
class Box {
public:
static int objectCount;
Box(double l = 2.0, double b = 2.0, double h = 2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
objectCount++;
}
double Volume()
{
return length * breadth * height;
}
static int getCount()
{
return objectCount;
}

private:
double length;
double breadth;
double height;
};
int Box::objectCount = 0;
int main(void)

28
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
{
cout << "Inital Stage Count: " << Box::getCount() << endl;
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
cout << "Final Stage Count: " << Box::getCount() << endl;
return 0;
}

********************* OUTPUT ***********************

29
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 15: WAP to pass an object as function argument in class?
#include <iostream>
using namespace std;
class point
{ private:
int x;
int y;
public:
point(int x1=0,int y1=0)
{ x=x1;
y=y1;
}
point addpoint(point p)
{ point temp;
temp.x=x+p.x;
temp.y=y+p.y;
return temp;
}
void display()
{cout<<"x= "<<x<<"\n";
cout<<"y= "<<y<<"\n";
}
};
int main()
{point p1(5,3);
point p2(12,6);
point p3;
cout<<"point 1\n";
p1.display();
cout<<"point 2\n";
p2.display();

30
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
p3=p1.addpoint(p2);
cout<<"The sum of the two points is: \n";
p3.display();
return 0;
}

********************* OUTPUT ***********************

31
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 16: WAP to return an object from member function of a class
#include <iostream>
using namespace std;
class point
{ private:
int x;
int y;
public:
point(int x1=0,int y1=0)
{ x=x1;
y=y1;
}
point addpoint(point p)
{ point temp;
temp.x=x+p.x;
temp.y=y+p.y;
return temp;
}
void display()
{cout<<"x= "<<x<<"\n";
cout<<"y= "<<y<<"\n";
}
};

int main()
{point p1(5,3);
point p2(12,6);
point p3;
cout<<"point 1\n";
p1.display();
cout<<"point 2\n";

32
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
p2.display();
p3=p1.addpoint(p2);
cout<<"The sum of the two points is: \n";
p3.display();
return 0;
}

********************* OUTPUT ***********************

33
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 17: WAP to add two matrices using class
#include<iostream>
using namespace std;

class Add{
public:
void sum(int r, int c){
int m1[r][c], m2[r][c], s[r][c];
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<r;i++ ) {
for (int j = 0;j < c;j++ ) {
cin>>m1[i][j];
}
}
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<r;i++ ) {
for (int j = 0;j<c;j++ ) {
cin>>m2[i][j];
}
}
cout<<"Output: ";
for (int i = 0;i<r;i++ ) {
for (int j = 0;j<c;j++ ) {
s[i][j]=m1[i][j]+m2[i][j];
cout<<s[i][j]<<" ";
}
}
}
};
int main(){
int row, col;

34
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout<<"Enter the number of rows(should be >1 and <10): ";
cin>>row;
cout<<"Enter the number of column(should be >1 and <10): ";
cin>>col;
Add obj;
obj.sum(row, col);
return 0;
}

********************* OUTPUT ***********************

35
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 18: WAP to demonstrate constructor overloading
#include <iostream>

using namespace std;

class complex{
float x;
float y;
public:
complex(){}
complex(float real, float img){
x=real;y=img;
}
complex operator+(complex);
void display(void);
};

complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}

void complex::display(void)
{
cout<<x<<"+ j"<<y<<"\n";
}

int main()

36
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
{
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;

cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3=";c3.display();

return 0;
}

********************* OUTPUT ***********************

37
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 19: WAP to generate fibonacci series using objects and classes
  
#include <bits/stdc++.h>
using namespace std;
class Fibonacci {
public:
    int a, b, c;
    void generate(int);
};
  
void Fibonacci::generate(int n)
{
    a = 0;
    b = 1;
  
    cout << a << " " << b;
    for (int i = 1; i <= n - 2; i++) {
         c = a + b;
        cout << " " << c;
        a = b;
        b = c;
    }
}
  int main()
{
    int n = 9;
  
    Fibonacci fib;
    fib.generate(n);
    return 0;
}

********************* OUTPUT ***********************

38
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 20: WAP to demonstrate destructor
#include <iostream>
using namespace std;
int count=0;
class alpha

{
public:
alpha()
{

count++;
cout<<"\n No. of object created"<<count;
}
~alpha()
{
cout<<"\n No. of objects destroyed"<<count;
count--;
}

};
int main()
{
cout<<"\n Enter main \n" ;
alpha A1,A2,A3,A4;
{
cout<<"\n Enter block 1 \n";
alpha A5;

}
{

39
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout<<"\n Enter block 2 \n";
alpha A6;
}
cout<<"\n Enter main \n" ;

return 0;
}

********************* OUTPUT ***********************

40
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 21: WAP to demonstrate the use of constructor and destructor for
performing dynamic operation on string
#include<iostream>
#include<string.h>
using namespace std;

class dynamic
{
char *name;
int length;

public :
dynamic()
// First Constructor
{
length = 0;
name = new char[length + 1];
}

dynamic(char *s)
// Second Constructor
{
length = strlen(s);
name = new char[length + 1];
strcpy(name,s);
}

void show()
// Method to display name using dynamic allocation in join method
{
cout << name << "\n";

41
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout << "Number of characters in the string is " << strlen(name) << "\n\n";
}

void join(dynamic &a, dynamic &b)


{
length = a.length + b.length;
delete name;
name = new char[length + 1];
// dynamic allocation of name using new

strcpy(name, a.name);
strcat(name, b.name);
}
~dynamic()
{ cout<<"destructor called";
}
};

int main ()
{
char *first = "Hello !";
dynamic name1(first);
dynamic name2("Technology");
dynamic name3("Lovers");

dynamic s1, s2;


s1.join(name1, name2);
s2.join(s1, name3);

name1.show();
name2.show();

42
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
name3.show();

return 0;
}

********************* OUTPUT ***********************

43
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

Question 22: WAP to demonstrate friend function


class rectangle
{
private:
int l;
int b;
public:
void getdata()
{
cout<<"enter length and breadth : ";
cin>>l>>b;
}
friend float area(rectangle s);
};
float area(rectangle s)
{
return s.l*s.b;
}
int main()
{
rectangle s1;
s1.getdata();
cout<<"\narea of rectangle : "<<area(s1);
return 0;
}

********************* OUTPUT ***********************

44
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 23: WAP to demonstrate friend function as a bridge between two
class
#include <iostream>
using namespace std;

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

45
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
friend int add(ClassA, ClassB);
};

// access members of both classes


int add(ClassA objectA, ClassB objectB) {
return (objectA.numA + objectB.numB);
}

int main() {
ClassA objectA;
ClassB objectB;
cout << "Sum: " << add(objectA, objectB);
return 0;
}

********************* OUTPUT ***********************

46
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 24: WAP to exchange private values of two classes using friend
function
#include<iostream>
using namespace std;
class Derived;
class Base
{
protected:
int num1;
public:
Base()
{
num1=10;
}
void show()
{
cout<<"\n Value of Number 1 : "<<num1;
}
friend void swap(Base *num1, Derived *num2);
};
class Derived
{
protected:
int num2;
public:
Derived()
{
num2=20;
}
void show()
{

47
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout<<"\n Value of Number 2 : "<<num2;
}
friend void swap(Base *num1, Derived *num2);
};
void swap(Base *no1, Derived *no2)
{
int no3;
no3=no1->num1;
no1->num1=no2->num2;
no2->num2=no3;
}
int main()
{
Base b;
Derived d;
swap(&b, &d);
b.show();
d.show();
return 0;
}

********************* OUTPUT ***********************

48
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 25: WAP to demonstrate this pointer

#include <iostream>
using namespace std;
class sample
{ int x;
public:
void display()
{ cout<<"\nObject's address is"<<this; //display the address of the object that invokes member fuction
}
};
int main(){
sample s1,s2,s3;
s1.display();
s2.display();
s3.display();
return 0;
}

********************* OUTPUT ***********************

49
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 26: WAP to demonstrate array of object in classes

#include <iostream>

using namespace std;

class Rectangle
{
public:
int length;
int breadth;
Rectangle( int l, int b )
{
length = l;
breadth = b;
}
int printArea()
{
return length * breadth;
}
};

int main()
{ Rectangle rt1( 7, 4 );
Rectangle rt2( 4, 5 );
cout << "Area of first rectangle " << rt1.printArea() << endl;
cout << "Area of second rectangle " << rt2.printArea() << endl;
return 0;
}

50
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
********************* OUTPUT ***********************

51
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 27: WAP to demonstrate pointer to a constant
#include <iostream>
using namespace std;
void demo()
{
cout<<"this function is called using function pointer";
}
int main()
{
int const a=10;
const int *ptr1 = &a;
// ptr1++; //run perfectly
//a =11 ; //show error (assignment of read-only variable)
cout<<a <<endl<<ptr1 <<endl<<endl;
return(0);
}

********************* OUTPUT ***********************

52
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 28: WAP to constant pointer to a constant
#include <iostream>
using namespace std;
void demo()
{
cout<<"this function is called using function pointer";
}
int main()
{
int const c=15;
const int *const ptr3 = &c;
//ptr3++; //show error (increment of read-only variable)
// c= 35; //show error (assignment of read-only variable)
cout<<c <<endl <<ptr3 <<endl<<endl;

return 0;
}

********************* OUTPUT ***********************

53
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 29:WAP to demonstrate constant pointer
#include <iostream>
using namespace std;
void demo()
{
cout<<"this function is called using function pointer";
}
int main()
{
int b= 11;
int *const ptr2 = &b;
// ptr2++; //show error (increment of read-only variable)
// b= 25; //run perfectly
cout<<b <<endl <<ptr2 <<endl <<endl;
return 0;
}

********************* OUTPUT ***********************

54
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 30: WAP to demonstrate pointer to a function
#include <iostream>
using namespace std;
void demo()
{
cout<<"this function is called using function pointer";
}
int main()
{
void (*ptr4)() = &demo;
(*ptr4)();
return 0;
}

********************* OUTPUT ***********************

55
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 31. WAP to generate Fibonacci series of first “n” numbers

#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" ";
for(i=2;i<number;++i)
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
}

********************* OUTPUT ***********************

56
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 32: WAP to input indefinite number and calculate the sum of
positive number and program should terminate when number is negative
#include <iostream>
using namespace std;
int main()
{
int num=0;
int sum=0;
cout<<"enter the numbers to be added"<<endl;
while(num>=0){
cin>>num;
if (num<0)
break;
sum=sum+num;
}
cout<<"you entered a negative number"<<endl;
cout<<"the sum is: "<<sum;

return 0;
}

********************* OUTPUT ***********************


57
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22

58
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 33: WAP to count the number digits in a number
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number "<<endl;
cin>>n;
int count = 0;
while (n != 0) {
n = n / 10;
++count;
}
cout << "Number of digits : "<< count;

return 0;
}

********************* OUTPUT ***********************

59
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 34: WAP to demonstrate unary operator using friend function
#include <iostream>
using namespace std;
class UnaryFriend
{
int a = 10;
int b = 20;
int c = 30;

public:
void getvalues()
{
cout<<"Values of A, B & C\n";
cout<<a<<'\n'<<b<<'\n'<<c<<'\n'<<endl;
}
void show()
{
cout<<a<<"\n"<<b<<"\n"<<c<<"\n"<<endl;
}
void friend operator-(UnaryFriend &x);
};

void operator-(UnaryFriend &x)


{
x.a = -x.a;
x.b = -x.b;
x.c = -x.c;
}

int main()
{

60
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
UnaryFriend x1;
x1.getvalues();
cout<<"before overloading \n";
x1.show();
cout<<"after overloading \n";
-x1;
x1.show();
return 0;
}

********************* OUTPUT ***********************

61
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 35: WAP to show overloading of postfix & prefix increment
operator
#include <iostream>
using namespace std;
class op {
private:
int x;
public:
void getdata(int a){
x=a;
}
void operator++()
{
x=++x;
}
void operator ++(int)
{
x=x++;
}
void display()
{
cout << "x = " << x << endl;
}
};
int main()
{
op a;
a.getdata(2);
cout << "Before increment: "<<endl;
a.display();
++a;

62
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout << "After pre increment: "<<endl;
a.display();
a++;
cout << "After post increment: "<<endl;
a.display();
}

********************* OUTPUT ***********************

63
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 36: WAP to demonstrate binary operator overloading using member
function
#include <iostream>
using namespace std;
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);};
complex complex :: operator+(complex c)
{ complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return (temp);
}
void complex::display(void)
{cout<<x<<"+ j"<<y<<"\n";
}
int main(){
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;

64
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c3="; c3.display();
return 0;}

********************* OUTPUT ***********************

65
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 37:WAP to demonstrate overloading of function call operator
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
Distance operator()(int a, int b, int c) {
Distance D;
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main() {

66
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Distance D1(11, 10), D2;
cout << "First Distance : ";
D1.displayDistance();
D2 = D1(10, 10,10);
cout << "Second Distance :";
D2.displayDistance();
return 0;
}

********************* OUTPUT ***********************

67
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 38: WAP to demonstrate comma operator overloading

#include <iostream>
using namespace std;
// Comma class
class comma {
int x, y;
public:
// Default Constructor
comma() {}
// Parameterized Constructor
comma(int X, int Y)
{
x = X;
y = Y;
}

// Function to display the value


void display()
{
cout << "x=" << x << " ";
cout << "y=" << y << " ";
}

comma operator+(comma ob2);


comma operator, (comma ob2);
};

// Function to overload the + operator


comma comma::operator+(comma ob2)
{
68
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
comma temp;

// Update the value temp


temp.x = ob2.x + x;
temp.y = ob2.y + y;

// Return the value temp


return temp;
}

// Function to overload the, operator


comma comma::operator, (comma ob2)
{
comma temp;

// Update the value temp


temp.x = ob2.x;
temp.y = ob2.y;

// Print the value


cout << "x=" << ob2.x << " "
<< "y=" << ob2.y << endl;

// Return the value temp


return temp;
}

// Driver code
int main()
{
// Initialize objects

69
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
comma ob1(10, 20), ob2(5, 30), ob3(1, 1);

ob1.display();
ob2.display();
ob3.display();

cout << endl;

ob1 = (ob2 + ob2, ob1, ob3);

// Displays the value of


// ob3 (Rightmost expression)
ob1.display();

return 0;
}

********************* OUTPUT ***********************

70
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 39: WAP to relational operator overloading
#include <iostream>
using namespace std;
class Distance {
private:
int feet; // 0 to infinite
int inches; // 0 to 1
public:
// required constructors
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
void displayDistance() {
cout << "F: " << feet << " I:" << inches <<endl;
}
// overloaded minus (-) operator
Distance operator- () {
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
// overloaded < operator
bool operator <(const Distance& d) {
if(feet < d.feet) {
return true;
}

71
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
if(feet == d.feet && inches < d.inches) {
return true;
}
return false;
}
};
int main() {
Distance D1(11, 10), D2(5, 11);
if( D1 < D2 ) {
cout << "D1 is less than D2 " << endl;
} else {
cout << "D2 is less than D1 " << endl;
}
return 0;
}

********************* OUTPUT ***********************

72
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
Question 40: WAP to demonstrate single inheritance
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
char name[50];
int rollno;
public:
void read()
{
cout<<"\nrollno"<<"\n";
cin>>rollno;
cout<<"\n name";
cin>>name;
}
void show()
{
cout<<"\n rollno="<<rollno;
cout<<"\n name="<<name;

};
class result:public student
{
private:
int m1,m2,m3;
int sum=0;
float perc=0;

73
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
public:
void calculate()
{
cout<<"\n marks in c++,dbms,maths";
cin>>m1>>m2>>m3;
sum=m1+m2+m3;
perc=sum/300;

}
void display()
{
cout<<"\n c++="<<m1;
cout<<"\n dbms="<<m2;
cout<<"\n maths="<<m3<<"\n";
cout<<"sum="<<sum;
cout<<"\n percentage="<<perc;

}
};
int main()
{
result s;
cout<<"the percentage of a student\n";
s.read() ;
s.show() ;
s.calculate();
s. display();
return 0;
}

74
MAHARAJA SURAJMAL INSTITUTE BCA(E)19-22
********************* OUTPUT ***********************

75

You might also like