Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Oops Notes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 75

Object Oriented Programming Lab.

(BTCS-304-18)

EXPERIMENT NO. 1
OBJECTIVE:
Wap to find reverse of a number

DESCRIPTION:
#include <iostream>
using namespace std;
int main()
{
int n, reversedNumber = 0, remainder;
cout <<"Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout <<"Reversed Number = "<< reversedNumber;
return 0;
}

OUTPUT:

Enter an integer: 12345


Reversed number = 54321

1
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 2
OBJECTIVE:
Wap to print Fibonacci series

DESCRIPTION:
#include<iostream>
usingnamespace std;
int main()
{
int n, t1 =0, t2 =1, nextTerm =0;
cout <<"Enter the number of terms: ";
cin >> n;
cout <<"Fibonacci Series: ";
for(int i =1; i <= n;++i)
{
// Prints the first two terms.
if(i ==1)
{
cout <<""<< t1;
continue;
}
if(i ==2)
{
cout << t2 <<"";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm <<"";
}
return0;
}

OUTPUT:

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

2
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 3
OBJECTIVE:
Wap to swap 2 numbers without using 3rd variable

DESCRIPTION:
#include<iostream>
usingnamespace std;
int main()
{
int a =5, b =10;
cout <<"Before swapping:"<< endl;
cout <<"a = "<< a <<", b = "<< b << endl;
a = a + b;
b = a - b;
a = a - b;
cout <<"\nAfter swapping:"<< endl;
cout <<"a = "<< a <<", b = "<< b << endl;
return0;
}

OUTPUT:

Before swapping:
a = 5, b = 10

After swapping:
a = 10, b = 5

3
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 4
OBJECTIVE:
Wap to print *
**
***
****
*****

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1; i<=6; i++)
{
for(j=1; j<i; j++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}

OUTPUT:

*
**
***
****
*****

4
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 5
OBJECTIVE:
Wap to find factorial of a number

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,no, fact=1;
clrscr();
cout<<"Enter the any no. : ";
cin>>no;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
cout<<"Factorial: "<<fact;
getch();
}

OUTPUT:

Enter the any no. : 4


Factorial: 24

5
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 6
OBJECTIVE:
Wap to check number is palindrome or not

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,no,b,temp=0;
clrscr();
cout<<"Enter any num: ";
cin>>no;
b=no;
while(no>0)
{
a=no%10;
no=no/10;
temp=temp*10+a;
}
if(temp==b)
{
cout<<"Palindrome";
}
else
{
cout<<"Not Palindrome";
}
getch();
}

OUTPUT:

Enter any num: 143


Not Palindrome

Enter any num: 141


Palindrome

6
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 7
OBJECTIVE:
Wap to print table of any number

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,no,table=1;
clrscr();
cout<<"Enter any num : ";
cin>>no;
clrscr();
cout<<"Table of "<<no;
for(i=1;i<=10;i++)
{
table=no*i;
cout<<" \n"<<table;
cout<<"\n";
}
getch();
}

OUTPUT:

Enter any num : 5


Table of 5
5
10
15
20
25
30
35
40
45
50

7
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 8
OBJECTIVE:
Wap to find ASCII value of a number

DESCRIPTION:
#include<iostream.h>
#include<ctype.h>
#include<conio.h>
int main(void)
{
int number, result;
clrscr();
cout<<"Enter any Character/Symbol/Digits: ";
number = getch();
result = toascii(number);
cout<<"Ascii value: "<<result;
getch();
}

OUTPUT:

Enter any Character/Symbol/Digits:a


Ascii value: 97

8
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 9
OBJECTIVE:
Wap to find average of two numbers

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
float avg;
cout<<"Enter two numbers : ";
cin>>a>>b;
avg=(a+b)/2;
cout<<”Average of 2 numbers is:”<<avg;
getch();
}

OUTPUT:

Enter two numbers: 4 6


Average of 2 numbers is: 5

9
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 10
OBJECTIVE:
Wap to find entered number is even or odd

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
intno;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no%2==0)
{
cout<<"Even num";
}
else
{
cout<<"Odd num";
}
getch();
}

OUTPUT:

Enter any num : 5


Odd num

10
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 11
OBJECTIVE:
Wap to find and print biggest of 3 numbers

