0

the code asks to define all member funtions outside the nested classes where the comment is written , and i couldnt figure it out ..

this is the code

#include<iostream>
#include<string>
using namespace std;
class Person
{
private:
    string name;
    int Age;
    char Gender;
    string Job;
public:
    class Address
    {
    private:
        string country_Name;
        string Street_Name;
        int Unit_No;
    public:
        Address();
        void Set_Data(Person::Address &Obj);
        static void Display_Address(Person::Address Obj1);
    };
    Person();
    string Get_Name();
    static int Get_Age(Person p);
    char Get_Gender();
    static string Get_Job(Person p);
    Person& Set_Data_Person(Person::Address &Obj);
    static void Display_Person(Person p, Person::Address addr);
};

// WRITE HERE -- MEMBER FUNCTIONS DEFINITIONS
int main()
{
    Person P1;
    Person::Address add;
    P1= P1.Set_Data_Person(add);
    cout << "-------------------------- Display Info----------" << endl;
    P1.Display_Person(P1, add );
    return 0;
}

i tried defining the setters for address and person class and i dont know where to begin

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
QTZ
  • 7
  • You should run your code through an indentation tool before posting. Also, prefer to use spaces instead of tabs when posting your code. – Thomas Matthews Jan 23 '23 at 03:45
  • In C++ the functions of classes are commonly called member functions (not methods). You have to repeat the signature `void Person::Address::Set_Data(Person::Address &Obj); {}` with the class `Person::Address::` as prefix. – Sebastian Jan 23 '23 at 03:46
  • I highly recommend not shortening variable names. Your code would be clearer if you used `address` instead of `add`. Add usually has the mathematical meaning and may conflict with other function names. – Thomas Matthews Jan 23 '23 at 03:47
  • C++ does not allow non-static nested classes, unlike languages like java. All nested classes are implicitly static (in the java sense), so instances of the nested class are not associated with a specific instance of the enclosing class and thus can't directly access members of the enclosing class. – Chris Dodd Jan 23 '23 at 03:50

1 Answers1

2

You can call the resolution operator twice to access the inner-class methods:

#include <iostream>

class A {
public:
    class B {
    public:
        void hello();
    };

    void hello();
};

void A::hello() { std::cout << "Hello from A!\n"; }
void A::B::hello() { std::cout << "Hello from B!\n"; }

int main() {
    A a;
    A::B b;

    a.hello(); // out: Hello from A!
    b.hello(); // out: Hello from B!

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34