oopt-c-lab-r20-1
oopt-c-lab-r20-1
#include<iostream
public:
int feet,inch,x,y,z;
void input()
n"; cin>>feet>>inch;
void show()
cout<<feet<<" feet
"<<inch<<"inch\n";
feet=x.feet+y.fe
et;
inch=x.inch+y.in
ch; if(inch>=12)
};
int main()
dist x,y,z;x.input();
y.input();
z.sum(x,y);
z.show();
}
OUTPUT:
#include<iostream>
using namespace
std; class dist
{
public:
int
feet,inch;
dist()
{
}
dist (int x,int y)
{
feet=x;
inch =y;
}
dist (float m,float n)
{
feet=
m;
inch=
n;
}
void display()
{
cout<<"Resulted distance is: ";
cout<<feet<<" feet "<<inch<<"
inch\n";
}
void sum(dist obj1,dist obj2)
{
feet=obj1.feet+obj2.feet;
inch=obj1.inch+obj2.inch;
if(inch>=12)
{
OUTPUT:-
object deleted:
object deleted:
object deleted:
object
deleted:
object
deleted:
#include
<iostream> using
namespace std;
class Distance
{
private:
int feet;
int
inches;
public:
void set_distance()
{
cout<<"Enter feet:
"; cin>>feet;
cout<<"Enter
inches: ";
cin>>inches;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<",
inches="<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches +
d2.inches; feet = feet +
(inches / 12);
}
void add(Distance *d1, Distance *d2)
{
feet = d1->feet + d2->feet;
inches = d1->inches + d2->inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int
main()
{
Distance
d1,d2,d3;
d1.set_distance(
);
d2.set_distance(
);
d3.add(d1,d2);
Enter feet: 3
Enter inches: 2
Enter feet: 4
Enter inches: 5
Distance is feet= 7, inches= 7
Distance is feet= 7, inches= 7
#include
<iostream> using
namespace
std;class A
{
Public :
int x ;
friend class B; // friend class.
};
class B
{
public:
void display(A &a)
{
cout<<"value of x is : "<<a.x;
}
};
int main()
{
A a;
a.x=20
;
B b;
Clrscr();
b.display(a);
return 0;
}
Output:
Value of x is: 20
#include <iostream>
/* Local variable is same as a member
name. class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x =
20;
obj.setx(x
);
obj.print();
return 0;
}
OUTPUT:-
X=20
#include
<iostream> class A
{
private:
int
x; int
y;
public
:
A(int x, int y)
{
this->x =
x; this-
>y = y;
}
void display()
{
cout<<"x =
"<<x<<endl;
cout<<"y =
"<<y<<endl;
}
};
int main()
{
A *ptr = new A(10, 30);
//Here ptr is pointer
to class A
ptr-
>display();
return 0;
}
X =10
Y = 30
12
a) unary operators
#include<iostream>
class increment
int
a,b;
public
increment(int x,int y)
a=x;
b=y;
increment( )
void show( )
cout<<"a="<<a<<",b="
<< b<<endl;
};
void increment::operator
++( )
+a;
b=+
+b;
main( )
increment u(10,20);
cout<<"Before
increment u:";
u.show( );
++u;
cout<<"After
increment u:";
u.show( );
#include<iomanip>
class x
public:
int
a,b; x
(){}
a=x1;b=y1;
void show()
cout<<"a="<<a<<",b="<<b<<endl;
};
x x3;
x3.a=x1.a*x2.a;
x3.b=x1.b*x2.b;
return(x3);
main()
x3=x1*x2;
cout<<"Object x1:";
x1.show();
cout<<"Object x2:";
x2.show();
cout<<"Object x3:";
x3.show();
16
#include
<iostream> class
Distance {
private:
int feet;
int
inches;
public:
// required constructors
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches =
i;
}
void operator = (const Distance &D )
{
feet = D.feet;
inches =
D.inches;
}
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2(5, 11);
Downloaded by Hanumantharao Murukutla
17
OUTPUT:-
18
Downloaded by Hanumantharao Murukutla
9. C++ program to demonstrate example of single inheritance.
#include<iostrea
m>
#include<iomanip
>
#include<string.h
public:
int rno;
char
name[20];
char
group[5];
public:
void getdata()
group:"; cin>>rno>>name>>group;
void putdata()
cout<<"\n Roll
no:"<<rno; cout<<"\n
Name:"<<name;
cout<<"\n
Branch:"<<group;
public:
19
void
getmarks()
cin>>s1>>s2>>s3;
void putmarks()
cout<<"\n
subject1:"<<s1;
cout<<"\n
subject2:"<<s2;
cout<<"\n
subject3:"<<s3;
void findtotal()
int
total=s1+s1+s3
; float
avg=total/3;
putdata();
putmarks();
cout<<"\n Total
marks:"<<total; cout<<"\n
Average marks:"<<avg;
20
#include <iostream>
using namespace
private:
int num;
public:
void getnumber()
int returnnumber()
return num;
};
21
int getsquare()
int num,sqr;
num=returnnumber();
sqr=num*num;
return (sqr);
};
public:
int getcube(void)
int num,cube;
num=returnnumber();
cube=num*num*num;
return (cube);
};
main()
22
square s;
cube c;
s.getnumber();
cout << "Square of "<< s.returnnumber() << " is: " <<s.getsquare() << endl;
c.getnumber();
cout << "Cube of "<< c.returnnumber() << " is: " << c.getcube()<< endl;
// in single inheritance
#include <iostream>
using namespace
std;
// base class
class Parent
public:
// base class
constructor Parent()
23
Downloaded by Hanumantharao Murukutla
{
};
// sub class
public:
Child()
};
// main
function int
main() {
return 0;
24
Downloaded by Hanumantharao Murukutla
12. Write a C++ Program to illustrate template class
#include
<iostream> using
namespace std;
template<class T>
class A
public:
T num1 =
5; T num2
= 6; void
add()
};
int main()
A<int>
d;
d.add();
return
0;
OUTPUT:--
25
#include<iostream
std; template<class
E>
temp=a;
a=b;
b=temp;
};
int main()
int
x=5,y=8;
float a,b;
n”; cin>>a>>b;
ty=”<<y<<endl; exchange(x,y);
tb=”<<b<<endl; exchange(a,b);
tb=”<<b<<endl; return 0;
14. Write a C++ program illustrating user defined string processing functions (string
length, string copy, string concatenation).
String length:
#include <iostream.h>
char
text[MAX_SIZE];
int count = 0;
cin>>text;
cout<<"Length of "<<text<<" is
"<<count; return 0;
}
Downloaded by Hanumantharao Murukutla
27
VSMCOE Length of
VSMCOE is 6
String copy:
#include<iostream.h>
#include<stdio.h>
int main()
char strOrig[100],
origPtr =
&strOrig[0]; copPtr
= &strCopy[0];
while(*origPtr)
*copPtr =
*origPtr;
origPtr++;
copPtr++;
*copPtr = '\0';
cout<<"\nEntered String:
"<<strOrig; cout<<"\nCopied
cout<<endl;
28
OUTPUT:
codes cracker
string concatenation:
#include <iostream>
char str1[MAX_SIZE],
char * s2 = str2;
cout<<"Enter 2nd
str1 while(*(++s1));
while(*(s1++) = *(s2++));
cout<<"Concatenated
string:"<<str1; return 0;
29
Downloaded by Hanumantharao Murukutla
OUTPUT:
15. Write C++ program that implement Bubble sort, to sort a given list of integers
in ascending order.
#include<iostrea
cout<<" ";
cin>>arr[i];
if(arr[j]>arr[j+1])
30
arr[j]=arr[j+1
];
arr[j+1]=tem
p;
cout<<" ";
cout<<arr[i]<
<" ";
return 0;
OUTPUT:
12
45
98
63
99
31