p120 7
ref:https://blog.csdn.net/weixin_42597789/article/details/123329323
#include<iostream>
using namespace std;
class Student{
public:
Student(int n, float s):num(n), score(s){}
void change(int n, float s)const {
num = n;
score = s;
}
private:
mutable int num;
mutable float score;
};
int main(){
const Student s1(10,78.5);
s1.change(100,100.1);
return 1;
}
改成指针的方式:
const Student s1(10,78.5);
s1.change(100,100.1);
Student stud(101,78.5);
Student *p = &stud;
p->change(1,2);
// 在界面上输入数字的月份(1-12),输出对应月份的英文单词的全称,如果输入的数字不合法,给出提示。
#include<iostream>
#include<string>
#include <cstring>
using namespace std;
int main(){
int month;
cout << "请输入月份:"; cin >> month;
cout << "month=" << month << endl;
if (month == 1){
cout << "January \n";
}else if(month == 2){
cout << "February \n";
}else if(month == 3){
cout << "March \n";
}else if(month == 4){
cout << "April \n";
}else if(month == 5){
cout << "May \n";
}else if(month == 6){
cout << "June \n";
}else if(month == 7){
cout << "July \n";
}else if(month == 8){
cout << " August\n";
}else if(month == 9){
cout << " September\n";
}else if (month == 10)
cout << "October\n";
else if (month == 11)
cout << "November\n";
else if (month == 12)
cout << "December\n";
else
cout << "输入的数字不正确。\n";
switch (month)
{
case 1:
cout << "January \n";
break;
case 2:
cout << "February \n";
break;
case 3:
cout << "March \n";
break;
case 4:
cout << "April \n";
break;
case 5:
cout << "May \n";
break;
case 6:
cout << "June \n";
break;
case 7:
cout << "July \n";
break;
case 8:
cout << "August \n";
break;
case 9:
cout << "October \n";
break;
case 10:
cout << "September\n";
break;
case 11:
cout << "November \n";
break;
case 12:
cout << "December \n";
break;
default:
cout << "输入的数字不正确。\n";
break;
}
// 方式一:必须指定第二维的大小,且应大于等于数组最长字符串的长度
char montharr[][10] = { "January","February","March","April","May",
"June","July","August","September","October","November","December" };
string montharr1[12] = { "January","February","March","April","May",
"June","July","August","September","October","November","December" };
if (month>=1 && month<=12)
cout << "======111" << montharr[month-1] << endl;
else
cout << "输入的数字不正确。\n";
if (month>=1 && month<=12){
cout << "======222" << montharr1[month-1] << endl;
}else{
cout << "输入的数字不正确。\n";
}
char *str_arr[] = { "January","February","March","April","May",
"June","July","August","September","October","November","December" };
int size = sizeof(str_arr)/sizeof(str_arr[0]); // 获取数组大小
printf("000000000-----------------%s\n", *(str_arr+month-1));
return 1;
}
几种遍历字符串数组的方式!!!