首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用面向对象技术实现功能

如何利用面向对象技术实现功能
EN

Stack Overflow用户
提问于 2021-04-20 15:03:27
回答 2查看 68关注 0票数 0

有人能帮我用OOP做些什么吗?如何利用面向对象的方法实现学生平均身高的求取功能?

代码语言:javascript
复制
class Person
{
    //data members
    private:
        string name;
        int age;
        int height;
        float weight;
 
    //member functions
    public:
        void setPerson(string n, int a, int h, float w)
        {
            name=n;
            age=a;
            height=h;
            weight=w;
        }
        void setName(string n) { name=n; }
        void setAge(int a) { age=a; }
        void setHeight(int h) { height=h; }
        void setWeight(int w) { weight=w; }
 
        string getName() {return name;}
        int getAge() {return age;}
        int getHeight() {return height;}
        float getWeight() {return weight;}
 
};
 
int main()
{
    Person p[100];
    int x;
    cin >> x;
    string name;
    int age;
    int height;
    float weight;
 
    for(int i=0; i<x; i++)
    {
        cin >> name >> age >> height >> weight;
        p[i].setName(name);
        p[i].setAge(age);
        p[i].setHeight(height);
        p[i].setWeight(weight);
    }

    ..............

我的意见如下:

3(人数) jason 20 185 70.50 (姓名、年龄、身高、体重)艾玛19 165 55.55 yerry 25 164 65.10

产出:

171.33

EN

回答 2

Stack Overflow用户

发布于 2021-04-20 15:41:40

一旦你记录了你的Person,这只是一个计算平均身高的问题。它不应该是Person的成员函数,因为类的目的是表示单个Person。平均高度不是对单个Person所采取的属性或操作,因此我们只在main()中计算它。

我注意到了我对您的代码所做的更改。

代码语言:javascript
复制
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

class Person {
  // data members
 private:
  std::string name;
  int age;
  int height;
  float weight;

  // member functions
 public:
  // CHANGE: Change function to constructor
  Person(std::string n, int a, int h, float w)
      : name(n), age(a), height(h), weight(w) {}
  void setName(std::string n) { name = n; }
  void setAge(int a) { age = a; }
  void setHeight(int h) { height = h; }
  void setWeight(int w) { weight = w; }

  std::string getName() { return name; }
  int getAge() { return age; }
  int getHeight() { return height; }
  float getWeight() { return weight; }
};

int main() {
  std::vector<Person> p;  // CHANGE: Switch from C-array to std::vector
  int x;
  std::cin >> x;
  std::string name;
  int age;
  int height;
  float weight;

  for (int i = 0; i < x; i++) {
    std::cin >> name >> age >> height >> weight;
    p.emplace_back(name, age, height,
                   weight);  // CHANGE: Take advantage of vector functionality
                             // to build Person directly into vector
  }

  // ADDITION: Calculating average height, std::for_each is subjective here,
  // this could have easily been a range-based for loop.
  double avgHeight = 0.0;
  std::for_each(p.begin(), p.end(), [&avgHeight, size = p.size()](auto person) {
    avgHeight += static_cast<double>(person.getHeight()) / size;
  });

  std::cout << std::fixed << std::setprecision(2) << avgHeight << '\n';
}

输出:

代码语言:javascript
复制
171.33

std::for_each看上去可能很傻,但它实际上只是把高度相加,除以Person的个数,就像我提到的,它也可能是一个基于范围的循环。在这种情况下,我更喜欢基于范围的for循环;我只有std::for_each(),因为它比我强迫std::reduce工作的失败尝试要快得多。

代码语言:javascript
复制
double avgHeight = 0.0;
for (auto i : p) {
    avgHeight += static_cast<double>(i.getHeight()) / p.size();
}

i的类型最好指定为const auto&,但是您的getter没有标记为const

票数 0
EN

Stack Overflow用户

发布于 2021-04-20 16:19:21

您可以为Person添加一个父结构(称为个人结构或其他结构),该结构将向量指针作为成员变量。例如,std::shared_ptr<std::vector<double> > Students ( new std::vector<double>() );这个结构将拥有作为纯虚拟函数的Person拥有的所有公共函数。

然后,添加一个新的虚空函数,使学生脱脂,并向后推新学生的身高。接下来,添加另一个函数(返回一个双或浮点数),该函数循环遍历学生,获取学生身高的总和,并将其除以向量的大小()。

所有的Person对象都有指向单个学生向量的指针,所以每次你推回一个新学生的高度时,它都会到达相同的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67181523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档