Member object of another class

Robert1428

I have two classes: Location and Adress. Adress contains a member named l1 which is of the type Location.

class Location
{
    double lat, lon;
    char *em;

    public:
        Location(int =0, int=0, const char* =NULL);
        ~Location();
        Location (const Location&);
        friend ostream& operator<< (ostream&, const Location &);
};

class Adress
{
    char *des;
    Location l1;
    char *country;

    public:
        Adress(char *,Location &, char *);
        virtual ~Adress();
        friend ostream& operator<< (ostream&, const Adress &);
};

Adress constructor:

Adress::Adress(char *des, Location &l1, char *country)
{
    if (des!=NULL)
    {
        this->des=new char [strlen (des)+1];
        strcpy (this->des, des);
    }
    if (country!=NULL)
    {
        this->country=new char [strlen (country)+1];
        strcpy (this->country, country);
    }

}

Location constructor:

Location::Location(int lat, int long, const char *em)
{
    this->lat=lat;
    this->lon=lon;
    if (emi!=NULL)
    {
        this->em=new char [strlen (em)+1];
        strcpy (this->em, em);
    }
}

What I want to do is when I call the constructor of the class Location in the main function for creating a new object to automatically call the constructor of the location class, something like: Address ("desc", l1 (43.23, 32.12, "south"), "country"). I have tried in many ways but none of them seems to work. Sorry for my mistakes, I'm a begginer.

PapaDiHatti

It seems you want to pass temporary location object to Address constructor so your parameterized constructor definition should be changed to accept const Location& and here is sample code for what you want to achieve :

class Location
{
    double lat, lon;
char *em;

    public:
        Location(int =0, int=0, const char* =NULL);
        ~Location();
        Location (const Location&);
        friend ostream& operator<< (ostream&, const Location &);



    protected:

    private:
};

class Adress

{
    char *des;
Location l1;
char *country;
    public:
        Adress(char *,const Location &, char *);
        virtual ~Adress();
        friend ostream& operator<< (ostream&, const Adress &);

    protected:

    private:
};

Adress::Adress(char *des, const Location &l1, char *country)
{
    if (des!=NULL)
    {
        this->des=new char [strlen (des)+1];
        strcpy (this->des, des);
    }
    if (country!=NULL)
    {
        this->country=new char [strlen (country)+1];
        strcpy (this->country, country);
    }

}
Location::Location(int lat, int lon, const char *em)
{
    this->lat=lat;
    this->lon=lon;
    if (em!=NULL)
    {
        this->em=new char [strlen (em)+1];
        strcpy (this->em, em);
    }
}
int main()
{
    Adress ("desc", Location (43.23, 32.12, "south"), "country");
    return 0;
}

Also you cannot call class constructor with object name so l1 should be changed to class name i.e Location

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use member function of another class without object and avoid static

From Dev

Object as private member of a class

From Dev

Add class member dynamically inside another member

From Dev

Constructing a class inside another class( a non static member reference must be relative to a specific object)

From Dev

Counter object as a class data member

From Dev

Counter object as a class data member

From Dev

QThread object as a member of worker class

From Dev

Using an interface class as member type in another class

From Dev

Member function of a class as friend to another class

From Dev

CPP: Class as private member in another class

From Dev

Creating a class instance as a member of another class

From Dev

accessing the member of a class of pointer array of another class

From Dev

Accessing a class' member variable from another class

From Dev

Class design to call member function of another class

From Dev

Pointer to object in another class?

From Dev

C++ Pass a member function to another class

From Dev

another initialize a static c++ class member

From Dev

call member function in another class c++

From Dev

How to check if a template is a class member of another template

From Dev

Calling member function of one class from another

From Dev

Android: passing member to method of another class as reference?

From Dev

Declare and use a pointer to member function in another class

From Dev

call member function in another class c++

From Dev

Member initialization in constructor when the member has another class as its type

From Dev

How to declare a member of type tied to the member of another class?

From Dev

Initializing a member array's size with another member within the class

From Dev

How do i get a class member function to have access to another class member function's private member?

From Dev

Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

From Dev

Modify a member variable outside the class object and have changes in the class object

Related Related

  1. 1

    Use member function of another class without object and avoid static

  2. 2

    Object as private member of a class

  3. 3

    Add class member dynamically inside another member

  4. 4

    Constructing a class inside another class( a non static member reference must be relative to a specific object)

  5. 5

    Counter object as a class data member

  6. 6

    Counter object as a class data member

  7. 7

    QThread object as a member of worker class

  8. 8

    Using an interface class as member type in another class

  9. 9

    Member function of a class as friend to another class

  10. 10

    CPP: Class as private member in another class

  11. 11

    Creating a class instance as a member of another class

  12. 12

    accessing the member of a class of pointer array of another class

  13. 13

    Accessing a class' member variable from another class

  14. 14

    Class design to call member function of another class

  15. 15

    Pointer to object in another class?

  16. 16

    C++ Pass a member function to another class

  17. 17

    another initialize a static c++ class member

  18. 18

    call member function in another class c++

  19. 19

    How to check if a template is a class member of another template

  20. 20

    Calling member function of one class from another

  21. 21

    Android: passing member to method of another class as reference?

  22. 22

    Declare and use a pointer to member function in another class

  23. 23

    call member function in another class c++

  24. 24

    Member initialization in constructor when the member has another class as its type

  25. 25

    How to declare a member of type tied to the member of another class?

  26. 26

    Initializing a member array's size with another member within the class

  27. 27

    How do i get a class member function to have access to another class member function's private member?

  28. 28

    Invalid use of non-static member function - Class Member Function Calling Another Class Member Function

  29. 29

    Modify a member variable outside the class object and have changes in the class object

HotTag

Archive