How to change the code, what would the example work?

Hi all. I have two classes
class Audioformat {
public:

 string titel() { return ""; }
 string info() { return ""; }
};

mp3_counter int = 0;

MP3_Format class : public Audioformat {
public:

 string titel() { return song; }
 string info() { return "MP3"; }

private:
 string song;
};


and test:
bool test_A1_a() {
#ifdef A1_a
 Audioformat * af = new MP3_Format("Hey Joe");
 string info = af->info();
 string absp = af->titel();
 return (info == "MP3" && absp == "Hey Joe");
#else
 return false;
#endif
}


What should change in the class MP_3 , would be that the code worked.
Now
string info = af->info();
string absp = af->titel();
return of Audioformat class result
July 4th 19 at 23:35
1 answer
July 4th 19 at 23:37
Solution
1. Title-is title and not titel
2. For every new a delete must follow. It is better to wrap Audioformat* to std::unique_ptr, which makes it delete itself:
#include <memory>
std::unique_ptr<audioformat> af(new Audioformat(""));
af->info(); af->title();
// Do not need delete :D</audioformat></memory>

3. Methods that can be invoked through a pointer to base class must be declared as virtual. If the contents of the base class methods does not make sense (as here), it is better to declare as pure virtual:
class Audioformat {
public:
 virtual string info() = 0;
};
MP3_Format class : public Audioformat {
public:
 string info() override { return "MP3"; }
};

override is not required, but highly desirable.
4. Virtual destructor:
class Audioformat {
public:
 virtual ~Audioformat() {}
};
what I do not understand what you mean in paragraph three. To begin with. I understand, I need a designer for two classes. isn't it ? - gay0 commented on July 4th 19 at 23:40
: Not necessarily. Read the tutorial section about classes, from beginning to end. - kenyatta commented on July 4th 19 at 23:43
: o how wrong I was. now I understand about the virtual world. f-I. test passes as it should. - gay0 commented on July 4th 19 at 23:46

Find more questions by tags C++