CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2018
    Posts
    165

    Exercise on pure virtual function

    I'm not able to understand solution about the following exercise where, given this code, It asks to write C++ code which read from standard input positive value N and, for N times, It has to read temperature values from 2 sensors and It has to writes their arithmetic average to standard output.



    Code:
    class Sensor {
    public:
       virtual int get_value(void) =0;
    };
    
    class Filter {
    public:
    virtual int filter(int value) =0;
    };
    
    class TempSensor : public Sensor {
       Filter* tf;
       int value;
       public:
          TempSensor(Filter* f): tf(f);
          int read(void);
          int get_value(void);
    };
    
    class KFilter : public Filter {
    // data structures for filtering
    public:
       int filter(int value);
    };
    
    int TempSensor::read(void)
    {
       // read value from hardware
    }
    
    int TempSensor::get_temp(void)
    {
       value = read();
       return tf->filter(value);
    }
    
    int KFilter::filter(int value)
    {
       // implementation of filter algorithm
    }

    Solution:

    Code:
    int
    main()
    {
       int N;
       cin >> N;
       KFilter kf1;
       KFilter kf2;
       TempSensor ts1(&kf1);
       TempSensor ts2(&kf2);
       for (int i = 0; i < N; i++) {
          ts1.read();
          int v1 = ts1.get_temp();
          ts2.read();
          int v2 = ts2.get_temp();
          cout << (v1 + v2)/2 < endl;
       }
    }
    1. I think there is error because 'get_temp()' function member is not declared inside TempSensor class, I see only your definition. Pheraps get_temp() and get_value() are the same thing ?
    2. In solution, the first step is to read sensor value by ts1.read() but next statement 'ts1.get_temp()' (I suppose that 'get_temp' was the same function 'get_value') read again sensor value and filters it. What necessary to read sensor value twice ?


    I think this solution code is wrong, what do you think ?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Exercise on pure virtual function

    1) probably.
    2) no.

    This is not good code. I suspect it's very old code from wherever it was obtained as it uses void for function parameter which was c style and not used in C++. My advice is to scrap it and write your own. For the given problem it is vastly over-engineered (unless the exercise is to use derived classes).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Exercise on pure virtual function

    Based upon the given design, consider as a starting point:

    Code:
    #include <iostream>
    #include <random>
    
    std::mt19937 gen(std::random_device {}());
    std::uniform_int_distribution<int> rnd(12, 25);
    
    class Sensor {
    public:
    	virtual int get_value() = 0;
    };
    
    class Filter {
    public:
    	virtual int filter(int value) const = 0;
    };
    
    class TempSensor : public Sensor {
    	const Filter* tf {};
    	int value {};
    public:
    	TempSensor(const Filter* f) : tf(f) {};
    	void read();
    	int get_value() override;
    };
    
    class KFilter : public Filter {
    	// data structures for filtering
    public:
    	int filter(int value) const override;
    };
    
    void TempSensor::read() {
    	// read value from hardware
    	value = rnd(gen);	// For testing
    }
    
    int TempSensor::get_value() {
    	read();
    	return tf->filter(value);
    }
    
    int KFilter::filter(int value) const {
    	// implementation of filter algorithm
    	return value - 10;
    }
    
    int main() {
    	int N {};
    
    	std::cout << "Enter number of values: ";
    	std::cin >> N;
    
    	KFilter kf1;
    	KFilter kf2;
    	TempSensor ts1(&kf1);
    	TempSensor ts2(&kf2);
    
    	for (int i {}; i < N; ++i)
    		std::cout << (ts1.get_value() + ts2.get_value()) / 2 << '\n';
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    May 2018
    Posts
    165

    Re: Exercise on pure virtual function

    Quote Originally Posted by 2kaud View Post
    1) probably.
    2) no.

    This is not good code. I suspect it's very old code from wherever it was obtained as it uses void for function parameter which was c style and not used in C++. My advice is to scrap it and write your own. For the given problem it is vastly over-engineered (unless the exercise is to use derived classes).
    The exercise requires to use derived classes
    I understand that this one is not good code but It's a university exam and now I have to understand just this code, not implement goot code.

    you're okay with me that It doesn't necessary to use both ts1.read() and ts1.get_temp() (that is ts1.get_value) ? ts1.get_value read again sensor value. So I have 2 reads for the same sensor, I think It's wrong.

    Code:
    ts1.read();
          int v1 = ts1.get_temp();
          ts2.read();
          int v2 = ts2.get_temp();
    thanks again
    Last edited by zio_mangrovia; March 19th, 2025 at 09:47 AM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Exercise on pure virtual function

    To help to learn C++ consider:
    https://www.learncpp.com/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2018
    Posts
    165

    Re: Exercise on pure virtual function

    Quote Originally Posted by 2kaud View Post
    To help to learn C++ consider:
    https://www.learncpp.com/
    what mistake did I make in my question please ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured