第10章 对象和类——对象和类(四) 对象数组

本文章是作者根据史蒂芬·普拉达所著的《C++ Primer Plus》而整理出的读书笔记,如果您在浏览过程中发现了什么错误,烦请告知。另外,此书由浅入深,非常适合有C语言基础的人学习,感兴趣的朋友可以自行阅读此书籍。

对象数组

用户通常要创建一个类的多个对象。声明对象数组的方法与声明标准类型数组相同。 以上一篇文章最后实现的Student类为例,我们可以这么声明:
Student sts[4];

当程序创建未被显示初始化的类对象时,总是调用默认构造函数。上述声明要求,这个类要么没有显示地定义任何构造函数,要么定义了一个显示默认构造函数。每个元素(sts[0]、sts[1]地等)都是Student对象,可以使用Student方法:

sts[0].display();
const Student& tops = sts[1].getMaxObj(sts[2]);

可以用构造函数来初始化数组元素。

const int STUS = 4;
Student students[STUS] = {
  Student("00001", "xiaoming", 89, 86, 91),
  Student("00002", "xiaojun",  98, 83, 92),
  Student("00003", "xiaohong", 86, 82, 100),
  Student("00004", "xiaoha",   79, 81, 97)
};

如果类包含多个构造函数,则可以对不同地元素使用不同地构造函数。

const int STUS = 10;
Student students[STUS] = {
  Student("00001", "xiaoming", 89, 86, 91),
  Student(),
  Student("00004", "xiaoha",   79, 81, 97)
};

上述代码使用Student(const string &id, const string &name, int yuwen, int shuxue, int yingyu)来初始化students[0]和student[2],使用构造函数Student()初始化students[1],由于该声明只初始化了数组的部分元素,因此余下的7个元素将使用默认构造函数进行初始化。

初始化对象数组的方案是,首先使用默认构造函数创建数组元素,然后花括号中的构造函数将创建临时对象,然后将临时对象的内容复制到相应的元素中。因此,要创建类对象数组,则这个类必须有默认构造函数,

一个使用对象数组的例子:
类声明:

// student.hpp
#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <string>
using namespace std;
class Student{
private:
    string m_id;
    string m_name;
    int m_yuwen;
    int m_shuxue;
    int m_yingyu;
    int m_total;
    int m_avr;
    void set_total() { m_total = m_yuwen + m_shuxue + m_yingyu;}
    void set_avr() { m_avr = m_total/3;}
public:
    Student();
    Student(const string &id, const string &name, int yuwen = 0, int shuxue = 0, int yingyu = 0);
    ~Student();
    void display() const;
    const Student& getMaxObj(const Student& st) const;
};

#endif

类方法实现:

//student.cpp
#include <iostream>
#include <string>
#include "student.hpp"
using  namespace std;
Student::Student()
{
    m_id = "no id";
    m_name = "no name";
    m_yuwen = 0;
    m_shuxue = 0;
    m_yingyu = 0;
    m_total = 0;
    m_avr = 0;
}
Student::~Student()
{
}
Student::Student(const string &id, const string &name, int yuwen, int shuxue, int yingyu)
{
    m_id = id;
    m_name = name;
    m_yuwen = yuwen;
    m_shuxue = shuxue;
    m_yingyu = yingyu;
    set_total();
    set_avr();
}
void Student::display() const
{
   cout << m_id << " " << m_name << endl 
        << "yuwen: " << m_yuwen << endl 
        << "shuxue: " << m_shuxue << endl 
        << "yingyu: " << m_yingyu<< endl 
        << "total : " << m_total << endl
        << "avr : " << m_avr << endl; 
}
const Student& Student::getMaxObj(const Student& st) const
{
    if(st.m_total > m_total)
    {
      return st;
    }
    else
    {
      return *this;
    }
}

使用类:

//student_main.cpp
#include <iostream>
#include "student.hpp"
using namespace std;

int main()
{
  const int STUS = 4;
  Student students[STUS] = {
    Student("00001", "xiaoming", 89, 86, 91),
    Student("00002", "xiaojun",  98, 83, 92),
    Student("00003", "xiaohong", 86, 82, 100),
    Student("00004", "xiaoha",   79, 81, 97)
  };

  cout << "show all students  info: " <<endl;
  for (int i = 0; i < STUS; i++)
  {
    students[i].display();
  }
  
  cout << endl;
  
  const Student *top  = &students[0];
  for (int i = 1; i < STUS; i++)
  {
    top = &top->getMaxObj(students[i]);
  }

  cout << "show top student info: " <<endl;
  top->display(); 

  return 0;
}

返回结果如下:

show all students info:
00001 xiaoming
yuwen: 89
shuxue: 86
yingyu: 91
total : 266
avr : 88
00002 xiaojun
yuwen: 98
shuxue: 83
yingyu: 92
total : 273
avr : 91
00003 xiaohong
yuwen: 86
shuxue: 82
yingyu: 100
total : 268
avr : 89
00004 xiaoha
yuwen: 79
shuxue: 81
yingyu: 97
total : 257
avr : 85

show top student info:
00002 xiaojun
yuwen: 98
shuxue: 83
yingyu: 92
total : 273
avr : 91

我们创建了一个对象数组students,然后先针对每个元素调用display()方法,然后再通过getMaxobj()方法,获取总分最高的对象,再将其打印出来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值