我可以使用类来简化这些代码 [英] Can I use class to simplify these codes

查看:98
本文介绍了我可以使用类来简化这些代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在这里使用课程?

如果我想添加更多功能,例如周边区域,我发现代码太长了。

我是C ++的新手,甚至没有一周。

我想用类来简化代码,不成功。

这是没有上课的原始代码。

How can I make use of the class here?
I found that the code would be too long if I wanted to add more features such as perimeter in addition to the area.
I'm new to C++, not even a week.
I wanted to use class to simplify the code, unsuccessful.
This is the original code without class.

#include <iostream>
using namespace std;

void rectangle()
{
    float height = 0;
    float width = 0;

    cout<<"Rectangle"<<endl;
    cout << "Enter the height:" << endl;
    cin >> height;

    cout << "Enter the width:" << endl;
    cin >> width;

    int area = height * width;
    cout << "The area is " << area << "." << endl;
    
    main();
}

/* More shape functions */

int main()
{
	
	cout << "Choose Shape:" << endl;
	cout << "1. Rectangle" << endl;
        /* More shapes */
	cout << "8. Sector" << endl;
	cout << "0. QUIT" << endl;

    bool retry = false;
	while (retry)
	{
		char user_input = '0';
		cin >> user_input;
		switch (user_input)
		{
		case '1':
			rect_area();
			break;
		case '2':
			tri_area();
			break;
		/* more */
		case'8':
			sect_area();
			break;
		case'9':
			retry = true;
		default:
			exit(0);
		}
	}
}





我尝试过上课,但不成功。



I've tried using class, but unsuccessful.

class area
{
	public:
		void shape_height(float h) {
			float height = h;
		}

		float get_height() {
			return height;
		}
                void shape_width(float w) {
                       float width = w;
                }

                float get_width() {
                     return width;
                }
	private:
		float height;
		float width;
}

int main()
{
	area measure;
	measure.shape_height(12); // How to make user input their data here?

}





要在main()中调用类及其功能的内容是什么?



What to put in the main() to call the class and it's function?

推荐答案

我建​​议你查看一个好的 C ++ 教程(或者更好的是,一本书)。如果你真的不知道,你不能写 C ++ 代码。

你可以在这里找到一个很好的起点:\"C++语言 [ ^ ]。
I suggest you to check out a good C++ tutorial (or, better, a book). You cannot write C++ code if you really have no idea about.
You may find a good starting point here: "C++ Language"[^].


我想注意,类的目的和其他根本重要的语言和技术特征并不完全简化任何东西。他们创造了全新的方法。即使最终结果可以与一些非OOP解决方案进行比较;那么,OOP解决方案看起来更简单,更易于维护。但是,这不是重点。真正意义上它以更有效和可维护的方式解决更复杂的问题。换句话说,重点是避免使用差的解决方案,所以你不会在简单(顺便说一下,这是一个非常模糊的概念)中进行比较,以便在这里认真讨论它。



I want to note, the purpose of classes and other fundamentally important language and technology features is not exactly "simplifying" anything. They create fundamentally new approaches. Even though the final result can be compared with some non-OOP solution; then, the OOP solution can look much simpler and maintainable. However, this is not the point. The real point it to solve much more complex problems as yours in much more efficient and maintainable manner. In other words, the point is avoiding poor solutions at all, so you would not have a material for comparison in "simplicity" (which is, by the way, a very fuzzy notion, to discuss it seriously here).

一种不影响编程方式的语言,不值得知道。

A language that doesn't affect the way you think about programming, is not worth knowing.

最终,这对于OOP来说是完全正确的。它首先为您提供了新的机制,首先是基于虚拟方法的机制的动态调度,并且,由于它,后期绑定多态。请参阅:

https://en.wikipedia.org/wiki/Dynamic_dispatch [< a href =https://en.wikipedia.org/wiki/Dynamic_dispatchtarget =_ blanktitle =New Window> ^ ],

https://en.wikipedia.org/wiki/Virtual_function [ ^ ],

https ://en.wikipedia.org/wiki/Late_binding [ ^ ],

https://en.wikipedia.org/wiki/Polymorphism_ %28computer_science%29 [ ^ ] 。



如果您不使用它,您的课程可以几乎没有意义,一些代码构造最好的方式。有这么多开发人员使用许多类(有时是事件虚拟函数)而不了解他们的真实目的甚至是OOP的痕迹。没有动态调度机制(包括封装和继承)的一切都只是真正的OOP的先决条件。使用类只是为了使用类(以及一些其他语言功能)是一个常见的错误。



现在,我想提请你注意我认为你的代码中最糟糕的是什么。这是立即常量 1,2,8和9硬编码的行,如情况9:这是不可维护的。你至少可以使用一些 enum 声明。



每个开发人员都需要阅读这个,首先:

Peter Norvig,十年自学编程

http://norvig.com/21-days.html [ ^ ]。



-SA

Ultimately, this is perfectly true for OOP. It gives you principally new mechanism, first of all, dynamic dispatch based on the mechanism of virtual methods, and, thanks to it, late binding and polymorphism. Please see:
https://en.wikipedia.org/wiki/Dynamic_dispatch[^],
https://en.wikipedia.org/wiki/Virtual_function[^],
https://en.wikipedia.org/wiki/Late_binding[^],
https://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^].

If you don't use it, your "classes" could be almost pointless, some way of code structuring at best. There are so many developers who use many classes (and sometime event virtual functions) without understanding of their true purpose and even the trace of OOP. Everything without dynamic dispatch mechanism (including encapsulation and inheritance) is just prerequisite for real OOP. Using classes just for sake of using classes (as well of some other language features) is one of the common mistakes.

Now, I would like to bring your attention to what I think is the worst in your code. This is immediate constants 1, 2, 8 and 9 hard coded in lines like case 9: this is not maintainable. You could at least use some enum declaration.

And every developers needs to read this, first of all:
Peter Norvig, Teach Yourself Programming in Ten Years,
http://norvig.com/21-days.html[^].

—SA


这篇关于我可以使用类来简化这些代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