1. 把课上strcut的练习,尝试着改成class
代码
#include <iostream>
using namespace std;
class Stu
{
private:
int age;
char gender;
int height;
public:
float score;
int get_age();
char get_gender();
int get_height();
void set_age(int age);
void set_gender(char gender);
void set_height(int height);
};
int Stu::get_age()
{
return age;
}
char Stu::get_gender()
{
return gender;
}
int Stu::get_height()
{
return height;
}
void Stu::set_age(int age)
{
Stu::age = age;
}
void Stu::set_gender(char gender)
{
Stu::gender = gender;
}
void Stu::set_height(int height)
{
Stu::height = height;
}
int main()
{
Stu s1;
s1.set_age(18);
s1.set_gender('f');
s1.set_height(165);
s1.score = 99.0;
cout << "s1.age = " << s1.get_age() << endl;
cout << "s1.gender = " << s1.get_gender() << endl;
cout << "s1.height = " << s1.get_height() << endl;
return 0;
}
运行结果
2. 写一个有默认参数的函数,把声明和定义分开,并在主函数内成功调用
代码
#include <iostream>
using namespace std;
void speak(int count=1, string content="hello");
int main()
{
speak(1);
speak(3, "Hello, xiaoming");
return 0;
}
void speak(int count, string content)
{
for(int i=1; i<=count; i++)
{
cout << content << endl;
}
}
运行结果