Calling parent class function within child

I'm not sure where I'm going wrong and google unfortunately isn't helping.
How do I go about calling a function from the parent group within one from the child group?

My header file
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
#ifndef WEEKDAY_H_H
#define WEEKDAY_H

#include <iostream>
#include <string>
#include <ctime>


using namespace std;

class DateTime{
  public:
    DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
    protected:         
    string get_string_component(char option, tm* dateStruct);
    int get_year_days(tm* dateStruct);
    struct tm DTstruct;
  private:
    bool validate_data( int y, int m, int d, int h, int min, int s); 
    }; 
   

class WeekDay : public DateTime{
    public: 
    WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
};   
 
#endif 


My source file
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "weekday.h"
#include <cstdlib>


WeekDay::WeekDay(int y, int m, int d, int h, int min, int s){
    : DateTime(y, m, d, h, min, s);
}


 WeekDay::display(){
    : display();
     cout <<" is" << DTstruct.tm_wday
     
 }

 
DateTime::DateTime(int y, int m, int d, int h, int min, int s){
    DateTime::validate_data(y, m, d, h, min, s);
    DTstruct.tm_year = y - 1900;  // Legacy problem
    DTstruct.tm_mon = m - 1;      // month starts from 0 not 1
    DTstruct.tm_mday = d;
    DTstruct.tm_hour = h;
    DTstruct.tm_min = min;
    DTstruct.tm_sec = s;
    DTstruct.tm_wday = 0;
    DTstruct.tm_yday = 0;
    DTstruct.tm_isdst = 0;
    mktime ( &DTstruct );

}


string DateTime::get_string_component(char component, tm* dateStruct){
    char format[3];
    format[0] = '%';
    format[1] = component;
    format[2] = '\0';
    char ans [80];
    strftime(ans, 80, format, dateStruct);
    string strans(ans);
    return strans;
    } 
 
int DateTime::get_year_days(tm* DTstruct){
    return DTstruct->tm_yday;
    } 
 
 void DateTime::display(){
    cout <<   DateTime::get_string_component('c', &DTstruct);

 }
 
 
 
 bool DateTime::validate_data(int y, int m, int d, int h, int min, int s) {
    if ( y >= 1970 && y <= 2020 && m >= 1 && m <= 12 && d >= 1 && d <= 31 && h >= 0 && h <= 23 && min >= 0 && min <= 59 &&s >= 0 && s <= 59){
        return true;
    } else
    cout << "Incompatible data!" << endl;
    exit(EXIT_FAILURE);
}


main file (cannot be changed)
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
32
33
34
35
36
#include <iostream>
#include "weekday.h"

using namespace std;

int main(){
    WeekDay WD1(2015, 1, 2, 3, 4, 5);
    WeekDay WD2(2015, 1, 2, 3, 4);
    WeekDay WD3(2015, 1, 2, 3);
    WeekDay WD4(2015, 1, 2);
    
    // WeekDay WD(2086, 1, 2)  // error
    // WeekDay WD(1945, 1, 2)  // error
    // WeekDay WD(2015, 0, 2)  // error
    // WeekDay WD(2015, 13, 2)  // error
    // WeekDay WD(2015, 1, 0)  // error
    // WeekDay WD(2015, 1, 32)  // error
    // WeekDay WD(2015, 13, 2, -1)  // error
    // WeekDay WD(2015, 13, 2, 24)  // error
    // WeekDay WD(2015, 13, 2, 3, -1)  // error
    // WeekDay WD(2015, 13, 2, 3, 60)  // error
    // WeekDay WD(2015, 13, 2, 3, 4, -1)  // error
    // WeekDay WD(2015, 13, 2, 3, 4, 60 )  // error
    
    WD1.display();
    cout << endl;
    WD2.display();
    cout << endl;
    WD3.display();
    cout << endl;
    WD4.display();
    cout << endl;
    
    return 0;
}


Where am I going wrong? Is there some website while would be applicable here that will help?

Many thanks
Sarah
Last edited on
1
2
3
4
5
6
7
8
 WeekDay::display(){

    // : display();
    DateTime::display() ;

     cout <<" is" << DTstruct.tm_wday
     
 }


