0% found this document useful (0 votes)
4 views23 pages

Oops File

prac file oops

Uploaded by

Dhruv Malhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Oops File

prac file oops

Uploaded by

Dhruv Malhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

BTCS 304-18 OOPS FILE 2301605

OBJECT ORIENTED PROGRAMMING


LAB PRACTICAL FILE
BTCS: 304-18

SUBMITTED TO: SUBMITTED BY:


Ms Kavita Sharma NAME: Dhruv Malhotra
Asst. Professor B.TECH CSE A (3rd Sem)
(CSE DEPT). Class Roll No: 120/23
Uni Roll No: 2301605

1
BTCS 304-18 OOPS FILE 2301605

INDEX
Sr. Practical Name Page No. Signature
No
1 Write a program that uses a class 3
where the member functions are
defined inside a class
2 Write a program that uses a class 4
where the member functions are
defined outside a class
3 Write a program to demonstrate the 5
use of static data members
4 Write a program to demonstrate the 6
use of constant data members
5 Write a program to demonstrate the 7-8
use of zero argument and
parameterized constructors
6 Write a program to demonstrate the 9
use of dynamic constructor
7 Write a program to demonstrate the 10
use of Friend Function
8 Write a program to demonstrate the 11
use of initializer list
9 Write a program to demonstrate the 12-13
overloading of increment and
decrement operators
10 Write a program to demonstrate the 14-15
overloading of memory management
operators
11 Write a program to demonstrate the 16
typecasting of basic type to class type
12 Write a program to demonstrate the 17
Function Overloading
13 Write a program to demonstrate the 18
Virtual Base Function
14 Write a program to demonstrate the 19
multiple inheritances
15 Write a program to demonstrate the 20
runtime polymorphism
16 Write a program to demonstrate the 21
exception handling
17 Write a program to demonstrate the 22
use of class template
18 Write a program to demonstrate the 23
reading and writing of mixed type of
data

2
BTCS 304-18 OOPS FILE 2301605

PRACTICAL1: Write a program that uses a class where the member


functions are defined inside a class

#include <iostream>

using namespace std;

class Circle {
public:
double radius;

Circle(double r) {
radius = r;
}

double area() {
return 3.14159 * radius * radius;
}

double circumference() {
return 2 * 3.14159 * radius;
}
};

int main() {
Circle myCircle(5.0);

cout << "Area: " << myCircle.area() << endl;


cout << "Circumference: " << myCircle.circumference() << endl;

return 0;
}

OUTPUT:

3
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 2: Write a program that uses a class where the member


functions are defined outside a class.

#include <iostream>
using namespace std;

class Rectangle {
public:Rectangle(double w, double h) : width(w), height(h) {}
double width;
double height;
};

double getArea(Rectangle& rect) {


return rect.width * rect.height;
}
double getPerimeter(Rectangle& rect) {
return 2 * (rect.width + rect.height);
}
int main() {
Rectangle rect1(5.0, 3.0); // Create a Rectangle object

double area = getArea(rect1);


double perimeter = getPerimeter(rect1);

cout << "Area: " << area << endl;


cout << "Perimeter: " << perimeter << endl;

return 0;
}

OUTPUT

4
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 3: Write a program to demonstrate the use of static data


members.

#include <iostream>
using namespace std;

class Counter {
public:
static int count;
Counter() {
count++; }
static int getCount() {
return count;
}
};
int Counter::count = 0;

int main() {
Counter c1; // Create first instance
Counter c2; // Create second instance
Counter c3; // Create third instance

cout << "Number of instances created: " << Counter::getCount() << endl;

return 0;
}

OUTPUT:

5
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 4: Write a program to demonstrate the use of const data


members.

#include <iostream>
using namespace std;

class Circle {
public:
const double radius;

Circle(double r) : radius(r) {}

double area() const {


return 3.14159 * radius * radius;
}

double circumference() const {


return 2 * 3.14159 * radius;
}
};

int main() {
Circle circle(5.0);
cout << "Radius: " << circle.radius << endl;
cout << "Area: " << circle.area() << endl;
cout << "Circumference: " << circle.circumference() << endl;
return 0;
}

OUTPUT:

6
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 5: Write a program to demonstrate the use of zero argument


and parameterized constructors.

