Oops Notes
Oops Notes
Oops Notes
(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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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)
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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)
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:
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:
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:
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:
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:
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:
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:
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:
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:
67
Object Oriented Programming Lab. (BTCS-304-18)
1. What is OOPS?
1. Abstraction.
2. Encapsulation.
3. Inheritance.
4. Polymorphism.
3. What is a class?
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.
6. What is Polymorphism?
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.
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:
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.
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.
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.
Friend can be declared anywhere in the class declaration, and it cannot be affected by
access control keywords like private, public or protected.
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);
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.
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.
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.
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.
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)
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.
Even punctuation characters are also considered as tokens – Brackets, Commas, Braces
and Parentheses.
Overriding is the same method names with same arguments and return types associates
with the class and its child class.
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.
71
Object Oriented Programming Lab. (BTCS-304-18)
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.
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.
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
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.
There are three various types of constructors, and they are as follows:.
72
Object Oriented Programming Lab. (BTCS-304-18)
– Parametric Constructor – With Parameters. Create a new instance of a class and also
passing arguments simultaneously.
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.
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.
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.
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.
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)
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?
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.
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.
74
Object Oriented Programming Lab. (BTCS-304-18)
50. Which OOPS concept exposes only necessary information to the calling
functions?
75