DESCRIPTION:
#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a, b, c;
cout <<"Enter any three numbers: ";
cin>>a;
cin>>b
cin>>c;
if(a>=b && a>=c)
{
cout<<"Largest number: "<<a;
}
if(b>=a && b>=c)
{
cout<<"Largest number: "<<b;
}
if(c>=a && c>=b)
{
cout<<"Largest number: "<<c;
}
getch();
}

OUTPUT:

Enter any three numbers:


15
30
20
Largest number: 30

11
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 12
OBJECTIVE:
Wap to find a day of week

DESCRIPTION:
#include<iostream>
using namespace std;
int main()
{
int day;
cout<<"Enter the day of the week: ";
cin>>day;
switch(day)
{
case 1:
cout<<"Day is Monday";
break;
case 2:
cout<<"Day is Tuesday";
break;
case 3:
cout<<"Day is Wednesday";
break;
case 4:
cout<<"Day is Thursday";
break;
case 5:
cout<<"Day is Friday";
break;
case 6:
cout<<"Day is Saturday";
break;case 7:cout<<"Day is Sunday";break;
default: cout<<"Invalid Choice";}}

OUTPUT:

Enter the day of the week: 4 Day is Thursday

12
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 13
OBJECTIVE:
Wap to find whether a number is prime or not

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no==1)
{
cout<<"Smallest prime num is 2";
}
for(i=2;i<no;i++)
{
if(no%i==0)
{
cout<<"Not prime num";
break;
}
}
if(no==i)
{
cout<<"Prime num";
}
getch();
}

OUTPUT:

Enter any num: 10


Not Prime Num

13
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 14
OBJECTIVE:
Wap to check leap year

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int y;
cout<<"Enter a year: ";
cin>>y;
if(y%4==0)
{
cout<<"Leap Year";
}
else
{
cout<<"Not a Leap Year";
}
getch();
}

OUTPUT:

Enter a year: 2004


Leap Year

14
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 15
OBJECTIVE:
Wap to print all Armstrong numbers

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int arm=0,a,b,c,d,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
d=no;
while(no>0)
{
a=no%10;
no=no/10;
arm=arm+a*a*a;
}
if(arm==d)
{
cout<<"Armstrong";
}
else
{
cout<<"not Armstrong";
}
getch();
}

OUTPUT:

Enter any number: 23


not Armstrong

15
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 16
OBJECTIVE:
Wap to sort elements of array in ascending order

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
{
cin>>a[i];
}
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
{
cout<<a[j];
}
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;}}}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++){ cout<<a[j];} getch();}

OUTPUT:

Enter any 10 num in array:


2 5 1 7 5 3 8 9 11 4
Data After Sorting: 1 2 3 4 5 7 8 9 11

16
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 17
OBJECTIVE:
Wap to add 2 matrix

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int x[3][3],y[3][3],z[3][3],i,j;
clrscr();
cout<<"ENTER ELEMENTS OF 1st MATRIX\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>x[i][j];
}
cout<<"ENTER ELEMENTS OF 2nd MATRIX\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>y[i][j];
}
cout<<"MATRIX [X]";
for(i=0;i<3;i++)
{
cout<<"\n\n";
for(j=0;j<3;j++)
cout<<x[i][j];
}
cout<<"\nMATRIX [Y]";
for(i=0;i<3;i++)
{
cout<<"\n\n";
for(j=0;j<3;j++)
cout<<y[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
z[i][j]=x[i][j]+y[i][j];
}
cout<<"\nMATRIX [Z]";
for(i=0;i<3;i++)
17
Object Oriented Programming Lab. (BTCS-304-18)

{
cout<<"\n\n";
for(j=0;j<3;j++)
cout<<z[i][j];
}
getch();
}

18
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 18
OBJECTIVE:
Wap to find sum of an array element

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[20],i,n,sum=0;
clrscr();
cout<<"How many elements you want to enter: ";
cin>>n;
cout<<"Enter any "<<n<<" elements in Array: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Sum of all Elements are: ";

for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
for(i=0;i<n;i++)
{
}
cout<<sum;
getch();
}

OUTPUT:

How many elements you want to enter : 5


Enter any 5 elements in Array:
14275
Sum of all Elements are: 19

19
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 19
OBJECTIVE:
Wap to find length of a string

DESCRIPTION:
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int i,count=0;
char ch[20];
clrscr();
cout<<"Enter any string: ";
gets(ch);
for(i=0;ch[i]!='\0';i++)
{
count++;
}
cout<<"String Length: "<<count;
getch();
}

