我的一项 C++ 家庭作业有问题。我们正在使用一个类来打印电话号码列表,但是我无法弄清楚如何获取 number 变量来打印我们应该与构造函数一起使用的默认值。
如果你们中的任何人都可以通过示例或类似的方式为我指明正确的方向,我将不胜感激!
#include <iostream>
#include <string>
class PhoneNumber { //This class will help the user make a list of phone numbers)
private:
int countryCode; //Stores the Country Code e.g. +1
int areaCode; //Stores Area Code e.g. (925)
int number; //Stores Number e.g. 754-6978
char type; //Stores the "type" of number. e.g. 'H' for home, 'B' for business, 'C' for cell.
int year; //Stores the year the number was added, for some odd reason.
public:
PhoneNumber(); //Empty constructor
PhoneNumber(int ccode, int acode, int num, char line, int year);
PhoneNumber(int num, char line = 'B');
PhoneNumber(int acode, int num, char line = 'C');
void setCountry(int ccode);
void setArea(int acode);
void setNumber(int num);
void setType(char line);
void setYear(int yr);
int getCountry() const;
int getArea() const;
int getNumber() const;
char getType() const;
int getYear() const;
bool doubleDigits() const;
void printNumber() const;
void printPhoneNumberStats() const;
};
//The Default Constructor
PhoneNumber::PhoneNumber() {
void setCountry(int ccode = 43);
void setArea(int acode = 800);
void setNumber(int num = 8675309);
void setType(char line = 'H');
void setYear(int year = 1981);
}
//Fill out the Phone Number class
PhoneNumber::PhoneNumber(int ccode, int acode, int num, char line, int year) {
}
PhoneNumber::PhoneNumber(int num, char line) {
}
PhoneNumber::PhoneNumber (int acode, int num, char line) {
}
int PhoneNumber::getCountry() const {
return countryCode;
}
int PhoneNumber::getArea() const {
return areaCode;
}
int PhoneNumber::getNumber() const {
return number;
}
char PhoneNumber::getType() const {
return type;
}
int PhoneNumber::getYear() const {
return year;
}
bool PhoneNumber::doubleDigits() const {
}
void PhoneNumber::printNumber() const {
std::cout << number << std::endl;
}
void PhoneNumber::printPhoneNumberStats() const {
}
这些都是我的功能。然后是我们需要使用的 int main()。
int main() {
PhoneNumber firstNum;
PhoneNumber secondNum(39, 415, 867666, 'B', 2012);
PhoneNumber thirdNum(1212121);
PhoneNumber fourthNum(1234566, 'C');
PhoneNumber fifthNum(925, 4392181);
PhoneNumber sixthNum(925, 5512346, 'H');
firstNum.printNumber();
/* firstNum.printPhoneNumberStats();
secondNum.printNumber();
secondNum.printPhoneNumberStats();
thirdNum.printNumber();
thirdNum.printPhoneNumberStats();
fourthNum.printNumber();
fourthNum.printPhoneNumberStats();
fifthNum.printNumber();
fifthNum.printPhoneNumberStats();
sixthNum.printNumber();
sixthNum.printPhoneNumberStats();
*/
return 0;
}
如果您能解释为什么我在尝试打印number变量时会出现垃圾,以及如何解决它,我将不胜感激。当然,任何其他建议也将不胜感激。
谢谢!凯特琳