标签:div 通过 oid 成员 int val ++ pause mic
1.
2.
#include <iostream> using namespace std; #include "arraylnt.h" int main() { // 定义动态整型数组对象a,包含2个元素,初始值为0 ArrayInt a(2); a.print(); // 定义动态整型数组对象b,包含3个元素,初始值为6 ArrayInt b(3, 6); b.print(); // 通过对象名和下标方式访问并修改对象元素 b[0] = 2; cout << b[0] << endl; b.print(); system("pause"); return 0; }
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int& operator [](int i); // 补足:将运算符[]重载为成员函数的声明 // ××× void print(); private: int *p; int size; }; #endif
#include "arraylnt.h" #include <iostream> #include <cstdlib> using std::cout; using std::endl; ArrayInt::ArrayInt(int n, int value): size(n) { p = new int[size]; if (p == nullptr) { cout << "fail to mallocate memory" << endl; exit(0); } for(int i=0; i<size; i++) p[i] = value; } ArrayInt::~ArrayInt() { delete[] p; } void ArrayInt::print() { for(int i=0; i<size; i++) cout << p[i] << " "; cout << endl; } int& ArrayInt::operator[](int i){ return p[i]; } // 补足:将运算符[]重载为成员函数的实现 // ×××
标签:div 通过 oid 成员 int val ++ pause mic
原文地址:https://www.cnblogs.com/changtingzao/p/10894843.html