带参数和比较的c ++类[关闭](c++ classes with parameters and comparisons [closed])

编程入门 行业动态 更新时间:2024-10-24 00:20:37
参数和比较的c ++类[关闭](c++ classes with parameters and comparisons [closed])

假设我有一类人类类型,我想创建一个名为jim.punch(billy)的方法函数; 我创造了吉姆,我创造了比利。 我在编写函数时如何引用jim? 让我们说任何回报都是基于他们的体重。 所以如果比利更大,会发生一些事情,如果吉姆更大,就会发生其他事情。 我只是不知道如何在函数中使用jim

#include <iostream> using namespace std; class dog { public: int age; int weight; int height; int punch(int); }; int jim::punch(x) { if (jim > x) <------------------ { return //something } else { return something } int main() { human jim; jim.age = 30"; jim.weight = 175; jim.height = 6; human billy; //etc jim.punch(billy); return 0; }

let's say i have a class of type human and i want to create a method function called jim.punch(billy); i created jim and i created billy. how do i refer to jim when i'm writing the function? let's say whatever returns is based on their weight. so if billy is bigger, something will happen and something else will happen if jim is bigger. i just don't know how to use jim in the function

#include <iostream> using namespace std; class dog { public: int age; int weight; int height; int punch(int); }; int jim::punch(x) { if (jim > x) <------------------ { return //something } else { return something } int main() { human jim; jim.age = 30"; jim.weight = 175; jim.height = 6; human billy; //etc jim.punch(billy); return 0; }

最满意答案

你应该真正关注一本好书(或者至少是一本好的在线教程); 你的问题显示出对非常基本的C ++概念的困惑。

然而,这里是对您的特定情况的回答(省略了大量细节和重要但不是针对特定情况的概念)。 human是一类。 类可以具有数据成员和成员函数。 可以存在类类型的许多实例 (或obejcts); 每个都有自己的数据成员值。

每个成员函数都可以访问调用它的实例的成员(数据和函数)。 它可以显式地(使用表示实例的指针的关键字)或隐式(仅命名数据成员)。

所以这里是你如何在代码中表达你的情况:

class human { //place data members such as age, height, weight here public: int punch(const human &target); }; int human::punch(const human &target) { std::cout << "PUNCH!"; if (weight > target.weight) //equivalent to this->weight > target.weight { return /* something which represents this object winning */ } else { return /* something which represents target object winning */ } //Note: you should handle == case as well } int main() { human jim, billy; jim.punch(billy); }

You should really follow a good book (or at least a good online tutorial); your question shows confusion about very basic C++ concepts.

Nevertheless, here's an answer to your particular situation (omitting loads of details and important-but-not-for-this-particular-case concepts). human is a class. Classes can have data members and member functions. Many instances (or obejcts) of a class type can exist; each of these has its own values of data members.

Each member function can access the members (data and functions) of the instance on which it was invoked. It can do so explicitly (using the keyword this which represents a pointer to the instance), or implicitly (just naming the data member).

So here's how you might express your situation in code:

class human { //place data members such as age, height, weight here public: int punch(const human &target); }; int human::punch(const human &target) { std::cout << "PUNCH!"; if (weight > target.weight) //equivalent to this->weight > target.weight { return /* something which represents this object winning */ } else { return /* something which represents target object winning */ } //Note: you should handle == case as well } int main() { human jim, billy; jim.punch(billy); }

更多推荐

本文发布于:2023-08-07 18:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465106.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   classes   parameters   comparisons   closed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!