abstract class


Hello guys i want to ask, what is the problem
i got error message cannot declare variable cocktailMachineObj to be of abstract class type CocktailMachine , because the following virtual function are pure within 'CocktailMachine'

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
 void Menue::menue()
{
	CocktailMachine cocktailMachineObj  ; // error here
	CocktailSimulator *cocktailSimulatorPointer;
	cocktailMachineObj = new CocktailMachine; 
	cocktailSimulatorPointer = &cocktailMachineObj; 

	char choice = ' ';

	do {
		system("cls");
		cout << "== CocktailMix | V0.2 ==" << endl;
		cout << "1 - Make Cocktails" << endl;
		cout << "2 - Configure CocktailMix" << endl;
		cout << "0 - Exit" << endl << endl;
		cout << "Auswahl=> ";
		cin >> choice;

		if (choice == '1')
		{
			submenue_makeCocktail();
		}
		else if (choice == '2')
		{
			submenue_configure();
		}
	} while (choice != '0');
}


Here is my abstract class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
   #ifndef COCKTAILSIMULATOR_H
#define COCKTAILSIMULATOR_H
//#include "CocktailMachine.h"
#include <vector>
#include "Cocktail.h"
#include "Ingredient.h"
#include "Dispenser.h"


class CocktailSimulator
{
   public:

        virtual void makeCocktail(Cocktail* ) = 0;
        virtual void setCocktails(Cocktail*) = 0;

        //getter
        virtual vector<Ingredient*> getIngredients() = 0;
        virtual vector<Dispenser*> getDispensers() = 0;
        virtual vector<Cocktail*> getCocktails() = 0;
    private:
};

#endif // COCKTAILSIMULATOR_H 


and here is my CocktaiLMachine class

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
#ifndef COCKTAILMACHINE_H
#define COCKTAILMACHINE_H
#include "Cocktail.h"
#include "Ingredient.h"
#include "Dispenser.h"
#include "CocktailSimulator.h"
#include <fstream>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;

class CocktailMachine : public CocktailSimulator
{
	public :
	   CocktailMachine();
		virtual ~CocktailMachine();

		//Funktion
		void makeCocktail(int);

		//setter
		void setCocktails(Cocktail*);

		//getter
		vector<Ingredient*> getIngredients();
		vector<Dispenser*> getDispensers();
		vector<Cocktail*> getCocktails();

private:

		Ingredient* ingredientObj;
		Dispenser* dispenserObj;
		Cocktail* cocktailObj;
		vector<Ingredient*> ingredients;
		vector<Dispenser*> dispensers;
		vector<Cocktail*> cocktails;
};

#endif // COCKTAILMACHINE_H



i need to make a object cocktailMachineObj, because i need to have acces like setter getter
Last edited on
A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class if the derived class is not abstract. Classes containing pure virtual methods are termed "abstract" and they cannot be instantiated directly. A subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class

- https://en.wikipedia.org/wiki/Virtual_function
Did you actually implememnt all the virtual methods in your derived class?
hi gunnerfunner
thx for for your answer.
i actually need make abstract for void makeCocktail(Cocktail*) in CocktailMachine.h
but even i delete the rest virtual except makeCocktail. i still get error
OK you've made some changes to your program, pls post the latest versions

edit: also noticed your derived class makeCocktail() has a different signature to the base class method of the same name and so be aware that the derived class method is hiding the base class method : http://stackoverflow.com/questions/11086238/c-pure-virtual-function-inheritance-same-signature
Last edited on
ahaa thx gunner
now its work
:D
Topic archived. No new replies allowed.