Oops Lab
Oops Lab
Output:
Hello World !
int main() {
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Output:
Enter an integer: 23
23 is odd.
int main() {
int n;
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
int factorial(int n) {
if(n > 1)
else
return 1;
Output:
Factorial of 6 = 720
int main() {
sum = 0;
sum += i;
return 0;
Output:
Enter a positive integer: 10
Sum = 55
class Person{
private:
string name;
int age;
public:
// declaring constructor
Person()
name = "student";
age = 12;
void display()
}
};
int main()
Person obj;
obj.display();
return 0;
Output:
#include <iostream>
public:
};
int main() {
MyClass::displayMessage();
return 0;
Output:
Hello, World!
#include<iostream.h>
#include<conio.h>
class number
{
int a;
public:
get_num(int x);
};
number::get_num(int x)
a=x;
number::print_num(number num)
cout<<"Value of a :"<<num.a;
main()
clrscr();
number obj;
obj.get_num(1000);
print_num(obj);
getch();
}
Output:
Value of a : 1000
#include <iostream>
class Rectangle {
public:
int length, width;
Rectangle() {
length = 1;
width = 1;
Rectangle(int side) {
length = side;
width = side;
Rectangle(int l, int w) {
length = l;
width = w;
void displayArea() {
}
};
int main() {
rect1.displayArea();
rect2.displayArea();
rect3.displayArea();
return 0;
Output:
Area: 1
Area: 25
Area: 15
9.Write a program to demonstrate the concept of single
inheritance.
#include <iostream>
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
void setHeight(int h) {
height = h;
protected:
int width;
int height;
};
// Derived class
public:
int getArea() {
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
Output:
Total area: 35
#include <iostream>
// Base class 1
class Car {
public:
void drive() {
};
// Base class 2
class Boat {
public:
void sail() {
};
// Derived class
public:
void use() {
};
int main() {
DualModeVehicle myVehicle;
return 0;
Output:
Driving on land
Sailing on water
11.Write a program to demonstrate the concept of exception
handling.
#include <iostream>
int main() {
if (denominator == 0)
throw 0;
cout << numerator << " / " << denominator << " = " << divide <<
endl;
cout << "Error: Cannot divide by " << num_exception << endl;
return 0;
Output 1
Enter numerator: 72
Enter denominator: 0
Output 2
Enter numerator: 72
Enter denominator: 3
72 / 3 = 24
#include <iostream>
#include <string>
int main () {
int i = 39;
int j = 20;
double f1 = 13.5;
double f2 = 20.7;
cout << "Max(f1, f2): " << Max(f1, f2) << endl;
string s1 = "Hello";
string s2 = "World";
cout << "Max(s1, s2): " << Max(s1, s2) << endl;
return 0;
Output:
Max(i, j): 39