Ideally add a const qualifier to the member functions (like display()) that do not modify the object.
See: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Ok, so I have been able to get it working using the below in my source cpp (I also added a Bool Validate_Data in my private child class

1
2
3
4
5
6
7
8
WeekDay::WeekDay(int y, int m, int d, int h, int min, int s) : DateTime(y, m, d, h, min, s){
}


void WeekDay::display(){
    DateTime::display() ;
     cout <<" is " << DateTime::get_string_component('A', &DTstruct);
 }


However I can't seem to get it working ok, it's only showing results as
01/02/15 03:04:05 is Friday
01/02/15 03:04:00 is Friday
01/02/15 03:00:00 is Friday
01/02/15 00:00:00 is Friday

Even though I've changed dates in the main cpp >_<
Last edited on
You may want to post the current (modified version of the) code.
header file
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
32
33
#ifndef WEEKDAY_H
#define WEEKDAY_H

#include <iostream>
#include <string>
#include <ctime>


using namespace std;

class DateTime{
  public:
    DateTime(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
    protected:         
    string get_string_component(char option, tm* dateStruct);
    int get_year_days(tm* dateStruct);
    struct tm DTstruct;
  private:
    bool validate_data( int y, int m, int d, int h, int min, int s); 
    }; 
   

class WeekDay : public DateTime{
    public: 
    WeekDay(int y, int m, int d, int h = 0, int min = 0, int s = 0);
    void display();
    private:
    bool validate_data( int y, int m, int d, int h, int min, int s); 
};

   
#endif 


Source file
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdlib>
#include "weekday.h"


WeekDay::WeekDay(int y, int m, int d, int h, int min, int s) : DateTime(y, m, d, h, min, s){
}


void WeekDay::display(){
    DateTime::display() ;
     cout <<" is " << DateTime::get_string_component('A', &DTstruct);
 }

 
DateTime::DateTime(int y, int m, int d, int h, int min, int s){
    DateTime::validate_data(y, m, d, h, min, s);
    DTstruct.tm_year = y - 1900;  // Legacy problem
    DTstruct.tm_mon = m - 1;      // month starts from 0 not 1
    DTstruct.tm_mday = d;
    DTstruct.tm_hour = h;
    DTstruct.tm_min = min;
    DTstruct.tm_sec = s;
    DTstruct.tm_wday = 0;
    DTstruct.tm_yday = 0;
    DTstruct.tm_isdst = 0;
    mktime ( &DTstruct );

}


string DateTime::get_string_component(char component, tm* dateStruct){
    char format[3];
    format[0] = '%';
    format[1] = component;
    format[2] = '\0';
    char ans [80];
    strftime(ans, 80, format, dateStruct);
    string strans(ans);
    return strans;
    } 
 
int DateTime::get_year_days(tm* DTstruct){
    return DTstruct->tm_yday;
    } 
 
 void DateTime::display(){
    cout <<   DateTime::get_string_component('c', &DTstruct);

 }
 
 
 
 bool DateTime::validate_data(int y, int m, int d, int h, int min, int s) {
    if ( y >= 1970 && y <= 2020 && m >= 1 && m <= 12 && d >= 1 && d <= 31 && h >= 0 && h <= 23 && min >= 0 && min <= 59 &&s >= 0 && s <= 59){
        return true;
    } else
    cout << "Incompatible data!" << endl;
    exit(EXIT_FAILURE);
}


main file
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
32
33
34
35
36
#include <iostream>
#include "weekday.h"

using namespace std;

int main(){
    WeekDay WD1(2017, 2, 2, 3, 4, 5);
    WeekDay WD2(2015, 1, 5, 3, 4);
    WeekDay WD3(2015, 2, 2, 3);
    WeekDay WD4(2015, 6, 2);
    
    //WeekDay WD(2086, 1, 2) ; // error
    // WeekDay WD(1945, 1, 2)  // error
    // WeekDay WD(2015, 0, 2)  // error
    // WeekDay WD(2015, 13, 2)  // error
    // WeekDay WD(2015, 1, 0)  // error
    // WeekDay WD(2015, 1, 32)  // error
    // WeekDay WD(2015, 13, 2, -1)  // error
    // WeekDay WD(2015, 13, 2, 24)  // error
    // WeekDay WD(2015, 13, 2, 3, -1)  // error
    // WeekDay WD(2015, 13, 2, 3, 60)  // error
    // WeekDay WD(2015, 13, 2, 3, 4, -1)  // error
    // WeekDay WD(2015, 13, 2, 3, 4, 60 )  // error
    
    WD1.display();
    cout << endl;
    WD2.display();
    cout << endl;
    WD3.display();
    cout << endl;
    WD4.display();
    cout << endl;
    
    return 0;
}


It hasn't changed aside from those changes mentioned above. I'm not sure what I'm doing wrong
Your program is working correctly (gives the expected results).

A few things you may want to consider:

a. Avoid placing using namespace std; in a header file

Sutter and Alexandrescu in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices':
"Don't write namespace usings in a header file or before an #include."
http://www.cplusplus.com/forum/beginner/212153/#msg992672

CoreGuidelines: "Don't write using namespace in a header file"
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-using-directive


b. You may have using namespace std; in an implementation file if you feel that it makes your code more readable. There is a trade off; not placing using namespace std; at global scope has its own advantages; it is your decision.

Pro:
CoreGuidelines: "Use using namespace directives for transition, for foundation libraries (such as std), or within a local scope (only)"
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rs-using

Sutter and Alexandrescu in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices':
"Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable."
http://www.cplusplus.com/forum/beginner/212153/#msg992672

Con:
LLVM Coding guidelines: "using explicit namespace prefixes makes the code clearer, because it is immediately obvious what facilities are being used and where they are coming from. And more portable... "
http://www.cplusplus.com/forum/beginner/212153/#msg992672


c. Use const where ever possible: if something can be const, declare it as const

Member functions: https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Variables: http://www.cplusplus.com/forum/beginner/180986/#msg888218


d. DTstruct is a protected member of the class DateTime; it can be accessed by directly in a member function. There is no need pass its address as an argument to functions like DateTime::get_string_component


header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef WEEKDAY_H
#define WEEKDAY_H

#include <string>
#include <ctime>

// using namespace std;

class DateTime {

    public:
        DateTime( int y, int m, int d, int h = 0, int min = 0, int s = 0 );
        void display() const ; // *** const

    protected:
        std::string get_string_component( char option ) const; // *** const
        int get_year_days() const ; // *** const
        std::tm DTstruct;

    private:
        bool validate_data( int y, int m, int d, int h, int min, int s) const ; // *** const
};

#endif // WEEKDAY_H 


implementation
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "weekday.h" // ideally, make this the first #include in the implementation file
#include <cstdlib>
#include <iostream>

WeekDay::WeekDay(int y, int m, int d, int h, int min, int s) : DateTime(y, m, d, h, min, s){
}


void WeekDay::display() const { // *** const

    DateTime::display() ;
    std::cout <<" is " << DateTime::get_string_component( 'A' );
}


DateTime::DateTime(int y, int m, int d, int h, int min, int s) {

    DateTime::validate_data(y, m, d, h, min, s);
    DTstruct.tm_year = y - 1900;  // Legacy problem
    DTstruct.tm_mon = m - 1;      // month starts from 0 not 1
    DTstruct.tm_mday = d;
    DTstruct.tm_hour = h;
    DTstruct.tm_min = min;
    DTstruct.tm_sec = s;
    DTstruct.tm_wday = 0;
    DTstruct.tm_yday = 0;
    DTstruct.tm_isdst = 0;
    mktime ( &DTstruct );

}


std::string DateTime::get_string_component( char component ) const {

    const char format[3] = { '%', component, 0 }; // *** const
    char ans [80];
    strftime( ans, sizeof(ans), format, &DTstruct );
    return ans; // implicit conversion from const char* to std::string
}

int DateTime::get_year_days() const {
    return DTstruct.tm_yday;
}

 void DateTime::display() const {

    std::cout <<   DateTime::get_string_component( 'c' );
}

 bool DateTime::validate_data(int y, int m, int d, int h, int min, int s) const {

    if ( y >= 1970 && y <= 2020 && m >= 1 && m <= 12 && d >= 1 && d <= 31 && h >= 0 && h <= 23 && min >= 0 && min <= 59 &&s >= 0 && s <= 59){
        return true;
    } else

    std::cerr << "Incompatible data!\n" ;
    std::exit(EXIT_FAILURE);
}


http://coliru.stacked-crooked.com/a/1bce2a0f56eb3d8f
Topic archived. No new replies allowed.