How do I use a class as a member of a class and other questions

I am learning C++ through online tutorials, though I must have not gotten to the part where this sort of thing is described.

I am trying to create a class that has a member class which can get or set a variable of the base class.

I am failing miserably. Also, what is the terminology for this called? I am trying to keep everything as simply as possible.

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
// Class.h
class SecondClass {
    public:
        SecondClass();
        ~SecondClass();
        int setBaseClassVar(int num);
        int getBaseClassVar();

};
class BaseClass {
    public:
        class SecondClass sc;
        BaseClass();
        ~BaseClass();
        int variable;

};

// Class.cpp
BaseClass::BaseClass() { cout << "BaseClass constructor\n"; variable = 1; };
BaseClass::~BaseClass() { cout << "BaseClass destructor\n"; };

SecondClass::SecondClass() { cout << "SecondClass constructor\n"; };
SecondClass::~SecondClass() { cout << "SecondClass destructor\n";};
int SecondClass::setBaseClassVar(int num) { BaseClass::variable = num; };
int SecondClass::getBaseClassVar() { cout << BaseClass::variable; };

// Main.cpp
int main(int argc, char *argv[]) {

    BaseClass bc;
    bc.sc.getBaseClassVar();
    bc.sc.setBaseClassVar(200);
    bc.sc.getBaseClassVar();
    return 0;

}
Define the first class in first_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// first_class.h

// include guard. see: https://en.wikipedia.org/wiki/Include_guard
#ifndef FIRST_CLASS_H_INCLUDED_
#define FIRST_CLASS_H_INCLUDED_

struct first_class
{
    // make the class responsible for maintaining class invariants
    // see: http://www.cplusplus.com/forum/beginner/142434/#msg751870
    // http://www.cplusplus.com/forum/beginner/152947/#msg794572
    int value() const ;
    void value( int new_value ) ;

    private: int val = 23 ;
};

#endif // FIRST_CLASS_H_INCLUDED_ 


Define the second class in second_class.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// second_class.h

#ifndef SECOND_CLASS_H_INCLUDED_
#define SECOND_CLASS_H_INCLUDED_

// the second class has a non-static member of type first_class
// using in name will not suffice; we need the definition of first_class
#include "first_class.h"

struct second_class
{
    int bar() const ;
    void foo( int a ) ;

    private: first_class fc ; // object of type first_class: non-static member
};

#endif // SECOND_CLASS_H_INCLUDED_ 


Use second_class by including second_class.h
1
2
3
4
5
6
7
8
9
10
11
// main.cpp
#include <iostream>
#include "second_class.h"

int main()
{
    second_class object ;

    object.foo(100) ;
    std::cout << object.bar() << '\n' ;
}


Implementation of first_class in first_class.cpp
1
2
3
4
5
6
// first_class.cpp
#include "first_class.h" // include the header

int first_class::value() const { return val ; }

void first_class::value( int new_value ) { /* validate etc. */ val = new_value ; }


Implementation of second_class in second_class.cpp
1
2
3
4
5
6
7
8
9
10
// second_class.cpp
#include "second_class.h"

int second_class::bar() const { return fc.value() ; }

void second_class::foo( int a )
{
    const int newv = bar() + a ;
    fc.value( newv ) ;
}
Topic archived. No new replies allowed.