2016-03-29 18 views
-1

Derslere başvurma konusunda yardıma ihtiyacım var. Ben C++ için oldukça yeni ve sınıf bir kişi listesi yapmak için ödev verildi. Hangi zaten oluşturduk ancak orijinal kişi listesi Şimdi önümüzdeki atama bir sınıfa adresi yapım oldu bu üye değişkenler Bir sınıfı başka bir C++ sınıfına nasıl referans gösterebilirim?

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

vardı. Şimdi adres için header ve .cpp dosyasını oluşturdum. Adres sınıfımın adres sınıfına nasıl yeniden yazılacağını bilmiyorum. Adres üstbilgisi ve .cpp sınıfı ve iletişim başlığı ve .cpp sınıfı için Tam Kodlarım. Hala anladım havn't çünkü benim İletişim sınıfa

Adres başlık kodunu Adresini başvurmak için gidiyorum nasıl benim ana programı içermiyordu:

#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 

Bu Adresi'dir. 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; 
} 

Tamam ve bu iletişime başlık dosyasıdır (orada Bildirim hala dize adresi f şu i sınıf) başvurmak anlamaya henüz e kadar bunu değiştirildi sığınak bir dize olarak adres kullanmış önceki kod rom:

#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 

Bu tekrar Contact.cpp dosya kodu (dize adresidir) geçicidir:

#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; 
} 
+2

Referans istemiyorsanız _'Adres & Addressreference; '_ ama düz bir üye değişkeni' Adres adresi; ' –

+0

Bu sadece test amaçlıydı Aslında bu durumda nasıl referece ile ilgili bir fikrim yok – Izine

+0

'' '' '' '' '' ('' '' '' '' '' '' '' '' '' '' (Tekil BTW’yi Not edin) –

cevap

1

aslında) sizin Contact sınıfında Address başvuru gerekmez (ve istemiyoruz.

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_) 
{} 

Çok daha anlamda çoğu durumda Contact durumlarda üzerinde Address örneklerini paylaşmak yapmaz:

Sadece Address bir gömülü üye değişken kullanın.

+0

Adres defterimdeki Adres sınıfına ihtiyaç duyacağımı düşündüm, çünkü ilk ve son adlarını girerek arama motorunu çalıştırmam gerekiyor. onların adresi. – Izine

+0

@Izine _ "... adresleri dahil olmak üzere tüm bilgilerini verir." _ "Adres" bilgisi bu şekilde yerleştirilir. –

+0

AHHHH çok teşekkür ederim – Izine

İlgili konular