Fill the Two Largest Numbers to class - C++ Class

C++ examples for Class:Member Function

Description

Fill the Two Largest Numbers to class

Demo Code

                           
#include <iostream>
                           //  w  w  w  .j ava2  s  . co  m
class MyValue {
 private:
    int counter = 0.0f;
    double number = 0.0f;
    double fLargest = 0.0f;
    double sLargest = 0.0f;
                           
 public:
    MyValue();
    ~MyValue();
                           
    // SETTERS
    void set1STLargest(double);
    void set2NDLargest(double);
                           
    // GETTERS
    double get1STLargest();
    double get2NDLargest();
                           
    void run();
};
                           
MyValue::MyValue() {}
MyValue::~MyValue() {}
                           
// SETTERS
void MyValue::set1STLargest(double num) { fLargest = num; }
void MyValue::set2NDLargest(double num) { sLargest = num; }
                           
// GETTERS
double MyValue::get1STLargest() { return fLargest; }
double MyValue::get2NDLargest() { return sLargest; }
                           
void MyValue::run() {
    std::cout << "Find the two largest numbers from 10" << std::endl;
                           
    while (counter < 10) {
        std::cout << "Enter number (" << 10 - counter << " remaining): ";
        std::cin >> number;
                           
        if (number > get1STLargest()) {
            set2NDLargest(get1STLargest());
            set1STLargest(number);
        }
                           
        counter++;
    }
                           
    std::cout << "First Largest: " << get1STLargest() << std::endl;
    std::cout << "Second Largest: " << get2NDLargest() << std::endl;
}
                           
int main(int argc, const char *argv[]) {
    MyValue ftl;
                           
    ftl.run();
    return 0;
}

Result


Related Tutorials