1
我已經單獨實現並定義了方法。現在我不明白如何在Main.cpp文件中創建Parcel2類的對象/實例。我也寫在Main.cpp中Parcel2 :: Parcel2(2);但它的日誌說構造函數不能直接調用。請親引導我。如何在主函數中設置Parcel2類的對象
Parcel2.h
#ifndef PARCEL2_H
#define PARCEL2_H
class Parcel2
{
private:
// Declare data members
int id;
public:
// Constructor
Parcel2(int id);
// Setter function
void setID(int id);
// getter function
int getID();
protected:
};
#endif
Parcel2.cpp
#include "Parcel2.h"
// Defination of constructor
Parcel2::Parcel2(int id) {
this->id = id;
}
// Defination of setter
void Parcel2::setID(int id) {
this->id = id;
}
// Defination of getter
int Parcel2::getID() {
return id;
}
Main7.cpp
#include <iostream>
#include "Parcel2.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
// how to make object
}
感謝。我找到解決方案 –