Non-member function and abstract class - visual-c++

So the objective is to create an abstract 'shape' class with 'rectangle' and 'triangle' as derived classes. Finishing the assignment with printing the perimeters and areas of a rectangle and triangle using a non-member function in main(). I'm having trouble on how to use just one non-member function, as opposed to making a print function for each type of shape:
void printPerimeter(Triangle triangle);
void printPerimeter(Rectangle rectangle);
//I'm trying not to do this
//what I had in mind, but don't know how to work this problem.
void printPerimeter(Shape shape)
{
float temp;
temp = shape.calcPerimeter();
cout << "Perimeter: " << temp << endl;
}
I know an abstract class can't be passed into a function, so how could I go about this?

class Shape
{
private:
public:
virtual void calcPerimeter()
{
cout << "Shape" << endl;
}
};
class Triangle1 : public Shape
{
public:
void calcPerimeter()
{
cout << "Triangle" << endl;
}
};
class Rectangle1 : public Shape
{
public:
void calcPerimeter()
{
cout << "Rectangle" << endl;
}
};
void printPerimeter(Shape* shape)
{
shape->calcPerimeter();
}
int main()
{
Shape *triangle = new Triangle1();
Shape *rectangle = new Rectangle1();
printPerimeter(triangle);
printPerimeter(rectangle);
return 0;
}
Please, check if this helps you to achieve your goal. Courtesy: #Retired Ninja

Related

How would I define move constructor and move assignment operator for unique_ptr of custom data type?

class threadMsg_t {
private:
string msg;
int type;
public:
threadMsg_t(const string& msg, int type) : msg(msg), type(type) {};
threadMsg_t& operator=(threadMsg_t&& oldMsg)
{
msg = move(oldMsg.msg);
type = move(oldMsg.type);
return *this;
}
threadMsg_t(threadMsg_t&& oldMsg) {
msg = move(oldMsg.msg);
type = move(oldMsg.type);
}
int getType() { return type; }
string getMsg() { return msg; }
};
using msgP_t = unique_ptr<threadMsg_t>;
static queue<msgP_t> q;
static condition_variable cv;
static mutex mtx;
static void postMsg(threadMsg_t* newMsg)
{
unique_lock<mutex> lck(mtx);
q.push(std::move(msgP_t{newMsg}));
cv.notify_one();
}
class workerThread_t {
public:
static void processMsg()
{
while(true) {
unique_lock<mutex> lck{mtx};
if(q.empty()) {
cout << "Waiting for the message!" << endl;
cv.wait(lck, []() { return !(q).empty(); });
}
if(q.empty()) {
continue;
}
auto msgP = move(q.front());
threadMsg_t* msg = msgP.get();
q.pop();
cout << "Processed Message - " << msg->getMsg() << endl;
lck.unlock();
}
}
thread* getThread()
{
return m_workerThread;
}
workerThread_t() {
m_workerThread = new thread(&processMsg);
}
~workerThread_t()
{
delete(m_workerThread);
}
private:
thread* m_workerThread;
};
Code works fine. But wondering the behavior when I do move(unique_ptr object) inside postMsg(). Seems like its not invoking the move constructor defined. Not sure whether its possible to define constructor for unique_ptr. I know that its needed for a custom structure containing "string" and "int". How would it behave if I have a ptr inside the structure?

how can i make objects in main function?

#include<iostream>
using namespace std;
class distance //class
{
private:
int feet;
float inches;
public:
distance() :feet(0), inches(0.0) //constructor
{}
distance(int ft, float in) :feet(ft), inches(in) //constructor
{}
void getdist() //get values
{
cout << "Enter feet :";
cin >> feet;
cout << "Enter inches :";
cin >> inches;
}
void showdist() //show values
{
cout << feet << "-" << inches << endl;
}
void adddist(distance, distance); //add distance through objects
};
void distance::adddist(distance d2, distance d3) //add distance
{
inches = d2.inches + d3.inches;
feet = 0;
if (inches >= 12.0)
{
inches -= 12.0;
feet++;
}
feet += d2.feet + d3.feet;
}
int main()
{
}
I'm writing a few classes for a homework assignment and I want it to be impossible for my class member functions to be called in main. If they are, I want the program to exit. How would I know when my member functions are being called?

c++ for each full cycle

i have a vector:
vector<Player> players;
players.push_back(Player(20000));
players.push_back(Player(10000));
players.push_back(Player(12000));
players.push_back(Player(32000));
players.push_back(Player(22000));
players.push_back(Player(18000));
i loop through it like this:
for (Player &player : players)
std::cout << player._gold << endl;
Is there some way to name a starting index and make a full loop through players?
like
//pseudo
for (Player &player : players, 2)
std::cout << player._gold << endl;
would give
12000
32000
22000
18000
20000
10000
note that it starts from the 3rd element (index 2), and it doesnt stop at the end of the vector. Please give me some keyword or example how to do it the most simple way in c++.
You need to create an adaptor object, something like
template<class T>
class iterator_range
{
private:
typename T::iterator begin_;
typename T::iterator end_;
public:
iterator_range(typename T::iterator b, typename T::iterator e):begin_(b),end_(e)
{
}
typename T::iterator begin() const
{
return begin_;
}
typename T::iterator end() const
{
return end_;
}
};
You can now do
for (Player &player : iterator_range(players.begin()+2,players.end()))
std::cout << player._gold << endl;

Runnable implementation using packaged_task in c++11

I am trying to create a Runnable interface in c++11 using packaged_task, with child class overriding run() function. I don't know why this code is not compiling. Its giving error related to type argument.
/usr/include/c++/4.8.1/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of()>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
Below is my code snippet. Could someone plz give me some information on this error and whether implementing Runnable this way is a right way to proceed ?
class Runnable {
public:
explicit Runnable() {
task_ = std::packaged_task<int()>(&Runnable::run);
result_ = task_.get_future();
std::cout << "starting task" << std::endl;
}
virtual int run() = 0;
int getResult() {
task_();
return result_.get();
}
virtual ~Runnable() {
std::cout << "~Runnable()" << std::endl;
}
private:
std::future<int> result_;
std::packaged_task<int()> task_;
};
class foo : public Runnable {
int fib(int n) {
if (n < 3) return 1;
else return fib(n-1) + fib(n-2);
}
public:
explicit foo(int n) : n_(n) {}
int run() {
cout << "in foo run() " << endl;
int res = fib(n_);
cout << "done foo run(), res = " << res << endl;
return res;
}
~foo() {}
private:
int n_;
};
int main(int argc, char*argv[]) {
stringstream oss;
oss << argv[1];
int n;
oss >> n;
shared_ptr<foo> obj(new foo(n));
obj->run();
cout << "done main" << endl;
return 0;
}

C++ passing an object to a function, the operator= is not called

So here is the code snippet:
class MyClass { public: MyClass(char chIn) { std::cout <<
"Constructor!" << std::endl; }
MyClass & operator= (char chIn) { std::cout << "Assigment
operator!" << std::endl; } } ;
void Func(MyClass objIn) { return; }
int _tmain(int argc, _TCHAR* argv[]) { Func('T'); system("PAUSE");
return 0; }
In the upper example the constructor of the object is called!!!! Why is this behavior? Shouldn't the assigment operator be called? Because we're assigning a value to the function parameter, aren't we?
operator= invoked for already existent object otherwise constructor(or copy constructor) is used to create needed instance

Resources