0% found this document useful (0 votes)
19 views2 pages

Practical 10

Uploaded by

Mansi Siddhanti
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)
19 views2 pages

Practical 10

Uploaded by

Mansi Siddhanti
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/ 2

PRACTICAL:-10

 Write C++ Program with base class convert declares two variables, val1 and val2, which hold
the initial
 and converted values, respectively. It also defines the functions getinit( ) and getconv( ),
which return the
 initial value and the converted value. These elements of convert are fixed and applicable to
all derived
 classes that will inherit convert. However, the function that will actually perform the
conversion, compute(
 ), is a pure virtual function that must be defined by the classes derived from convert. The
specific nature
 of compute( ) will be determined by what type of conversion is taking place.

Programe:-

#include <iostream>
using namespace std;

// Base class 'Convert' which defines the common structure for conversion
class Convert {
protected:
double val1; // Initial value
double val2; // Converted value

public:
// Constructor to initialize val1 and val2
Convert(double initValue = 0) : val1(initValue), val2(0) {}

// Function to get the initial value


double getinit() const {
return val1;
}

// Function to get the converted value


double getconv() const {
return val2;
}

// Pure virtual function for performing conversion


virtual void compute() = 0; // Pure virtual function must be implemented by derived classes
};

// Derived class to convert Kilometers to Miles


class KmToMiles : public Convert {
public:
// Constructor to initialize initial value for conversion
KmToMiles(double km) : Convert(km) {}

// Function to perform the conversion (1 km = 0.621371 miles)


void compute() override {
val2 = val1 * 0.621371;
}
};

// Derived class to convert Celsius to Fahrenheit


class CelsiusToFahrenheit : public Convert {
public:
// Constructor to initialize initial value for conversion
CelsiusToFahrenheit(double celsius) : Convert(celsius) {}

// Function to perform the conversion (°F = (°C * 9/5) + 32)


void compute() override {
val2 = (val1 * 9.0 / 5.0) + 32;
}
};

int main() {
double value;

// Conversion from Kilometers to Miles


cout << "Enter distance in kilometers: ";
cin >> value;
KmToMiles kmToMiles(value);
kmToMiles.compute(); // Perform the conversion
cout << value << " kilometers is equal to " << kmToMiles.getconv() << " miles.\n";

// Conversion from Celsius to Fahrenheit


cout << "\nEnter temperature in Celsius: ";
cin >> value;
CelsiusToFahrenheit cToF(value);
cToF.compute(); // Perform the conversion
cout << value << " Celsius is equal to " << cToF.getconv() << " Fahrenheit.\n";

return 0;
}

You might also like