如何将类引用到另一个类C ++中

时间:2016-03-29 22:26:27

标签: c++ class nested

我需要一些帮助引用类。我是C ++的新手,我的班级被赋予了一个联系人列表。我已经创建但原始的联系人列表有这些成员变量

string firstName;
string lastName;
string phones;
string emails;
string addresss;

现在接下来的任务是将地址变成一个类。现在我为地址创建了头文件和.cpp文件,我不知道如何将地址类重新配置到我的联系人类中这是地址头和.cpp类以及联系头和.cpp类的完整代码。我没有包括我的主程序,因为我还没弄明白我将如何将地址引用到我的Contact类

地址标题代码:

#ifndef ADDRESS_H
#define ADDRESS_H
#include <iostream>
#include <string>
using namespace std;

class Address
{
private:
string home2;
string street2;
string apt2;
string city2;
string state2;
string zip2;

public:

// Default constructor
// Initializes all variable to empty string
Address();

Address(string home, string street, string apt, string city, string state, string zip);

~Address(); // Deconstructor

// Accessor method for the home instance variable
string getHome() const;

// Accessor method for the street instance variable
string getStreet() const;

// Accessor method that returns apartment number
// if it is an apartment building, or "none" if
// it is a private house.
string getApt() const;

// Accessor method for the city instance variable
string getCity() const;

// Accessor method for the state instance variable
string getState() const;

// Accessor method for the zip instance variable
string getZip() const;

// Method that prints Address to console
void output() const;

// Method that solicits the information 
// Apartment will be set to "none" if it is a private house
// If it is an Apartment Building, method will solicit
// info about apartment 
void input();
};
#endif

这是地址.cpp:

#include "stdafx.h"
#include "Address.h"


Address::Address() {

}

Address::Address(string home, string street, string apt, string city, string       
state, string zip) {
home2 = home;
street2 = street;
apt2 = apt;
city2 = city;
state2 = state;
zip2 = zip;

}

Address::~Address() {

}

string Address::getApt() const {
return apt2;
}
string Address::getCity() const {
return street2;
}
string Address::getHome() const {
return home2;
}
string Address::getStreet() const
{
return string();
}
string Address::getState() const {
return state2;
}
string Address::getZip() const {
return zip2;
}

好的,这是联系人头文件(注意,仍然是我之前的代码中的字符串地址,我使用地址作为字符串我还没有改变它,直到我弄清楚如何引用类):

#include <iostream>
#include <string>
#include "Address.h"

using namespace std;

// Function declarations 
#ifndef Contacts_H
#define Contacts_H



class Contacts {
private: // access modifiers    
     // All my member variables
string firstName;
string lastName;
string phones;
string emails;
string addresss;
Address& Addressreference;

public:
Contacts(); // default constructor

Contacts(string, string, string, string, string, Address); // overload  constructor

Contacts(string firstname, string lastname, string phone, string email,    string address, Address & reference);

~Contacts(); // Destructor function

             // Accessor Functions
string getfirstName() const;
string getlastName() const;
string getphones() const;
string getemails() const;
string getadresss() const;


};
#endif

这是Contact.cpp文件代码(字符串地址也是临时的):

#include "stdafx.h"
#include "Contacts.h"

// Function definitions

Contacts::Contacts()
{


}

Contacts::Contacts(string, string, string, string, string, Address) 
{
**strong text**}


Contacts::Contacts(string firstname, string lastname, string phone, string       email, string address, Address& reference) // overload constructor
{
firstName = firstname;
lastName = lastname;
phones = phone;
emails = email;
addresss = address;
Addressreference = reference
}

Contacts::~Contacts() {

}
string Contacts::getfirstName() const
{
return firstName;
};

string Contacts::getlastName() const
{
return lastName;
};
string Contacts::getphones() const
{
return phones;
};
string Contacts::getemails() const {
return emails;
};
string Contacts::getadresss() const
{
return addresss;
}

1 个答案:

答案 0 :(得分:1)

您实际上不需要(并且不希望)Address课程中对Contact的引用。

您只需使用Address的嵌入式成员变量:

class Contact { // Note the singular term Contact. 
                // What you're designing isn't multiple contacts
    string firstName;
    string lastName;
    string phones;
    string emails;
    Address address; 
};
Contact::Contact
    ( string firstname_
    , string lastname_
    , string phones_
    , string emails_
    , const Address& address_) // overload constructor
: firstname(firstname_)
, lastname(lastname_)
, phones(phones_)
, emails(emails_)
, address(address_)
{}

在大多数情况下,在Address个实例上共享Contact个实例没有多大意义。