Oops
Oops
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:
#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 :