一、
我的修改方式如下:
#include<iostream>
using namespace std;
class Time{
public:
void set_time(void);
void show_time(void);
private:
int hour;
int minute;
int sec;
};
Time t;
int main()
{
t.set_time();
t.show_time();
return 0;
}
void Time::set_time(void)
{
cin >> t.hour;
cin >> t.minute;
cin >> t.sec;
}
void Time::show_time(void)
{
cout<< t.hour << ":" << t.minute << ":" << t.sec << endl;
}
二、
#include<iostream>
using namespace std;
class Time{
public:
void set_time()
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void show_time()
{
cout<< hour << ":" << minute << ":" << sec << endl;
}
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1;
t1.set_time();
t1.show_time();
return 0;
}
三、
#include<iostream>
using namespace std;
class Time{
public:
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t;
t.set_time();
t.show_time();
return 0;
}
void Time::set_time()
{
cin >> hour;
cin >> minute;
cin >> sec;
}
void Time::show_time()
{
cout<< hour << ":" << minute << ":" << sec << endl;
}
四、在第2.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。
这个题最好在C++环境下调试运行
//student.h
#include<string>
using namespace std;
class Student{
public:
void display();
void set_value();
private:
int num;
string name;
char sex;
};
//student.cpp
#include<iostream>
#include "student.h"
void Student::display()
{
cout << "num:" << num << endl;
cout << "name:" << name <<endl;
cout << "sex:" << sex << endl;
}
void Student::set_value()
{
cout << "Please input num name and sex:"<< endl;
cin >> num >> name >> sex;
}
//main.cpp
#include<iostream>
#include "student.h"
using namespace std;
int main()
{
Student stud;
stud.set_value();
stud.display();
return 0;
}
部分运行结果如下:
五、
arraymax.h
class Array_max
{
public:
void set_value();
void max_value();
void show_value();
private:
int array[10];
int max;
};
arraymax.cpp
#include<iostream>
#include "arraymax.h"
using namespace std;
void Array_max::set_value()
{
int i;
for(i = 0; i < 10; i++)
cin >> array[i];
}
void Array_max::max_value()
{
int i;
max = array[0];
for(i = 1; i < 10; i++)
if(array[i] > max)
max = array[i];
}
void Array_max::show_value()
{
cout << "max=" << max << endl;
}
file1.cpp
#include<iostream>
#include "arraymax.h"
using namespace std;
int main()
{
Array_max a;
a.set_value();
a.max_value();
a.show_value();
return 0;
}
我的样例运行结果如下
六、
T6.h
#include<iostream>
using namespace std;
class Rectangle
{
public:
void set_value();
double volume();
private:
double length;
double width;
double height;
};
void Rectangle::set_value()
{
cout << "Please input length, width and height:"<<endl;
cin >> length >> width >> height;
}
double Rectangle::volume()
{
return length * width * height;
}
main6.cpp
#include<iostream>
#include "T6.h"
using namespace std;
int main()
{
Rectangle r[3];
for(int i = 0; i < 3; i++)
r[i].set_value();
cout << '\n';
for(int i = 0; i < 3; i++)
cout << "rectangle " << i + 1 <<"'s volueme is : " <<r[i].volume() << endl;
return 0;
}
最好将成员函数的定义和所属的类放在同一个文件里,这样编译运行效率更高
注意:编译时可能会遇到这样的报错:error: new types may not be defined in a return type,这就说明你的类class定义后花括号后面漏了分号
标准正确的class定义规范是:
class
{
};
我的样例运行结果如下: