G
Gary Wessle
Hi
Stroustrup p.312 second paragraph after the code.
" To get polymorphic behavior in C++, the member function called must
be virtual and objects must be manipulated through pointers or
references."
the code below fulfills the virtual part, can someone please show me
how the second part is done? i.e, manipulating the object through
pointers or references.
thank you
//task.h
****************************************************************
#ifndef TASK_H
#define TASK_H
#include <string>
class Task {
public:
virtual void print_mi()=0; // print menu item
};
class Thr_task : public Task {
std::string m_item;
public:
Thr_task(const std::string&);
void print_mi();
};
#endif
//task.cpp
****************************************************************
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using namespace std;
#include "task.h"
Thr_task::Thr_task(const string& n) :
m_item(n)
{
}
void Thr_task:rint_mi() {
cout << m_item << endl;
}
//main.cpp
****************************************************************
#include "task.h"
using std::vector;
using namespace std;
int main() {
Thr_task t1("preform Thread task.");
vector<Task*> vTp;
vTp.push_back(&t1);
vTp[0]->print_mi();
Stroustrup p.312 second paragraph after the code.
" To get polymorphic behavior in C++, the member function called must
be virtual and objects must be manipulated through pointers or
references."
the code below fulfills the virtual part, can someone please show me
how the second part is done? i.e, manipulating the object through
pointers or references.
thank you
//task.h
****************************************************************
#ifndef TASK_H
#define TASK_H
#include <string>
class Task {
public:
virtual void print_mi()=0; // print menu item
};
class Thr_task : public Task {
std::string m_item;
public:
Thr_task(const std::string&);
void print_mi();
};
#endif
//task.cpp
****************************************************************
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using namespace std;
#include "task.h"
Thr_task::Thr_task(const string& n) :
m_item(n)
{
}
void Thr_task:rint_mi() {
cout << m_item << endl;
}
//main.cpp
****************************************************************
#include "task.h"
using std::vector;
using namespace std;
int main() {
Thr_task t1("preform Thread task.");
vector<Task*> vTp;
vTp.push_back(&t1);
vTp[0]->print_mi();