Where is static polymorphism implemented in the code?
The teacher asked the question, in which line of this code (C++) is static polymorphism implemented? The teacher also said that there is a static polymorphism in an implicit form.
#include"stdafx.h"
#include<iostream>
#include<clocale>
#include<conio.h>
#include<ctime>
using namespace std;
class pulsometer
{
public:
int pulse;
int calories;
int timer;
void show_pulse(void);
void show_calories(void);
void show_timer()
{
cout << "Таймер: " << timer << endl;
}
void start_timer()
{
timer = 10;
}
};
void pulsometer::show_pulse(void)
{
pulsometer::pulse = rand() % 45 + 90;
cout << "Пульс: " << pulse << endl;
}
void pulsometer::show_calories(void)
{
pulsometer::calories = rand() % 1000;
cout << "Сожжено: " << calories << " ККал" << endl;
}
int_tmain(intargc, _TCHAR* argv[])
{
setlocale(0,"");
srand( time( 0 ) );
pulsometer polar;
polar.start_timer();
polar.show_timer();
polar.show_pulse();
polar.show_calories();
_getch();
return 0;
}
1
Author: Andrey Dudukin, 2016-11-15
2 answers
There is no polymorphism here: neither static nor dynamic. There is no inheritance, no overloaded functions, no template functions.
Polymorphism means many forms.
There is also one class - one form. Here there is only a static binding of class method calls to an object that has a static type of this class.
6
Author: Vlad from Moscow, 2016-11-15 11:41:20
In all such lines (the <<
operator is overloaded):
cout << "Таймер: " << timer << endl;
1
Author: Igor, 2016-11-15 17:25:39