OUTPUT:

Enter any String: hitesh


String Length: 6

20
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 20
OBJECTIVE:
Wap to compare 2 strings

DESCRIPTION:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],i,j,flag=0;
clrscr();
cout<<"Enter first string: ";
gets(str1);
cout<<"Enter Second string: ";
gets(str2);
i=0;
j=0;
while(str1[i]!='\0')
{
i++;}
while(str2[j]!='\0')
{
j++;}
if(i!=j)
{
flag=0;}
else
{
for(i=0,j=0;str1[i]!='\0',str2[j]!='\0';i++,j++)
{
if(str1[i]==str2[j])
{
flag=1;}}}
if(flag==0){
cout<<"Strings are not equal";}
else{
cout<<"Strings are equal";} getch();}

21
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 21
OBJECTIVE:
Wap to reverse a string

DESCRIPTION:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100],temp;
int i,j=0;
clrscr();
cout<<"Enter any the string :";
gets(str);// gets function for input string
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
cout<<"Reverse string is: "<<str;
getch();
}

OUTPUT:

Enter any the string : hitesh


Reverse string is : hsetih

22
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 22
OBJECTIVE:
Wap to concatenate 2 strings

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char*ch1="john";
char*ch2="porter";
char*ptr;
clrscr();
cout<<"1 st String: "<<ch1;
cout<<"\n 2 nd String: "<<ch2;
strcat(ch1,ch2);
cout<<"\nCombined string is: "<<ch1;
getch();
}

OUTPUT:

1 st String: john
2 nd String: porter
Combined string is: johnporter

23
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 23
OBJECTIVE:
Wap to copy one string to another

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10], s2[10];
clrscr();
cout<<"Enter string s1: ";
cin>>s1;
strcpy(s2, s1);
cout<<"String s2: "<<s2;
getch();
}

OUTPUT:

Enter string s1: Kumar


String s2: Kumar

24
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 24
OBJECTIVE:
Wap that uses a class where member functions are defined inside the class

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b;
public:
void get()
{
cout<<”Enter the first number a=”;
cin>>a;
cout<<endl;
cout<<”Enter the second number b=”;
cin>>b;
cout<<endl;
}
void largest()
{
if(a>b)
{
cout<<"a is greater";
}
else
cout<<"b is greater";
}
void display()
{
largest();
}
};
void main()
{
clrscr();
abc x;
x.get();
x.display();
getch();
}

25
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 24.1 Output of Functions defined inside the class

26
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 25
OBJECTIVE:
Wap that uses a class where member functions are defined outside the class

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class outside
{
int x,y,s;
public:
void get();
void sum();
void show();
};
void outside::get()
{
cout<<"enter the values of x and y: "<<endl;
cout<<"x= ";
cin>>x;
cout<<"y= ";
cin>>y;
}
void outside::sum()
{
s=x+y;
}
void outside::show()
{
cout<<"the sum of two numbers is= ";
cout<<s;
}
void main()
{
clrscr();
outside p;
p.get();
p.sum();
p.show();
getch();
}

OUTPUT:

27
Object Oriented Programming Lab. (BTCS-304-18)

Fig. 25.1 Output of Member Functions defined Outside the Class

28
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 26
OBJECTIVE:
Wap to demonstrate use of static data members

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
Class abc
{
Static int count;
int number;
public:
void get(int a)
{
number=a;
count++;
}
void show()
{
cout<<"number="<<number<<endl;
cout<<"count="<<count<<endl;
}
};
int abc::count;
void main()
{
clrscr();
abc p,q,r;
p.get(30);
q.get(40);
r.get();
p.show();
q.show();
r.show();
getch();
}

29
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 26.1 Output of Static data members

30
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 27
OBJECTIVE:
Wap to demonstrate use of constant data members

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class abc
{
int data;
public:
void assign()
{
data=20;
}
void changedata()const
{
//data=40;
cout<<"Can't change the data of constant member function! "<<endl;
}
void show()
{
cout<<"Data= "<<data<<endl;
}
};
void main()
{
clrscr();
abc s;
s.assign();
s.show();
s.changedata();
s.show();
getch();
}

31
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 27.1 Output of Constant data members

32
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 28
OBJECTIVE:
Wap to demonstrate use of zero argument and parameterized constructor

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class def
{
Int x,y;
public:
def(inta,int b=0)
{
x=a;
y=b;
}
void show()
{
cout<<x<<endl<<y<<endl;
}
};
void main()
{
clrscr();
def p(5);
def q(5,6);
p.show();
cout<<endl;
q.show();
getch();
}

