I want to access and print the class pointer member variable from main()
without using any class member functions.
#include <iostream>
#include <string>
using namespace std;
class MyVector {
int* mem;
int size;
public:
MyVector();
MyVector(int n, int val);
~MyVector() { delete[]mem; }
// **I don't want to use this show() function**
void show(){
for (int i = 0; i < 10; i++)
{
cout << mem[i] << endl;
}
}
};
MyVector::MyVector() {
mem = new int[100];
size = 100;
for (int i = 0; i < size; i++)
{
mem[i] = 0;
}
}
MyVector::MyVector(int n, int val)
{
size = n;
for (int i = 0; i < size; i++)
{
mem[i] = val;
}
}
How do I modify the code to access it like mem[index]
in the main()
function?
int main()
{
MyVector mv;
mv.show();
}
- The pointer
mem
variable of the class should remain the same. - I want to print the
mem
variable without using theshow()
function. - I want to know how to modify the code to access the
mem[index]
form.
For example:
int main()
{
MyVector mv;
for (int i = 0; i < 5; i++)
{
cout << mem[index];
}
}