Sorry in advance !! I have just finished learning OOP lessons in class, but my teacher he just provided the students a small exercise (class and object). Now, i really want the OOP idea (project) which are related to inheritance, overloading, polymorphism else,or if you have any links that related to what i asked just send them to me please !! Don't mind if i had made mistakes.

Recommended Answers

All 5 Replies

There are a gazillion articles on the Net that cover this subject. You need to do some serious reading. The subjects of inheritance (dog inherits from animal, so does human), polymorphism (virtual int number_of_legs() - returns 2 for humans, 4 for dogs or cats but calling on a pointer or reference to animal will return the correct value), overloading - usually applied to operators such as assignment, comparison, math, or output operators. So, what language are you supposed to use for your exercise?

In c++ for sure !!

So, what do you know about these subjects, and implementing examples in C++? Show what you might do, and we can help you, but I will NOT show you directly how to - that is the purpose of an education, to figure this stuff out from your text books and classes. Also, there is a ton of stuff on the Internet to help you.

// Inheritance example.
using namespace std;
class base
{
private:
    int m_foo;
public:
    base(int afoo = 0) : m_foo(afoo) {}
    virtual ~base() { cout << "~base() called." << endl; }
    int getFoo() const { return m_foo; }
    void setFoo(int afoo) { m_foo = afoo; }
};

class derived : public base
{
private:
    float m_bar;
public:
    derived() : m_bar(0.0) {}
    virtual ~derived() { cout << "~derived() called." << endl; }
    float getBar() const { return m_bar; }
    void setBar(float abar) { m_bar = abar; }
};

This is an example of inheritance and polymorphism. When an instance of a "derived" class is destroyed, you will get this output:

~derived() called.
~base() called.

And no, this won't compile and run - there are missing includes needed - you get to figure that out!

Not to mention a main() function... :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.