33
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 28.1 Output of Zero argument and Parameterized Constructors

34
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 29
OBJECTIVE:
Wap to demonstrate use of dynamic constructor

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class test
{
int*p;
public:
test(int a1)
{
p=new int;
*p=a1;
}
void display()
{
cout<<*p;
}
};
void main()
{
clrscr();
test t1(10);
t1.display();
}

35
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 29.1 Output of Dynamic Constructors

36
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 30
OBJECTIVE:
Wap to demonstrate use of explicit constructor

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class A
{
inta,b;
public:
A (intx,int y)
{
a=x;
b=y;
cout<<”The values of a and b are: “;
cout<<a<<endl;
cout<<b<<endl;
}
void sum()
{
cout<<"sum is="<<a+b;
}
};
void main()
{
clrscr();
A a1=A(20,30);
cout<<endl;
a1.sum();
getch();
}

37
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 30.1 Output of Explicit Constructor

38
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 31
OBJECTIVE:
Wap to demonstrate use of initializer list

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class example
{
const float pie;
int k;
public:
example(int b,float a):k(b),pie(a)
{
}
void show()
{
cout<<"pie= "<<pie<<endl;
cout<<"k= "<<k<<endl;
}
};
void main()
{
clrscr();
example obj(10,3.14);
obj.show();
getch();
}

39
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 31.1 Output of Initializer List

40
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 32
OBJECTIVE:
Wap to show operator overloading of increment and decrement operators

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class complex
{
intr,i;
public:
void get()
{
cout<<"Enter the original values: "<<endl;
cout<<"r= ";
cin>>r;
cout<<"i= ";
cin>>i;
}
void operator ++()
{
++r;
++i;
}
void operator --()
{
--r;
--i;
}
void show()
{
cout<<"The value of number after the operation is: "<<endl;
cout<<r<<"+i"<<i<<endl;
}
};
void main()
{
clrscr();
complex c1;
c1.get();
c1++;
c1.show();
cout<<endl;
c1.get();
41
Object Oriented Programming Lab. (BTCS-304-18)

c1--;
c1.show();
getch();
}

OUTPUT:

Fig. 32.1 Output of operator overloading of Increment-Decrement Operators

42
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 33
OBJECTIVE:
Wap to show operator overloading of arithmetic operators

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class complex
{
intr,i;
public:
complex()
{}
complex(inta,int b)
{
r=a;
i=b;
}
void show()
{
cout<<r<<”+i”<<i<<endl;
}
complex operator+(complex c)
{
complex temp;
temp.r=r+c.r;
temp.i=i+c.i;
return temp;
}
};
void main()
{
clrscr();
complex c1(3,4);
complex c2(4,5);
c1.show();
c2.show();
getch();
}

43
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 33.1 Output of operator overloading of Arithmetic operators

44
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 34
OBJECTIVE:
Wap to demonstrate typecasting of basic type to class type

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
sample(int a)
{
x=a;
}
void show()
{
cout<<x<<endl;
}
};
void main()
{
clrscr();
sample s1(10);
cout<<"The number x= ";
s1.show();
int c=20;
cout<<endl;
cout<<"Converting basic type to class type: "<<endl;
cout<<endl;
cout<<"New value assigned to x= ";
s1=c;
s1.show();
getch();
}

45
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 34.1 Output of typecasting of basic type to class type

46
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 35
OBJECTIVE:
Wap to demonstrate typecasting of class type to basic type

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class sample
{
public:
operatorint()
{int sum;
sum=10;
return sum;
}
};
void main()
{
clrscr();
sample s1;
int x=20;
cout<<"Initialized value of x= ";
cout<<x<<endl;
cout<<endl;
cout<<"After converting class type to basic type:"<<endl;
cout<<endl;
cout<<"The value of x= ";
x=int(s1);
cout<<x<<endl;
getch();
}

47
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 35.1 Output of typecasting of class type to basic type

