Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22001

Class with a pointer to base class containing virtual method

$
0
0

This is a simple program to calculate a worker's salary based on contract type. In class worker I have a pointer to Contract. I declare in main whether it points to contract1 or contract2. But my method in worker that should show me how much money worker earns doesn't work, program exits with random values. I tried compiling it in visual studio and everything seems fine there. So could you explain me what's the problem with gcc and dev c++ ?

In main i have:

Contract *au=new Contract1(100);
    Worker ap("worker1",au);
    cout<<ap.salary()<<endl;

then in my worker.h file:

class Contract{
    protected:
        double money;
    public:
        Contract(double money):money(money){};
        virtual double getSalary()=0;

};

class Contract1:public Contract{
    public:
        Contract1(double money):Contract(money){};
        double getSalary(){
            return 0.9*money;
        }
};

class Contract2:public Contract{
    public:
        Contract2(double money):Contract(money){};
        double getSalary(){
        return 0.8*money;
        }

};

class Worker{
    protected:
        string name;
        Contract *contract;
    public:
        Worker(string n, Contract *c):name(n),contract(c){};
        double salary(){
            return contract->getSalary();
        };

};

Viewing all articles
Browse latest Browse all 22001

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>