[]のリロード練習&配列パッケージ
2878 ワード
Marry.hコードは
Marry.cppコードは
main.cppコードは
#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include
#include
using namespace std;
class m_arry
{
private:
int *p_addr; //
int m_size; //
int m_capacity; //
public:
m_arry ();
m_arry(int v_capacity);
m_arry(const m_arry& v_arry); //
~m_arry();
//
void push_back(int val);
//
void set_data(int index,int val);
//
int get_data(int index) const;
//
int get_capacity() const;
//
int get_size() const;
//
void show_element() const;
int& operator[](int index);
};
Marry.cppコードは
#include "MyArry.h"
//
m_arry::m_arry ()
{
cout<m_size = 0;
this->m_capacity = 100;
this->p_addr = new int[this->m_capacity ];
}
//
m_arry::m_arry (int v_capacity)
{
cout<m_size = 0;
this->m_capacity = v_capacity;
this->p_addr = new int[this->m_capacity];
cout<< "this->p_addr " <p_addr<p_addr = new int[v_arry.m_capacity];
this->m_size = v_arry.m_size;
this->m_capacity = v_arry.m_capacity;
for (int i=0;ip_addr[i] = v_arry.p_addr[i];
}
}
m_arry::~m_arry()
{
cout<p_addr != NULL)
{
delete[] this->p_addr;
this->p_addr = NULL;
}
}
//
void m_arry::push_back(int val)
{
if (this->m_size > this->m_capacity)
{
cout<p_addr+this->m_size) = val;
this->m_size++;
}
//
void m_arry::set_data(int v_index,int val)
{
if (v_index > this->m_size)
{
cout<p_addr[v_index]= val;
}
//
int m_arry::get_data(int index) const
{
return this->p_addr[index];
}
//
int m_arry::get_capacity() const
{
return this->m_capacity;
}
//
int m_arry::get_size() const
{
return this->m_size;
}
//
void m_arry::show_element() const
{
for (int i=0;im_size;i++)
{
cout<p_addr[i]<p_addr[index];
}
main.cppコードは
#define _CRT_SECURE_NO_WARNINGS
#include "MyArry.h"
void test_01(void)
{
/*m_arry arr1(30);
arr1.push_back(1);
arr1.push_back(5);
arr1.push_back(10);
arr1.set_data(2,100);
cout<push_back(10);
arr1->push_back(20);
arr1->push_back(30);
arr1->push_back(1000);
(*arr1)[2]=50;
(*arr1).operator[](2)=60;
arr1->show_element();
//m_arry * arr2 = new m_arry(*arr1);
//m_arry arr3 = *arr1;
/*cout<< "arr1 " <