C++面向对象程序设计第二章部分课后习题以及关于error: new types may not be defined in a return type的解决

一、

我的修改方式如下:

#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
{
};

我的样例运行结果如下:
在这里插入图片描述

之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值