Oops Practicals
Oops Practicals
#include <iostream>
using namespace std;
int main() {
int num1,num2;
float float1,float2;
char char1,char2;
cout<<"Enter the first number:";
cin>>num1;
cout<<"Enter the second number:";
cin>>num2;
int sumintegers = num1+num2;
cout<<"Sum of integer:"<<sumintegers<<endl;
cout<<"Enter the first float:";
cin>>float1;
cout<<"Enter the second float:";
cin>>float2;
float sumfloats = float1+float2;
cout<<"Sum of float:"<<sumfloats<<endl;
cout<<"Enter the first character:";
cin>>char1;
cout<<"Enter the second character:";
cin>>char2;
char sumcharacters = char1+char2;
cout<<"Sum of character:"<<sumcharacters<<endl;
return 0;
}
Output--------
Enter the first number:4
Enter the second number:3
Sum of integer:7
Enter the first float:5.6
Enter the second float:3.2
Sum of float:8.8
Enter the first character:g
Enter the second character:k
Sum of character:gk
Output--------
Enter value of A::3
Enter value of B::6
before swapping value of ::
A=3 B=6
inside function after swapping value of ::
A=6 B=3
outside function after swapping value of ::
A=3 B=6
output--------
Before swapping:
a=1
b=2
After swapping:
a=2
b=1
Output---------
Enter the number of elements:3
Enter3elements:
1
2
3
Elements in reverse order:
321
Program 4--------
#include <iostream>
using namespace std;
double power (double, int =2);
int main() {
int p;
double n,r;
cout<<"Enter number:";
cin>>n;
cout<<"Enter exponent:";
cin>>p;
r=power(n,p);
cout<<"Result is :"<<r;
cout<<"\n Result without passing exponent is "<<r;
return 0;
}
double power (double a, int b)
{
double x=1;
for(int i=1;i<=b;i++)
x=x*a;
return x;
}
Output---------
Enter number:5
Enter exponent:4
Result is :625
Result without passing exponent is 625
Output--------
1000
157.26
112500
Program 6: traverse an array using pointer notations and also copy contents of one
array into another---------
#include <iostream>
using namespace std;
int main()
{
int*p;
int a[4]={44,55,6,4},b[4];
p=a;
p=&a[0];
for(int i=0;i<4;i++)
{
cout<<*(a+i)<<"\n";
}
for(int i=0;i<4;i++)
{
*(b+i)=*(b+i);
cout<<*(b+i);
}
return 0;
}
Output----------
44
55
6
4
445564
Program 7: design box class with three data mem bers l,b,h and also find volume and
area-----------
#include <iostream>
using namespace std;
class Box
{
int l,b,h;
public:
void volume()
{
cout<<"Enter length,breadth and height of box:";
cin>>l>>b>>h;
cout<<"Volume of box:"<<l*b*h<<endl;
}
void area()
{
cout<< "Area of box:"<<l*b<<endl;
}
};
int main() {
Box b1;
b1.volume();
b1.area();
return 0;
}
Output----------
Enter length, breadth and height of box:2 3 4
Volume of box:24
Area of box:6
Program 8:(a) define member function inside the class---------
#include <iostream>
using namespace std;
class Car
{
int speed;
int mileage;
public:
void maxspeed()
{
cout<<"Enter max speed of the car:";
cin>>speed;
cout<<"max allowed speed:"<<speed<<endl;
}
void maxmileage()
{
cout<<"Enter max mileage of the car:";
cin>>mileage;
cout<<"max mileage of the car:"<<mileage<<endl;
}
};
int main() {
Car c1;
c1.maxspeed();
c1.maxmileage();
return 0;
}
Output-----------
Enter max speed of the car:40
max allowed speed:40
Enter max mileage of the car:18
max mileage of the car:18
(b) outside the class using scope resolution operator----------
#include <iostream>
using namespace std;
class Car
{
int speed;
int mileage;
public:
void maxspeed();
void maxmileage();
};
void Car::maxspeed()
{
cout<<"Enter max speed of the car:";
cin>>speed;
cout<<"max allowed speed:"<<speed<<endl;
}
void Car:: maxmileage()
{
cout<<"Enter max mileage of the car:";
cin>>mileage;
cout<<"max mileage of the car:"<<mileage<<endl;
}
int main() {
Car c1;
c1.maxspeed();
c1.maxmileage();
return 0;
}
Output--------
Enter max speed of the car:40
max allowed speed:40
Enter max mileage of the car:18
max mileage of the car:18
Output------------
Value of x and y=0 0
Value of x and y=10 20
Value of x and y=10 20
Output---------
Time of Travel: 2.90476 hrs
Program 11: Create two classes DM and DB which stores the value of distances. WAP
that can read values for the class objects and add one object DM with another object
DB. Use friend function to carry out addition------------------
#include <iostream>
using namespace std;
class DB;
class DM
{
int m,cm;
public:
void get_data()
{
cout<<"\nEnter distance in meters-centimetres: ";
cin>>m>>cm;
}
friend float sum(DM a,DB b);
};
class DB
{
int ft,in;
public:
void get_data()
{
cout<<"\nEnter distance feet-inches : ";
cin>>ft>>in;
}
friend float sum(DM a,DB b);
};
float sum(DM a,DB b)
{
float x,y,z;
x=(a.m+(a.cm/100));
y=(b.ft+(b.in/12));
z=(x+(y*0.304)); //Since 1 ft = 0.304 m
return z;
}
int main()
{
DM a;
DB b;
a.get_data();
b.get_data();
cout<<"\nThe summed value in meter is: "<<sum(a,b);
return 0;
}
Output--------------
Enter distance in meters-centimetres: 2 3
Enter distance feet-inches: 3 1
The summed value in meter is: 2.912
Program 12: WAP to overload unary increment prefix and postfix operator--------------
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5) {}
void operator ++ () {
++value;
}
void operator ++ (int) {
value++;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
count1++;
count1.display();
++count1;
count1.display();
return 0;
}
Output-----------
Count: 6
Count: 7
Output---------------
Enter the values of x and y:
37
Enter the values of x and y:
21
The new x and y are:
x:5
y:8
Program 14---------------
#include <iostream>
using namespace std;
class Number{
protected:
int x,y;
public:
void get(){
cout<<"Enter two number:"<<endl;
cin>>x>>y;
}
};
class Product:public Number
{
public:
void printProduct()
{
cout<<"Product:"<<x*y<<endl;
}
};
class Sum:public Number
{
public:
void printSum()
{
cout<<"Sum:"<<x+y;
}
};
int main() {
Product p;
Sum s;
p.get();
p.printProduct();
s.get();
s.printSum();
return 0;
}
Output----------
Enter two number:
34
Product:12
Enter two number:
23
Sum:5
Output-----------
Enter the name:
riya
Enter the rollno:
32
Enter the semester:
4
Enter the stream:
it
Enter assignment1, housetest1 and housetest2 marks:10 15 15
Name:riya
Roll NO.:32
Semester:4
Stream:it
Marks Obtained
assignment1:10
housetest1:15
housetest2:15
Total marks score:40
Output-----------
Enter name, stream, rno and semester: Teena it 31 3
Enter the marks of housetest1, housetest2, assignment marks:10 15 15
Enter the sports score:400
Student Details
teena
it
31
3
Marks Details
10
15
15
Sports score Details400
Total marks:440
Output-------------
Print derived class
Show base class
Circle(float x)
{
r = x;
}
float calculateArea()
{
return 3.14*r*r ;
}
};
class Rectangle : public Shape
{
float l;
float b;
public:
Rectangle(float x, float y)
{
l=x;
b=y;
}
float calculateArea()
{
return l*b;
}
};
int main()
{
Shape *shape;
Square s(3.4);
Rectangle r(5,6);
Circle c(7.8);
shape =&s;
int a1 =shape->calculateArea();
shape = &r;
int a2 = shape->calculateArea();
shape = &c;
int a3 = shape->calculateArea();
cout << "Area of the square is " <<a1<< endl;
cout << "Area of the rectangle is " <<a2<< endl;
cout << "Area of the circle is " <<a3<< endl;
return 0;
}
Output---------------
Area of the square is 11
Area of the rectangle is 30
Area of the circle is 191
Output--------------
Enter two integer values: 2 3
After swap a = 3, b = 2
Enter two double values: 4.7 3.2
After swap p = 3.2, q = 4.7
before Swap
Before swap mr= Radha, mrs= Krishna
after Swap
After swap mr= Krishna, mrs= Radha
Output----------------
Int results:
Numbers: 2 and 1.
2+1=3
2-1=1
2*1=2
2/1=2
Float results:
Numbers: 2.4 and 1.2.
2.4 + 1.2 = 3.6
2.4 - 1.2 = 1.2
2.4 * 1.2 = 2.88
2.4 / 1.2 = 2
Output---------
Contents are:Teena
5.6
9