polymorphism

Hi, this this a part of my code, so i have a problem with "virtual double Add()=0;" function, if i delete it my code works but i need it another class, so how i make it work?
Thanks in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 #include <iostream>
#include <string>

using namespace std;

//etalons//////////
class ETalons {
public:
   ETalons(  double);
   virtual string Use()=0 ;
   virtual double Add()=0;
   virtual void print();      
protected:
   double serial;
};

ETalons::ETalons(double serial){
   this->serial = serial;
}

void ETalons::print() { 
   cout << " E-ID: " << serial ; 
}

//___________________________________________________________________________________
   class BraucienuskET : public ETalons{
public:
	BraucienuskET(double, int);
	virtual void print() ;
	virtual string Use();

		protected:
		int trip;
};
BraucienuskET::BraucienuskET(double serial, int trip) : ETalons(serial){
		this->trip=trip;
	}
    
string BraucienuskET::Use()  {

if (trip > 0){
	return "green";
	}
	else {return "red";
	}		
}

void BraucienuskET::print(){ 
   cout << "Trips: \n ";
   ETalons::print();
   cout<< " trips left: "<< trip; }
   

//main__________________________________________________________________________

int main(){

	BraucienuskET y(343, 15);
	BraucienuskET y1(35553, 0);
	
	ETalons *array1[2];
   array1[0]=&y;
   array1[1]=&y1;

   
 for(int i=0; i<2; i++)
   {
      array1[i]->print();
     cout << "used?: " << array1[i]->Use()<< endl; 
   }
    
   system("pause");
 
   return 0;	
}

[Error] cannot declare variable 'y' to be of abstract type 'BraucienuskET'
[Error] cannot declare variable 'y1' to be of abstract type 'BraucienuskET'
[Note] virtual double ETalons::Add()
> virtual double Add()=0;
Every derived class has to implement this function.

Like you did with Use()
So define Add() within your BraucienuskET class.

Seems an odd function to have with no arguments though ...
Hello there,
Instead of writing virtual double Add() = 0;
you can use virtual double Add() { return 0; };
depends on what are the use for this function

Have a nice day.
When you want to instantiate an object of a derived class from an abstract class, you need to ensure that all methods of this abstract class are overridden by non-virtual methods at your derived class. If you forgot this for a method, your derived class remains abstract too.

At your example, BraucienuskET has still the method Add() as pure virtual, so you cannot instantiate objects from. You need to override Add() in your BraucienuskET class.
Topic archived. No new replies allowed.