#include <iostream>
#include <string>
using namespace std;

class Student {
public:
string name;
int age;

Student() {
name = "Unknown";
age = 0;
}

Student(string n, int a) {
name = n;
age = a;
}

void display() const {


cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};

int main() {
Student student1;
Student student2("Alice", 20);

cout << "Student 1 Details:" << endl;


student1.display();

cout << "\nStudent 2 Details:" << endl;


student2.display();

return 0;
}

7
BTCS 304-18 OOPS FILE 2301605

OUTPUT:

8
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 6: Write a program to demonstrate the use of dynamic


constructor.
#include <iostream>
#include <string>
using namespace std;

class DynamicArray {
public:
int* arr;
int size;

DynamicArray(int s) {
size = s;
arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = i + 1;
}
}

void display() const {


for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

DynamicArray() {
delete[] arr;
}
};

int main() {
DynamicArray myArray(5);
myArray.display();
return 0;
}

OUTPUT:

9
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 7: Write a program to demonstrate the use of friend


function.

#include <iostream>
using namespace std;

class Distance {
public:
int meters;

explicit Distance(int m) {
meters = m;
}

void display() const {


cout << "Distance: " << meters << " meters" << endl;
}
};

int main() {
Distance d1(10);
d1.display();

return 0;
}

OUTPUT:

10
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 8: Write a program to demonstrate the use of initializer list.


#include <iostream>
using namespace std;

class Rectangle {
public:
int width;
int height;

Rectangle(int w, int h) : width(w), height(h) {}

int area() const {


return width * height;
}
};

int main() {
Rectangle rect(10, 5);
cout << "Area of Rectangle: " << rect.area() << endl;
return 0;
}

OUTPUT:

11
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 9: Write a program to demonstrate the overloading of


increment and decrement operators.
#include <iostream>
using namespace std;

class Counter {
public:
int value;

Counter(int val = 0) {
value = val;
}

Counter operator++() {
value++;
return *this;
}

Counter operator++(int) {
Counter temp = *this;
value++;
return temp;
}

Counter operator--() {
value--;
return *this;
}

Counter operator--(int) {
Counter temp = *this;
value--;
return temp;
}

void display() const {


cout << "Value: " << value << endl;
}
};

int main() {

12
BTCS 304-18 OOPS FILE 2301605

Counter c1(5);
Counter c2 = c1++;

cout << "c1: ";


c1.display();
cout << "c2: ";
c2.display();

Counter c3 = ++c1;

cout << "c1: ";


c1.display();
cout << "c3: ";
c3.display();

Counter c4 = c1--;

cout << "c1: ";


c1.display();
cout << "c4: ";
c4.display();

Counter c5 = --c1;

cout << "c1: ";


c1.display();
cout << "c5: ";
c5.display();

return 0;
}

OUTPUT:

13
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 10: Write a program to demonstrate the overloading of


memory management operators.

#include <iostream>
using namespace std;

class MemoryManager {
public:
int size;
int* data;

MemoryManager(int s) : size(s) {
data = new int[size];
}

~MemoryManager() {
delete[] data;
}

void fillData() {
for (int i = 0; i < size; ++i) {
data[i] = i + 1;
}
}

void display() const {


for (int i = 0; i < size; ++i) {
cout << data[i] << " ";
}
cout << endl;
}

void* operator new(size_t size) {


cout << "Custom new operator called" << endl;
return ::operator new(size);
}

void operator delete(void* pointer) {


cout << "Custom delete operator called" << endl;
::operator delete(pointer);
}
};
14
BTCS 304-18 OOPS FILE 2301605

int main() {
MemoryManager* obj = new MemoryManager(5);
obj->fillData();
obj->display();
delete obj;
return 0;
}

OUTPUT:

15
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 11: Write a program to demonstrate the typecasting of


basic type to class type.

#include <iostream>
using namespace std;

class Number {
public:
int value;

Number(int val) : value(val) {}

operator int() {
return value;
}
};

int main() {
Number num(42);
int basicType = num;

cout << "Value as Number: " << num.value << endl;


cout << "Value as int: " << basicType << endl;

return 0;
}

OUTPUT:

16
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 12: Write a program to demonstrate function overloading.


#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

double add(double a, double b) {


return a + b;
}

int main() {

int sum1 = add(10, 20);


int sum2 = add(10, 20, 30);
double sum3 = add(10.5, 20.5);

cout << "Sum of two integers: " << sum1 << endl;
cout << "Sum of three integers: " << sum2 << endl;
cout << "Sum of two doubles: " << sum3 << endl;

return 0;
}
OUTPUT:

17
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 13: Write a program to demonstrate virtual base function.


#include <iostream>
using namespace std;
class Shape {
public:
Shape() {
cout << "Shape constructor" << endl;
} virtual void display() {
cout << "Displaying shape" << endl;
}
};class TwoDimensional : virtual public Shape {
public:
TwoDimensional() {
cout << "TwoDimensional constructor" << endl;
} virtual void display() override {
cout << "Displaying 2D shape" << endl;
}};class ThreeDimensional : virtual public Shape {
public:
ThreeDimensional() {
cout << "ThreeDimensional constructor" << endl;
}virtual void display() override {
cout << "Displaying 3D shape" << endl;
}};class Cube : public TwoDimensional, public ThreeDimensional {
public:
Cube() {cout << "Cube constructor" << endl;}
void display() override {
cout << "Displaying Cube" << endl;
};
int main() {Cube cube;
cube.display();
return 0;}

OUTPUT:

18
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 14: Write a program to demonstrate the multiple


inheritances.

#include <iostream>
using namespace std;

class Base1 {
public:
void displayBase1() {
cout << "Base1 class method" << endl;
}
};

class Base2 {
public:
void displayBase2() {
cout << "Base2 class method" << endl;
}
};

class Derived : public Base1, public Base2 {


public:
void displayDerived() {
cout << "Derived class method" << endl;
}
};

int main() {
Derived obj;
obj.displayBase1();
obj.displayBase2();
obj.displayDerived();

return 0;
}

OUTPUT:

19
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 15: Write a program to demonstrate the runtime


polymorphism.

#include <iostream>
using namespace std;

class Base {
public:
virtual void display() {
cout << "Display from Base class" << endl;
}
};

class Derived : public Base {


public:
void display() override {
cout << "Display from Derived class" << endl;
}
};

int main() {
Base* basePtr;
Derived derivedObj;
basePtr = &derivedObj;

basePtr->display();

return 0;
}

OUTPUT:

20
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 16: Write a program to demonstrate the exception


handling.

#include <iostream>
using namespace std;

class DivisionByZeroException {};

double divide(int numerator, int denominator) {


if (denominator == 0) {
throw DivisionByZeroException();
}
return static_cast<double>(numerator) / denominator;
}

int main() {
int a = 10, b = 0;
try {
double result = divide(a, b);
cout << "Result: " << result << endl;
} catch (DivisionByZeroException&) {
cout << "Error: Division by zero!" << endl;
}

return 0;
}

OUTPUT:

21
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 17: Write a program to demonstrate the use of class


template.

#include <iostream>
using namespace std;

template <typename T>


class Calculator {
public:
T add(T a, T b) {
return a + b;
}
T subtract(T a, T b) {
return a - b;
}
T multiply(T a, T b) {
return a * b;
}
T divide(T a, T b) {
if (b == 0) {
throw runtime_error("Division by zero");
}
return a / b;
}
};
int main() {
Calculator<int> intCalc;
cout << "Int Add: " << intCalc.add(5, 3) << endl;
Calculator<double> doubleCalc;
cout << "Double Divide: " << doubleCalc.divide(5.0, 2.0) << endl;
return 0;
}

OUTPUT:

22
BTCS 304-18 OOPS FILE 2301605

PRACTICAL 18: Write a program to demonstrate the reading and writing


of mixed type of data.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ofstream outFile("data.txt");
int age = 25;
double salary = 45000.50;
string name = "John Doe";
outFile << name << endl;
outFile << age << endl;
outFile << salary << endl;
outFile.close();
ifstream inFile("data.txt");
string readName;
int readAge;
double readSalary;
getline(inFile, readName);
inFile >> readAge;
inFile >> readSalary;
inFile.close();
cout << "Name: " << readName << endl;
cout << "Age: " << readAge << endl;
cout << "Salary: " << readSalary << endl;

return 0;
}

OUTPUT:

23

You might also like