48
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 36
OBJECTIVE:
Wap to demonstrate typecasting of one class type to another class type

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class minutes
{
double m;
public:
minutes(double x)
{
m=x;
}
double display()
{
return m;
}
};
class seconds
{
double s;
public:
seconds(double y)
{
s=y;
}
void display()
{
cout<<"s= "<<s<<endl;
}
operator minutes()
{
return minutes(s/60);
}
seconds(minutes m)
{
s=m.display()*60;
}
};
void main()
{
clrscr();
49
Object Oriented Programming Lab. (BTCS-304-18)

cout<<"using conversion functiom: "<<endl;


seconds sec=60;
minutes min=sec;
sec.display();
cout<<"minutes min= ";
cout<<min.display();
cout<<endl;
cout<<"using constructor: "<<endl;
minutes min1=1;
seconds sec1=min1 ;
cout<<"minutes min= ";
cout<<min1.display();
getch();
}

OUTPUT:

Fig. 36.1 Output of typecasting of one class type to another class type

50
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 37
OBJECTIVE:
Wap to demonstrate use of multilevel inheritance

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class B
{
protected:
char name[20];
introll_no;
public:
voidgetB()
{
cout<<"Enter the student data:";
cin>>name>>roll_no;
}
voidshowB()
{
cout<<endl;
cout<<"Name of the student is: ";
cout<<name<<endl;
cout<<"The roll no of the student is:";
cout<<roll_no<<endl;
}
};
class D1:public B
{
protected:
float m1;
float m2;
public:
void getD1()
{
cin>>m1>>m2;
}
void showD1()
{
cout<<"Marks of first subject:";
cout<<m1<<endl;
cout<<"Marks of second subject:";
cout<<m2<<endl;
}
51
Object Oriented Programming Lab. (BTCS-304-18)

};
class D2:public D1
{
float total;
public:
void add()
{
total=m1+m2;
cout<<"The total of marks is: ";
cout<<total<<endl;
}
};
void main()
{
clrscr();
D2 s;
s.getB();
s.getD1();s.showB();
s.showD1();s.add();
getch();}

OUTPUT:

Fig. 37.1 Output of multilevel inheritance

52
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 38
OBJECTIVE:
Wap to demonstrate use of multiple inheritance

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class student
{
protected:
char name[20];
introll_no;
public:
void gets()
{
cout<<"Enter the student data:";
cin>>name>>roll_no;
}
void shows()
{
cout<<endl;
cout<<"Name of the student is: ";
cout<<name<<endl;
cout<<"The roll no of the student is:";
cout<<roll_no<<endl;
}
};
class marks
{
protected:
float m1;
float m2;
public:
voidgetm()
{
cin>>m1>>m2;
}
voidshowm()
{
cout<<"Marks of first subject:";
cout<<m1<<endl;
cout<<"Marks of second subject:";
cout<<m2<<endl;
}
53
Object Oriented Programming Lab. (BTCS-304-18)

};
classdata:publicstudent,public marks
{
float total;
public:
void add()
{
total=m1+m2;
cout<<"The total of marks is: ";
cout<<total<<endl;
}
};
void main()
{
clrscr();
data s;
s.gets();
s.getm();
s.shows();s.showm();
s.add();getch();}

OUTPUT:

Fig. 38.1 Output of multiple inheritance

54
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 39
OBJECTIVE:
Wap to demonstrate use of virtual derivation of class

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class x
{
public:
void showx()
{
cout<<"show of class x"<<endl;
}
};
class y:virtual public x
{
public:
void showy()
{
cout<<"show of class y"<<endl;
}
};
class z:virtual public x
{
public:
void showz()
{
cout<<"show of class z"<<endl;
}
};
class w : public y, public z
{
public:
void show1()
{
cout<<"show of class w"<<endl;
}
};
void main()
{
clrscr();
w obj;
obj.showx();
55
Object Oriented Programming Lab. (BTCS-304-18)

obj.showy();
obj.showz();
obj.show1();
getch();
}

OUTPUT:

Fig. 39.1 Output of virtual derivation of a class

56
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 40
OBJECTIVE:
Wap to demonstrate the concept of runtime polymorphism

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
class B
{
public:
virtual void show()
{
cout<<"show of B class"<<endl;
}
};
class D1: public B
{
void show()
{
cout<<"show of class D1"<<endl;
}
};
class D2: public B
{
void show()
{
cout<<"show of class D2"<<endl;
}
};
void main()
{
B*p;
B B1;
p=& B1;
p->show();
D1 obj;
p=&obj;
p->show();
D2 obj1;
p=&obj1;
p->show();
getch();
}

57
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 40.1 Output of runtime polymorphism

