我是C 的新手,我很想知道你是否可以用相同的引數創建多個建構式。例如,我有這樣一個類,其中有病人,我有他們的名字和年齡。 我知道我可以創建一個這樣的建構式:
class hospital {
hospital(){
setname("John"/span>)。
setage(24)。
}
private:
字串名稱。
int age。
};
但是我是否可以像上面那樣創建另一個建構式。比如說:
hospital patientBilly{
setname("Billy") 。
setage(32)。
uj5u.com熱心網友回復:
問題在于,你重新定義了建構式。允許的定義只有一個。 簡化的例子:
void myFunc (){}。
void myFunc (){};
int
main ()
{
myFunc()。
}
我應該把Hospital類做成這樣:
#include <string>
struct Hospital // struct here to signal we have no invariant. You could also use a class and make the member public
{
std::string name{}; ///沒有setter和getter,如果你沒有不變式。
int age{};
};
int
main ()
{
auto hospital = Hospital{ .name = "John", .age = 42 }; /c 20 Designated Initializers so we can construct Hospital with a name and age without define a constructor
}
uj5u.com熱心網友回復:
我相信你目前只是有點困惑。所以,讓我們把事情整理一下吧......
一個類描述了物件是怎樣的。
一個類描述了物件應該如何表現。建構式是該描述的一部分,并且等同于它以后將創建的所有實體。你理解的第一步應該是。有一個單一的類和它的多個實體/物件。
。所以你寫一個單一的類,并為每個實體/物件提供不同的引數以獲得不同的物件。
例子:
class hospital {
public:
醫院(const std::string& name_, int age_ ) 。
name { name_ }, age{ age_ }{
}
void Print() const
{
std::cout << "Hospital" << name << ":" << age < < std::endl;
}
private:
std::string name;
int age。
};
int main()
{
hospital醫院1{ "John", 24 }。
hospital hospital2{ "Bill", 77 };
hospital1.Print()。
hospital2.Print()。
}
你也可以為你后來創建的每個物件創建一個不同的類,但我相信這絕不是你想做的,尤其是在你的C 職業生涯的開始!
如果你想創建某種實體串列,你可以將物件存盤在容器中,并按照你的意愿對容器進行操作。
int main()
{
std::vector< hospital > objs;
objs.emplace_back( "John", 24 ) 。
objs.emplace_back( "Bill", 77 ) 。
for ( const auto& hos: objs )
{
hos.Print()。
}
}
uj5u.com熱心網友回復:
如果我沒有理解錯的話,你需要的是定義兩個建構式
hospital( const std::string &name, int age )
{
setname( name );
setage( age );
}
hospital() : hospital( "John"/span>, 24 )
{
}
然后你就可以寫出宣告該類的一個物件了
hospital patientBilly( "Billy" , 32 )。
uj5u.com熱心網友回復:
在你的問題中,你有兩個概念,你想把它們混合起來。 醫院和病人。因此,將它們建模為兩個不同的類是有意義的。
這樣一來,你可以將病人建模為具有年齡和姓名的東西。 而醫院則是 "包含 "病人的東西。
給病人一個結構體,在那里你可以傳遞年齡和名字。 并給醫院一個或多個方法來添加病人。 在這個例子中,我向大家展示了如何向醫院添加病人的各種變體。
我還使用了無符號變數型別來表示永遠不能小于0的數字。我在代碼中經常使用const關鍵字來表示那些只能使用且不能改變的值。
#include <iostream>
#include <string>
#include <utility>
#include <vector>
//---------------------------------------------------------------------------------------------------------------------
class patient_t
{
public:
//你需要一個這樣的建構式。
patient_t(const unsigned int age, const std::string& name ) :
m_age{ age },
m_name{ name }
{
}
//用于觀察私有資料的getter函式。
const unsigned int& age() const noexcept
{
return m_age。
}
//觀察私有資料的getter函式
const std::string& name() const noexcept
{
return m_name。
}
private:
unsigned int m_age。
std::string m_name;
};
//a useful output function to have (will make code later shorter)
std::ostream& operator<<(std::ostream& os, const patient_t& patients)
{
os << "Patient : " << patient. name() << ", age : " << patient.age() < < std::endl;
return os。
}
//---------------------------------------------------------------------------------------------------------------------
class hospital_t
{
public:
void add_patient(const unsigned int age。const std: :string& name)
{
m_patients.emplace_back(age,name); // will call patient constructor with two parameters age and name and puts it into vector
}
void add_patient(const patient_t& patient)
{
m_patients.push_back(patient); //在向量中存盤一個病人的副本。
}
const auto& patients() const
{
return m_patients。
}
private:
std::vector<patient_t> m_patients。
};
//---------------------------------------------------------------------------------------------------------------------
int main()
{
hospital_t hospital。
patient_t billy{ 42, "Billy" }。
hospital.add_patient(billy)。
hospital.add_patient(31, "Jane") 。
for (const auto& patient : hospital.patients()
{
std::cout << 病人。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/310497.html
標籤:
上一篇:互相設定自變數
