0% found this document useful (0 votes)
9 views7 pages

Oops

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

Oops

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

OBJECT ORIENTED PROGRAMMING LANGUAGE

VISHALAKSHI PL CSE-C 2127230501180

1. Make a class named Fruit with a data member to calculate the number of fruits
in a basket. Create two other class named Apples and Mangoes to calculate the
number of apples and mangoes in the basket. Print the number of fruits of each
type and the total number of fruits in the basket.
PROGRAM:
#include <iostream>
using namespace std;
class Fruit {
protected:
int total;
public:
Fruit() : total(0) {}
void add(int count) {
total += count; }
void display() {
cout << "Total fruits in the basket: " << total << endl; }};
class Apples : public Fruit {
private:
int apples;
public:
Apples() : apples(0) {}
void addApples(int count) {
apples += count;
add(count); }
void displayApples() {
cout << "Apples in the basket: " << apples << endl; }};
class Mangoes : public Fruit {
private:
int mangoes;
public:
Mangoes() : mangoes(0) {}
void addMangoes(int count) {
REG NO: 2127230501180
REGISTER NUMBER : 2127230501157
mangoes += count;
add(count); }
void displayMangoes() {
cout << "Mangoes in the basket: " << mangoes << endl; }};
int main() {
Apples apples;
Mangoes mangoes;
apples.addApples(5);
mangoes.addMangoes(3);
apples.displayApples();
mangoes.displayMangoes();
apples.display(); return 0;}
Output :

2. Write a C++ program to implement all types of access specifier using inheritance concepts
PROGRAM
#include <iostream>
using namespace std;
class Base {
public:
int publicVar;
Base() : publicVar(0) {}
void display() {
cout << "Base class - Public: " << publicVar << endl;
}
protected:

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157
void displayProtected() {
cout << "Base class - Protected: " << protectedVar << endl; }
private:
int privateVar;
void displayPrivate() {
cout << "Base class - Private: " << privateVar << endl; }
public:
void setPrivateVar(int value) {
privateVar = value; }
void setProtectedVar(int value) {
protectedVar = value; }};
class Derived : public Base {
public:
void accessBaseMembers() {
publicVar = 10;
display();
protectedVar = 20;
displayProtected();
}};
int main() {
Derived obj;
obj.publicVar = 5;
obj.display();
obj.accessBaseMembers(); return 0;}
Output :

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157
3. Program
#include <iostream>
#include <cmath>
using namespace std;
class Shape {
protected:
double dimension1;
double dimension2;
public:
void getData() {
cout << "Enter dimension 1: ";
cin >> dimension1;
cout << "Enter dimension 2: ";
cin >> dimension2;
}
virtual void displayArea() {
cout << "Area: " << endl;
}};
class Triangle : public Shape {
public:
void displayArea() override {
double area = 0.5 * dimension1 * dimension2;
cout << "Area of Triangle: " << area << endl;
}};
class Rectangle : public Shape {
public:
void displayArea() override {
double area = dimension1 * dimension2;
cout << "Area of Rectangle: " << area << endl;
}
};

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157
int main() {
Shape *shapePtr;
char choice;
cout << "Enter 'T' for Triangle or 'R' for Rectangle: ";
cin >> choice;
if (choice == 'T') {
Triangle triangle;
shapePtr = &triangle;
} else if (choice == 'R') {
Rectangle rectangle;
shapePtr = &rectangle;
} else {
cout << "Invalid choice!" << endl;
return 1;
}
shapePtr->getData();
shapePtr->displayArea();
return 0;
}
Output :

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157
4. Write a function template for finding the minimum value contained in an array.

#include <iostream>
#include <algorithm>
using namespace std;
template <typename T, size_t N>
T findMin(const T (&arr)[N]) {
return *min_element(arr, arr + N);
}
int main() {
int intArr[] = {5, 3, 9, 1, 7};
double doubleArr[] = {3.5, 1.2, 6.7, 2.4, 5.1};
cout << "Minimum value in intArr: " << findMin(intArr) << endl;
cout << "Minimum value in doubleArr: " << findMin(doubleArr) << endl;
return 0;
}

Output :

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157
5. Write a program that illustrates the application of multiple catch statements
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
int numerator = 10;
int denominator = 2;
int result = numerator / denominator;
cout << "Result: " << result << endl;
int arr[5] = {1, 2, 3, 4, 5};
cout << "Value at index 4: " << arr[4] << endl;
string str = "123abc";
int num = stoi(str);
cout << "Converted string to integer: " << num << endl;
}
catch (const std::exception& ex) {
cout << "Exception caught: " << ex.what() << endl;
}
catch (...) {
cout << "Unknown exception caught." << endl;
}
return 0;
}
Output:

REG NO: 2127230501180


REGISTER NUMBER : 2127230501157

You might also like