58
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 41
OBJECTIVE:
Wap to demonstrate the concept of exception handling

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the values: "<<endl;
cout<<"a= ";
cin>>a;
cout<<"b= ";
cin>>b;
c=a-b;
try
{
if(c!=0)
{
cout<<"The result of a/x is= "<<a/c<<endl;
}
else
{
throw(c);
}
}
catch(int x)
{
cout<<"Exeption has occured "<<c;
}
getch();
}

59
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 41.1 Output of exception handling

60
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 42
OBJECTIVE:
Wap to demonstrate the use of function template

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
template<class t>
void compare(t a,t b)
{
t largest;
if(a>b)
{
largest=a;
}
else
largest=b;
cout<<"largest="<<largest;
}
void main()
{
clrscr();
int i,j;
cout<<"Enter the integer values: "<<endl;
cin>>i>>j;
compare(i,j);
float p,q;
cout<<endl;
cout<<"Enter the float values: "<<endl;
cin>>p>>q;
compare(p,q);
getch();
}

61
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 42.1 Output of Function template

62
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 43
OBJECTIVE:
Wap to demonstrate the use of class template

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
template<class t>
class sum
{
t a, b,c;
public:
void get();
void add();
};
template<class t>
void sum<t>::get()
{
cin>>a>>b;
}
template<class t>
void sum<t>::add()
{
cout<<"The sum of two numbers is: ";
c=a+b;
cout<<c<<endl;
cout<<endl;
}
void main()
{
clrscr();
sum<int>obj;
sum<float>obj1;
cout<<"Enter the integer values: "<<endl ;
obj.get();
obj.add();
cout<<"Enter the float values: "<<endl ;
obj1.get();
obj1.add();
getch();
}

63
Object Oriented Programming Lab. (BTCS-304-18)

OUTPUT:

Fig. 43.1 Output of class template

64
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 44
OBJECTIVE:
Wap to demonstrate the reading and writing of strings

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ofstream outfile("result");
outfile<<"nidhi"<<endl;
outfile<<"marks"<<endl;
outfile<<"450"<<endl;
outfile.close();
ifstream infile("result");
char name[40],name1[40],name2[40];
infile>>name;
infile>>name1;
infile>>name2;
cout<<"Name of the student is: "<<name<<endl;
cout<<name1<<endl; cout<<name2<<endl; infile.close(); getch();}

OUTPUT:

Fig. 44.1 Output of reading and writing of strings

65
Object Oriented Programming Lab. (BTCS-304-18)

EXPERIMENT NO. 45
OBJECTIVE:
Wap to demonstrate the reading and writing of objects

DESCRIPTION:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
protected:
char name[20];
int rn;
public:
void get()
{
cout<<"roll no.= ";
cin>>rn;
cout<<"enter name= ";
cin>>name;
}
void show()
{
cout<<"roll no="<<rn<<endl;
cout<<"name="<<name<<endl;
}
};

