У меня есть эта программа на C ++.
ref2.h:
#ifndef REF2_H
#define REF2_H
#include <iostream>
using std::cout;
using std::endl;
int add_int_int(int a, int b) {return (a+b);}
class IntClass;
class Number {
public:
//return a Number object that's the results of x+this, when x is IntClass
virtual Number& addInt(IntClass& x) = 0;
//Print the number stored in the object
virtual void print_number() = 0;
}
class IntClass : public Number {
private:
int my_number;
public:
//Constructor
IntClass(int n):my_number(n) {}
//returns the number stored in the object
int get_number() {return my_number;}
//print the number stored in the object
void print_number() {cout << my_number << endl;}
Number& addInt(IntClass& x);
}
Number& IntClass::addInt(IntClass& x)
{
int n = add_int_int(my_number, x.get_number());
IntClass elem = IntClass(n);
IntClass &ref = elem;
return ref;
}
#endif
test.cpp
#include "ref2.h"
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "Testing subtyping polymorphism:" << endl;
IntClass ia(1);
IntClass ib(2);
Number& num = ia.addInt(ib); num.print_number(); //should be: 3
}
Я не нахожу свою ошибку.
Если кто-нибудь может мне помочь?