I hope I got the relevant code in here. I have some problem when I want to fetch the menu option that I've added into to menu_1. I have this function on_select(int) that I use to fetch one sub-menu's options, which I do by using the display() function. But when I compile it will say that there are no function named display() in menu_option() class, which is the Base class, but what I want to is to access the display() function which is located in the sub_menu() class.
I have tried multiple thing to get the relevant object from the array without any success, so I'm here now asking for help with this one.
I have this following main()
#include <iostream>
using namespace std;
#include "menu.h"
int main()
{
sub_menu* main_menu = new sub_menu("Warehouse Store Menu");
sub_menu* menu_1 = new sub_menu("Menu1");
main_menu->add_option(new sub_menu("Menu2"));
main_menu->add_option(menu_1);
product_menu->add_option(new add_item("sub_item1"));
product_menu->add_option(new add_item("sub_item2"));
product_menu->add_option(new add_item("sub_item3"));
main_menu->display();
main_menu->on_select(1);
delete main_menu;
return 0;
}
header file
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const int MAX_SIZE = 9;
class menu_option
{
public:
menu_option(string const& n) : title(n) {};
virtual ~menu_option();
virtual void on_select(int) = 0;
string get_title() { return title; }
protected:
string title;
};
/* ------------------------------------- */
class sub_menu : public menu_option
{
public:
sub_menu(string const& n)
: menu_option(n) { no_item = 0; }
~sub_menu() { delete[] list; };
void on_select(int);
void add_option(menu_option*);
void display();
private:
menu_option* list[MAX_SIZE]; //container for options in the sub_menu
int no_item;
};
implement file
void sub_menu::on_select(int i)
{
cout << (list[i])->get_title() << endl;
cout << (list[i])->display() << endl; //<------ Doesn't work
}
void sub_menu::add_option(menu_option* item)
{
list[no_item] = item;
no_item++;
}
void sub_menu::display()
{
cout << ">> " << get_title() << " <<"<< endl;
for( int i = 0; i < no_item; i++ )
{
cout << setw(2) << i << ": " << (list[i])->get_title() << endl;
}
}
You can do what you want to do, but it's bad. You have to cast down to sub_menu when you call display() in on_select(). Of course it's not going to work the way you have it, and the compiler is telling you exactly why.
The other option, which is probably better (though without a clear understanding of the problem space may not be the best) would be to add display() as a virtual function to the menu_option class.
To solve your immediate problem you'll want to use dynamic_cast to turn a menu_option* into a sub_menu*, like so:
sub_menu* submenu(dynamic_cast<sub_menu*>(list[i]));
Note that if the cast fails (i.e., the menu_option pointed to by list[i] is not a sub_menu after all) the value of the submenu pointer will be NULL, so make sure you check that it is a valid pointer before using it in subsequent code.
Related
I am trying to develop a text adventure in C++ where users can input string commands (ex. "take apple").
Here is a very naive sample of code I came up with:
# include <iostream>
using namespace std;
class fruit{
public:
string actual_name;
fruit(string name){
actual_name = name;
}
take() {
cout << "You take a " << actual_name << "." << endl;
}
};
fruit returnObjectFromName(string name, fruit Fruits[]){
for(int i = 0; i <= 1; i++){ // to be modified in future depending on Fruits[] in main()
if (Fruits[i].actual_name == name)
return Fruits[i];
}
}
int main(){
string verb;
cout << "Enter verb: ";
cin >> verb;
string object;
cout << "Enter object: ";
cin >> object;
fruit apple("apple");
fruit Fruits[] = { apple }; // to be extended in future
// returnObjectFromName(object, Fruits). ??? ()
}
How can I possibly get the fruit method with something similar to the function returnObjectFromName, if this is even possible?
I began the development with Python (independently), and there I can at least use eval(), but as I understand in C++ this is not an option.
I tried also with map, but I didn't manage to make it work with methods.
Thank you all for your answers.
Its not good way to rely on reflection in C++ and i think there is no way to list methods in classes. Maybe you can use function pointers but pointer to instance methods are hell.
I recommend to use polymorphism and good design. If some items might be taken, then use interface like this:
#include <iostream>
using namespace std;
class ITakeable {
public:
virtual bool isTakeable() = 0;
virtual void take() = 0;
virtual void cannotTake() = 0;
};
class fruit : public ITakeable {
public:
string actual_name;
fruit(string name){
actual_name = name;
}
bool isTakeable() {
return true;
}
void take() {
cout << "You take a " << actual_name << "." << endl;
}
void cannotTake() {
cout << "not needed to be implemented";
}
};
class airplane : public ITakeable {
public:
string actual_name;
airplane(string name){
actual_name = name;
}
bool isTakeable() {
return false;
}
void take() {
cout << "not needed to be implemented";
}
void cannotTake() {
cout << "You CANNOT take a " << actual_name << "." << endl;
}
};
int main() {
fruit apple("apple");
if (apple.isTakeable()) {
apple.take();
}
airplane plane("boeing");
if (plane.isTakeable()) {
plane.take();
} else {
plane.cannotTake();
}
// use of interface in general
ITakeable * something = &apple;
if (something->isTakeable()) {
something->take();
}
something = &plane;
if (something->isTakeable()) {
something->take();
} else {
something->cannotTake();
}
return 0;
}
Since fruit is a user defined type, you have to declare your own methods for your type or you inherit from one previously defined.
There are a lot of method for "built-in" string type
that Performs virtually the same job as eval (...) in python.
Also I noticed your function need not be defined independently outside of class fruit.
I am working on a class assignment to create three classes nested inside each other. I need to make constructors and deconstructors for each that have a message that goes along with them. Finally, I need to create an instance of each class using new and call the display() function to show their message, followed by delete.
I have completed the assignment but in the wrong way, and I am confused about how I can properly put the code into the heap instead of the stack (as I was advised by my course tutor).
This is what I started with: (this code seems to work well, but does not fulfill the assigned project)
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Hen {
public:
Hen();
~Hen();
string display(void) {
return ("Im a Hen");
}
class Nest;
friend Nest;
class Nest {
public:
Nest();
~Nest();
string display(void) {
return ("Im a Nest");
}
class Egg;
friend Egg;
class Egg {
public:
Egg();
~Egg();
string display(void) {
return ("Im an egg");
}
};
};
};
Hen::Hen() {
cout << "I construct Hens" << endl;
}
Hen::~Hen() {
cout << "I deconstruct Hens" << endl;
}
Hen::Nest::Nest() {
cout << "I construct Nests" << endl;
}
Hen::Nest::~Nest() {
cout << "I deconstruct Nests" << endl;
}
Hen::Nest::Egg::Egg() {
cout << "I construct Eggs" << endl;
}
Hen::Nest::Egg::~Egg() {
cout << "I deconstruct Eggs" << endl;
}
int main() {
Hen hone;
Hen::Nest none;
Hen::Nest::Egg eone;
string h, n, e;
h = hone.display();
n = none.display();
e = eone.display();
cout << h << "\n" << n << "\n" << e << endl;
}
Where I am stuck is when I try to implement my code inside the heap, it seems to break by the second class:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
class Hen {
public:
void display() {
cout << "Im a Hen" << endl;
}
class Nest;
friend Nest;
class Nest {
public:
void display() {
cout << "Im a Nest" << endl;
}
class Egg;
friend Egg;
class Egg {
public:
void display() {
cout << "Im an egg" << endl;
}
};
};
};
int main() {
Hen *hone = new Hen();
Hen::Nest *none = new Nest();
hone -> display();
none -> display();
}
Question 1:
If I remove all the information related to nest, the program runs Hen just fine and returns the "I'm a hen" statement. But, when I add in nest, the warning I recieve is
"error: expeected type-specifier before 'Nest'
Hen::Nest *none = new Nest();"
I do not understand what I am doing wrong as I did the exact same process for Hen and it worked. I do know that the error must be in the way Nest gets called through hen?
I apologize if this question is obvious, but I am just starting c++ and do not understand why I am getting these messages...
Thanks for your help!
Now,I hava a class with some components to do things while these components also need the reference of this parent class to update states.They coupling with each other make the design hard.Here is a example code snippet:
#include<iostream>
#include <windows.h>
#include <math.h>
using namespace std;
class man {
public:
int health{ 10 }, x, y;
void update();
private:
class moveCom *_move;
class judgeCom *_judge;
};
class moveCom {
public:
void update(man&m) {
++m.x;
++m.y;
}
};
class judgeCom {
public:
void update(man&m) {
if(rand()%10 <= 5)
++m.health;
else --m.health;
}
};
void man::update() {
_move->update(*this);
_judge->update(*this);
cout << health << " " << x << " " << y << endl;
}
int main() {
man m;
while (1) {
Sleep(200);
m.update();
}
return 0;
}
This code can only work in the same cpp file which means if split them into different hpp/cpp will cause compile error.Is there any practice to do better?
Edit:Sorry,i try it again and it okay with separate.
I'm working with my code and I faced this problem. I want to put 3 different classes data in single vector, so I created base class vector. It look like this :
std::vector <Video*> vid_list;
When I read the file I push data in this vector using one of the derived class constructors like that :
vid_list.push_back(new Official_Music_Video(video_title, video_time,
Date(y,m,d), num_of_views));
so I want to display this data on the screen, but my base class only got methods for
std::string title;
double time;
Date date_of_release;
but not for int number_of_views. I heard that if I wanna use derived class getter method I have to use upcasting so I tried to make this
void views (Video& v) {
v.get_views();
}
and then in my display method
void Official_Music_Video::display_info(std::vector <Video*>& vid_list){
Official_Music_Video ofc;
for (unsigned int i = 0; i < vid_list.size(); i++){
cout << vid_list[i]->get_title() << " " << vid_list[i]->get_time()
<< " " << vid_list[i]->write_year_to_file() << "-" <<
vid_list[i]->write_month_to_file() << "-" << vid_list[i]-
>write_day_to_file () << endl;
cout << "This video viewed " << vid_list[i].views(ofc) << " times"
<< endl;
}
}
But it's not working. Can you help me understand where I made mistake and what's wrong ?
If all three of your derived classes have a "number_of_views" method, put it in the base class as well, and problem solved. If not, then your code is nonsensical. What do you expect to happen when you put an "Unofficial_Music_Video" into the vector, and run your loop over it?
If you really need to add per-type behavior to the class, consider what you actually want to accomplish. Do you need users of Video to be able to query "number_of_views", despite the fact that field doesn't exist for all subtypes? Maybe you just want to access a human-readable description of the statistics? If that's the case, it might make the most sense to add a "virtual std::string StatsSummary()" method to Video, and make subclasses implement it as required.
You might want to move some of the implementation to the base class or so, but this is something that could help you finding the solution:
#include <vector>
#include <string>
#include <iostream>
class Date // for demo only
{
public:
Date(int y, int, int m, int d)
: y(y) , m(m), d(d) {}
int y;
int m;
int d;
};
class Video
{
public:
virtual std::string get_title() = 0;
virtual int get_time() = 0;
virtual int write_year_to_file() = 0;
virtual int write_month_to_file() = 0;
virtual int write_day_to_file () = 0;
virtual ~Video(){}
virtual void display_info()
{
std::cout << get_title()
<< " " << get_time()
<< " " << write_year_to_file()
<< "-" << write_month_to_file()
<< "-" << write_day_to_file ()
<< std::endl;
}
};
class VideoWithViewCount
: public Video
{
public:
virtual int views() = 0;
virtual void display_info()
{
Video::display_info();
std::cout << "This video viewed " << views() << " times" << std::endl;
}
};
class Official_Music_Video : public VideoWithViewCount
{
public:
Official_Music_Video(std::string _video_title, int _video_time, Date _date, int _Num_of_views)
: d(_date)
, time(_video_time)
, Num_of_views(_Num_of_views)
, video_title(_video_title)
{}
virtual int get_time() override {return time; };
virtual int write_year_to_file() override { return d.y; } // better call them get...
virtual int write_month_to_file() override {return d.m; }
virtual int write_day_to_file () override { return d.d; }
virtual int views() override {return Num_of_views; };
private:
Date d;
int time;
int Num_of_views;
std::string video_title;
};
class Other_Video : public Video
{
public:
Other_Video(std::string _video_title, int _video_time, Date _date)
: d(_date)
, time(_video_time)
, video_title(_video_title)
{}
virtual int get_time() override {return time; };
virtual int write_year_to_file() override { return d.y; }
virtual int write_month_to_file() override {return d.m; }
virtual int write_day_to_file () override { return d.d; }
private:
Date d;
int time;
std::string video_title;
};
int main(int,char**)
{
std::vector <Video*> vid_list;
// could also use unique_ptr or shared_ptr from #include <memory> in vector as follows:
// std::vector <unique_ptr<Video> > vid_list;
// std::vector <shared_ptr<Video> > vid_list;
// fill video vector
// vid_list.push_back(new Official_Music_Video(video_title, video_time, Date(y,m,d), num_of_views));
for (auto v : vid_list) // when using unique_ptr remember to use "const auto&" instead of "auto"
{
v->display_info();
}
return 0;
}
I was studying c++ language with shared pointer and builder pattern.
I have written following code that is not working but I don't understand why it emits run-time error.
Could you tell me why it is not working well and how can I solve this problem to work well?
#include <iostream>
#include <memory>
#include <string>
using namespace std;
class Popup
{
public:
Popup(int value, string str){
this->v = value;
this->str = str;
}
virtual void print() = 0;
int v;
string str;
};
typedef shared_ptr<Popup> PopupPtr;
class PopupA : public Popup
{
public:
PopupA(int v, string str) : Popup(v, str) { }
virtual void print() {
cout << "PopupA" << endl;
}
};
typedef shared_ptr<PopupA> PopupAPtr;
class PopupB : public Popup
{
public:
PopupB(int v, string str) : Popup(v, str) { }
virtual void print() {
cout << "PopupB" << endl;
}
};
typedef shared_ptr<PopupB> PopupBPtr;
class Builder
{
public:
PopupPtr popupPtr;
Builder() { };
shared_ptr<Builder> init(int value, string str) {
shared_ptr<Builder> builder;
switch (value)
{
case 1:
popupPtr = PopupAPtr(new PopupA(value, str));
break;
case 2:
popupPtr = PopupBPtr(new PopupB(value, str));
break;
default:
cout << "default error" << endl;
break;
}
if (popupPtr) {
builder = shared_ptr<Builder>(this);
}
else {
cout << "popup is null" << endl;
}
if (!builder) {
cout << "builder is null" << endl;
}
return builder;
}
PopupPtr build()
{
if (!popupPtr) {
cout << "popup is null" << endl;
}
return PopupPtr(popupPtr);
}
};
typedef shared_ptr<Builder> BuilderPtr;
int main()
{
BuilderPtr builderPtr = BuilderPtr(new Builder());
PopupPtr popupPtr1 = builderPtr->init(1, "111111111111")->build();
popupPtr1->print();
PopupPtr popupPtr2 = builderPtr->init(2, "222222222222")->build();
popupPtr2->print();
return 0;
}
Thanks in advance for your answers and sorry for my poor english. If you don't understand my question please make a comment.
Your problem is this line:
builder = shared_ptr<Builder>(this);
This will not create a copy of the std::shared_ptr already tracking this, nor will it affect the reference count of it. This creates an entirely new shared pointer which will track this independently, causing a double-delete when both of the reference counts hit zero.
Fortunately, the standard library provides a solution to this problem in the form of std::shared_from_this.
First you need to enable this for your class:
class Builder : std::enable_shared_from_this<Builder>
{
//...
};
Then instead of creating a new std::shared_ptr from this, call std::shared_from_this:
builder = std::shared_from_this();