void main()
{
clrscr();
{
student std;
fstream file;
file.open("test.dat",ios::out);
for(int i=1;i<=4;i++)
{
cout<<"student data"<<endl;
std.get();
file.write((char*)&std,sizeof(std));
}
file.close();
file.open("test.dat",ios::in);
file.read((char*)&std,sizeof(std));
66
Object Oriented Programming Lab. (BTCS-304-18)

while(!file.eof())
{
cout<<"student is: ";
std.show();
file.read((char*)&std,sizeof(std));
}
getch();
}

OUTPUT:

Fig. 45.1 Output of reading and writing of objects

67
Object Oriented Programming Lab. (BTCS-304-18)

VIVA VOCE QUESTIONS

1. What is OOPS?

OOPS is abbreviated as Object Oriented Programming system in which programs are


considered as a collection of objects. Each object is nothing but an instance of a class.

2. Write basic concepts of OOPS?

Following are the concepts of OOPS and are as follows:.

1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.

3. What is a class?

A class is simply a representation of a type of object. It is the blueprint/ plan/ template


that describe the details of an object.

4. What is an object?

Object is termed as an instance of a class, and it has its own state, behaviour and identity.

5. What is Encapsulation?

Encapsulation is an attribute of an object, and it contains all data which is hidden. That
hidden data can be restricted to the members of that class.

Levels are Public, Protected, Private, Internal and Protected Internal.

6. What is Polymorphism?

Polymorphism is nothing but assigning behaviour or value in a subclass to something that


was already declared in the main class. Simply, polymorphism takes more than one form.

7. What is Inheritance?

Inheritance is a concept where one class shares the structure and behaviour defined in
another class. If inheritance applied on one class is called Single Inheritance, and if it
depends on multiple classes, then it is called multiple Inheritances.

8. What are manipulators?

68
Object Oriented Programming Lab. (BTCS-304-18)

Manipulators are the functions which can be used in conjunction with the insertion (<<)
and extraction (>>) operators on an object. Examples are endl and setw.

9. Define a constructor?

Constructor is a method used to initialize the state of an object, and it gets invoked at the
time of object creation. Rules for constructor are:

 Constructor Name should be same as class name.


 Constructor must have no return type.

10. Define Destructor?

Destructor is a method which is automatically called when the object is made of scope or
destroyed. Destructor name is also same as class name but with the tilde symbol before
the name.

11. What is Inline function?

Inline function is a technique used by the compilers and instructs to insert complete body
of the function wherever that function is used in the program source code.

12. What is a virtual function?

Virtual function is a member function of class and its functionality can be overridden in
its derived class. This function can be implemented by using a keyword called virtual, and
it can be given during function declaration.

Virtual function can be achieved in C++, and it can be achieved in C Language by using
function pointers or pointers to function.

13. What is friend function?

Friend function is a friend of a class that is allowed to access to Public, private or


protected data in that same class. If the function is defined outside the class cannot access
such information.

Friend can be declared anywhere in the class declaration, and it cannot be affected by
access control keywords like private, public or protected.

14. What is function overloading?

Function overloading is defined as a normal function, but it has the ability to perform
different tasks. It allows creation of several methods with the same name which differ
from each other by type of input and output of the function.

69
Object Oriented Programming Lab. (BTCS-304-18)

Example
void add(int& a, int& b);
void add(double& a, double& b);
void add(struct bob& a, struct bob& b);

15. What is operator overloading?

Operator overloading is a function where different operators are applied and depends on
the arguments. Operator,-,* can be used to pass through the function, and it has their own
precedence to execute.

16. What is an abstract class?

An abstract class is a class which cannot be instantiated. Creation of an object is not


possible with abstract class, but it can be inherited. An abstract class can contain only
Abstract method. Java allows only abstract method in abstract class while for other
language it allows non-abstract method as well.

17. What is a ternary operator?

Ternary operator is said to be an operator which takes three arguments. Arguments and
results are of different data types, and it is depends on the function. Ternary operator is
also called as conditional operator.

18. What is the use of finalize method?

Finalize method helps to perform cleanup operations on the resources which are not
currently used. Finalize method is protected, and it is accessible only through this class or
by a derived class.

19. What are different types of arguments?

A parameter is a variable used during the declaration of the function or subroutine and
arguments are passed to the function, and it should match with the parameter defined.
There are two types of Arguments.

 Call by Value – Value passed will get modified only inside the function, and it
returns the same value whatever it is passed it into the function.
 Call by Reference – Value passed will get modified in both inside and outside the
functions and it returns the same or different value.

20. What is super keyword?

Super keyword is used to invoke overridden method which overrides one of its super-
class methods. This keyword allows to access overridden methods and also to access
hidden members of the super-class.
70
Object Oriented Programming Lab. (BTCS-304-18)

It also forwards a call from a constructor to a constructor in the super-class.

21. What is method overriding?

Method overriding is a feature that allows sub class to provide implementation of a


method that is already defined in the main class. This will overrides the implementation
in the super-class by providing the same method name, same parameter and same return
type.

22. What is an interface?

An interface is a collection of abstract method, if the class implements an inheritance, and


then thereby inherits all the abstract methods of an interface.

23. What is exception handling?

Exception is an event that occurs during the execution of a program. Exceptions can be of
any type – Run time exception, Error exceptions. Those exceptions are handled properly
through exception handling mechanism like try, catch and throw keywords.

24. What are tokens?

Token is recognized by a compiler and it cannot be broken down into component


elements. Keywords, identifiers, constants, string literals and operators are examples of
tokens.

Even punctuation characters are also considered as tokens – Brackets, Commas, Braces
and Parentheses.

25. Difference between overloading and overriding?

Overloading is static binding whereas Overriding is dynamic binding. Overloading is


nothing but the same method with different arguments, and it may or may not return the
same value in the same class itself.

Overriding is the same method names with same arguments and return types associates
with the class and its child class.

26. Difference between class and an object?

An object is an instance of a class. Objects hold any information, but classes don’t have
any information. Definition of properties and functions can be done at class and can be
used by the object.

Class can have sub-classes, and an object doesn’t have sub-objects.

71
Object Oriented Programming Lab. (BTCS-304-18)

27. What is an abstraction?

Abstraction is a good feature of OOPS, and it shows only the necessary details to the
client of an object. Means, it shows only necessary details for an object, not the inner
details of an object. Example – When you want to switch on television, it not necessary to
show all the functions of TV. Whatever is required to switch on TV will be showed by
using abstract class.

28. What are access modifiers?

Access modifiers determine the scope of the method or variables that can be accessed
from other various objects or classes. There are 5 types of access modifiers, and they are
as follows:.

 Private.
 Protected.
 Public.
 Friend.
 Protected Friend.

29. What is sealed modifiers?

Sealed modifiers are the access modifiers where it cannot be inherited by the methods.
Sealed modifiers can also be applied to properties, events and methods. This modifier
cannot be applied to static members.

30. How can we call the base method without creating an instance?

Yes, it is possible to call the base method without creating an instance. And that method
should be,.

Static method

Doing inheritance from that class-Use Base Keyword from derived class

31. What is the difference between new and override?

The new modifier instructs the compiler to use the new implementation instead of the
base class function, whereas, Override modifier helps to override the base class function.

32. What are the various types of constructors?

There are three various types of constructors, and they are as follows:.

– Default Constructor – With no parameters.

72
Object Oriented Programming Lab. (BTCS-304-18)

– Parametric Constructor – With Parameters. Create a new instance of a class and also
passing arguments simultaneously.

– Copy Constructor – This creates a new object as a copy of an existing object

33. What is early and late binding?

Early binding refers to assignment of values to variables during design time whereas late
binding refers to assignment of values to variables during run time.

34. What is ‘this’ pointer?

THIS pointer refers to the current object of a class. THIS keyword is used as a pointer
which differentiates between the current object with the global object. Basically, it refers
to the current object.

35. What is the difference between structure and a class?

Structure default access type is public, but class access type is private. A structure is used
for grouping data whereas class can be used for grouping data and methods. Structures
are exclusively used for data and it doesn’t require strict validation, but classes are used to
encapsulates and inherit data which requires strict validation.

36. What is the default access modifier in a class?

The default access modifier of a class is Private by default.

37. What is pure virtual function?

A pure virtual function is a function which can be overridden in the derived class but
cannot be defined. A virtual function can be declared as Pure by using the operator =0.

38. What are all the operators that cannot be overloaded?

Following are the operators that cannot be overloaded -.

1. Scope Resolution (:: )


2. Member Selection (.)
3. Member selection through a pointer to function (.*)

39. What is dynamic or run time polymorphism?

Dynamic or Run time polymorphism is also known as method overriding in which call to
an overridden function is resolved during run time, not at the compile time. It means
having two or more methods with the same name, same signature but with different
implementation.
73
Object Oriented Programming Lab. (BTCS-304-18)

40. Do we require parameter for constructors?

No, we do not require parameter for constructors.

41. What is a copy constructor?

This is a special constructor for creating a new object as a copy of an existing object.
There will be always only on copy constructor that can be either defined by the user or
the system.

42. What does the keyword virtual represented in the method definition?

It means, we can override the method.

43. Whether static method can use non static members?

False

44. What are base class, sub class and super class?

Base class is the most generalized class, and it is said to be a root class.

Sub class is a class that inherits from one or more base classes.

Super class is the parent class from which another class inherits.

45. What is static and dynamic binding?

Binding is nothing but the association of a name with the class. Static binding is a binding
in which name can be associated with the class during compilation time, and it is also
called as early Binding.

Dynamic binding is a binding in which name can be associated with the class during
execution time, and it is also called as Late Binding.

46. How many instances can be created for an abstract class?

Zero instances will be created for an abstract class.

47. Which keyword can be used for overloading?

Operator keyword is used for overloading.

48. What is the default access specifier in a class definition?

74
Object Oriented Programming Lab. (BTCS-304-18)

Private access specifier is used in a class definition.

49. Which OOPS concept is used as reuse mechanism?

Inheritance is the OOPS concept that can be used as reuse mechanism.

50. Which OOPS concept exposes only necessary information to the calling
functions?

Data Hiding / Abstraction

75

You might also like