Oodp Practical File
Oodp Practical File
Oodp Practical File
PRACTICAL FILE
NAME:
REGISTRATION NO.
DEGREE:
BONAFIDE CERTIFICATE
& Reg.No. ________________ of FIRST year & SECOND semester for Bachelor
Engineering, SRM IST has been done for the course Object Oriented Design
EXANINER-1 EXAMINER-2
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
DELHI NCR CAMPUS, MODINAGAR
LIST OF EXPERIMENTS
OUTPUT:
Enter an integer: 70
The number is: 70
#include<iostream>
using namespace std;
int main()
{
int myNum = 5;
float myFloatNum = 5.99;
double myDoubleNum = 9.98908;
char myLetter = 'D';
string myText = "Hello";
cout<<mynum<<endl<< myFloatNum<<endl<< myDoubleNum<<endl<< myLetter<<endl<<
myText; return 0;
}
OUTPUT:
5
5.99
9.98908
D
Hello
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 7;
b = 2;
cout << "a + b = " << (a + b) <<
endl; cout << "a - b = " << (a - b) <<
endl; cout << "a * b = " << (a * b)
<< endl; cout << "a / b = " << (a / b)
<< endl; cout << "a % b = " << (a %
b) << endl; return 0;
}
OUTPUT:
a+b=9
a-b= 5
a * b = 14
a/b=3
a%b=1
Program-4: Write a program for if_else statement in C++.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
return 0;
}
OUTPUT:
Enter an integer: 4
You entered a positive integer: 4
Enter an integer: -4
You entered a negative integer: -4
{} double area() {
return length * width;
}
};
int main() {
Rectangle rect(5.0, 3.0);
cout << “Area of the rectangle: ” << rect.area() <<
endl; return 0;
}
OUTPUT:
#include <iostream>
using namespace std;
class Z
{
public:
Z()
{
cout<<"Constructor called"<<endl;
}
~Z()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Z z1;
int a = 1;
if(a==1)
{
Z z2;
}
}
OUTPUT:
Constructor called
Constructor called
Destructor called
Destructor called
#include <iostream>
using namespace std;
class Count {
private:
int value;
public:
Count() : value(5)
{} void operator ++
() {
++value;
}
void display() {
cout << "Count: " << value << endl;
}
};
int main() {
Count count1;
++count1;
count1.display();
return 0;
OUTPUT:
Count: 6
sum = 12
sum = 11.5
};
OUTPUT:
Id: 54
Name: vaibhav
Course: B.tech
Fee: 300000
Program 11: Write a program for friend function in C++
#include <iostream>
class Distance
{ private:
int meter;
public:
Distance() : meter(0) {}
};
int addFive(Distance d)
{ d.meter += 5;
return d.meter;
int main() {
Distance
D;
addFive(D); return 0;
Output:
Distance: 5
Program12: Write a program for inline function in C++.
#include <iostream>
class operation {
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
cin >> b;
add = a + b;
sub = a - b;
cout << "Difference of two numbers: " << a - b << "\n";
mul = a * b;
}
inline void operation::division()
div = a / b;
int main()
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division()
; return 0;
Output:
Program13: Write a program for virtual and pure virtual function in C++.
1. Virtual Function
#include <iostream>
class base
public:
void show()
};
public:
void show()
};
public:
void show()
{
std::cout << "Derived class 2" << std::endl;
};
int main()
base *b;
derived1 d1;
derived2 d2;
b=&d1;
b->show();
b=&d2;
b->show();
return 0;
Output:
Base class
Base class
2. Pure virtual
function #include
<iostream> using
namespace std;
// Abstract class
class Shape
public:
float a;
public:
Square(float l)
a = l;
float calculateArea()
return a*a;
};
float r;
public:
Circle(float x)
r = x;
float calculateArea()
{
return 3.14*r*r ;
};
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();
std::endl; return 0;
Output:
#include <iostream>
return (x > y) ? x : y;
int main()
{
endl;
return 0;
Output:
g
Program-2: Write a program for exception handling in C++.
#include <iostream>
int main() {
try {
if (denominator == 0)
throw 0;
cout << numerator << " / " << denominator << " = " << divide << endl;
}
cout << "Error: Cannot divide by " << num_exception << endl;
return 0;
Output:
Enter numerator: 2
Enter denominator: 4
2 / 4 = 0.5