Inherit Class functions

Kindly assist. I would like to create class A and inherit datetype functions. to be able to call out like.

A parent;
A.Date.SetDay... //something like dat. Anyone who knows how. Kindly guide.

class A{
public:
//get and set method of class date
void SetDate(DateType date) {m_date = date;}
DateType GetDate() {return m_date;}

private:
//refers to the date
DateType m_date;
};

class DateType{
public :
//get and set method of variable day
void SetDay(short day) {m_day = day;}
short GetDay() {return m_day;}
private : ---- members not included-----
};
You don't need a object within your A class for enheritance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class DateType{
public:
  DateType();
  ~DateType();

  void SetDay(short day);
};

class A: public DateType{
public:
  A();
  ~A();

  void A_func();
};

int main()
{
  A a;
  a.SetDay(2);
  a.A_func();

  return 0;
}
in programming logic doesnt seem right.

any better suggestions.

bcos date class is supposed to be inside my A class. created a date class bcos of storing of day month year.
I gave you a inheritance example. You get all the functionality of DateType inside your A function. So i don't really see what you want to do...

but this is an example with a private DateType object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class DateType{
public :
  DateType();
  ~DateType();
  void SetDay(short day)	 {m_day = day;}
  short GetDay()	 {return m_day;}
private :
  short m_day;
};

class A{
public:
  A();
  ~A();
  void SetDate(DateType date)	 {m_date = date;}
  DateType* GetDate()	 {return &m_date;} //returns pointer to m_date

private:
  DateType m_date;
};

int main()
{
  A a;
  DateType date;

  a.SetDate(date);
  a.GetDate()->SetDay(2);

  return 0;
}


but this way you could also make m_date public.
Last edited on
Thanks! i'm looking for the

a.getdate()->setDay(2);

as i'm not familiar with the syntax of C++

but what does DateType* getDate() {return &m_date;} means?
it returns a pointer to the private m_date inside the A class.
to access object pointer elements use '->', that is why i used that.
It is done this way, because otherwise there is no way to alter a private element outside a object.

But using it this way has the same effect as editing a public element.
So maybe that is a better way, make m_date public.

You could also make a member function that takes in a day and set it to the private m_date.
But then i would advise you to use the inhertance example i gave you in my first post.
Last edited on
okay! Thanks!
closed account (zb0S216C)
On a side note, an interface must have a virtual destructor. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Interface
{
    virtual ~Interface( ) { } // VITAL!

    virtual inline void Print( ) const = 0;
};

class Object : public Interface 
{
    public:
        inline void Print( ) const
        {
            std::cout << "Object::Print( )" << std::endl;
        }
};

int main( )
{
    Object New_Object;
    New_Object.Print( );

    return( 0 );
}


Wazzak
Last edited on
What is the point of having a private member variable if you have a public member function that returns a non-const pointer to it, thus giving full public access to it? This is wrong on so many levels.
That is exactly what i was trying to make clear...
Topic archived. No new replies allowed.