Hello

I am studying for my exam & I came across this question that is stumping me.

class X
{
   public:
      X();
   private:
      int x1, x2;
};

class Y : public X
{
    public :
     Y();
   private:
     int y3;
}  // the class Y does not end in an '};' but just a } maybe thats a typo or its meant to be like that

Question:
Describe the kinds of operations which would be implemented in function Y();

I know what constructors are & I recognise that X(); & Y(); are default constructors BUT I have never seen this before class Y : public X

What does that mean?

Does it mean,an object of class Y can be accessed by an object of class X because the class Y is a public 'element/function?' of class X??

This means that class x is inheriting class y using the public method meaning that it can contains the same public members as class x.

@gretty: I have never seen this before class Y : public X. What does that mean?

Reusability is important feature of object-oriented programming. C++ strongly supports the concept of reusability. The C++ classes can be reused in two ways:

Creating an object

Once a class has been created and tested, it should represent a useful unit of code. The simplest way to reuse a class is to just use an object of that class directly.


Inheritance

Inheritance : reusing the interface. Once a class has been written and tested, it can be adapted by other progrmmers to suit their requirements. This is basically done by creating new classes, reusing properties of the existing classes. The mechanism of deriving a new class from an old one is called "Inheritance" or "derivation". The old class is referred to as the "base" or "super" class and the new one is called "derived" or "sub" class.

An inheritance relationship is one in which one class derived from another class. Derived class's declaration space implicitly contains all of the non-constructor members of the base class.

Take a look at this tutorial - http://www.cplusplus.com/doc/tutorial/inheritance/

Edited 7 Years Ago by __avd: n/a

This article has been dead for over six months. Start a new discussion instead.