answersLogoWhite

0


Best Answer

A array within a class in C++ is no different than any other type of attribute in a class...

class myclass {

private:

int someInteger;

int somArray[10];

int *somePointer;

public:

myclass() { ... constructor ...}

myclass(const &myclass()) { ... copy constructor ...}

~myclass() { ... destructor ...}

mymethods() { ... methods, etc. ...}

private:

myhelpers() {... helpers, etc. ...}

}

Recall that arrays and pointers are tightly related. Well, in classes, also recall that their memory must be explicitly handled or you are going to have all sorts of problems. You need constructors and destructors, as well as copy constructors, to properly make deep copies and deallocations if arrays area in the form of pointers.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

10y ago

The following example uses a template class to contain an array of any type. Since you specifically asked about C++ code, the array is actually a vector (a dynamic array implemented as an object) as this greatly simplifies the code.

The array is initialised at the point of instantiation and is initially empty. The class has an append() member method that allows us to input new elements to the end of the array, while the array subscript operator ([]) is overloaded to allow us to output the individual elements from the array, just as if myArray were an actual array. If you do not wish consumers to modify elements within your array, remove the non-const version of the array subscript operator overload.

The main() function demonstrates how myArray objects can be used to store both integers and strings from literal constants.

#include<iostream>

#include<vector>

template<class T>

class myArray

{

public:

void add(T t){ m_data.push_back( t ); }

const T& operator[](const int element )const { return( m_data[element] ); }

T& operator[](const int element ) { return( m_data[element] ); }

private:

std::vector<T> m_data;

};

int main()

{

myArray<int> a;

a.add(1);

a.add(2);

std::cout<<a[0]<<" "<<a[1]<<std::endl;

myArray<char*> b;

b.add("String 1");

b.add("String 2");

std::cout<<b[0]<<" "<<b[1]<<std::endl;

return(0);

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in c plus plus to initialize the array in an class and then to input the data in it in one member function and output the data by using another member function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering
Related questions

What function will be called by default to initialize the member variables of the object of the class?

The constructor. It's run each time a new object is created, usually setup to initialize member variables, but it can do most anything.


Is object a member of a class?

Yes, you would need to define your variables. Also initialize them


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


What is the meaning of x.getdata in c plus?

it mens u r calling de member function of de program


How the member function can be undefined?

You cannot undefine a member function.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


What does a Member's Helper do in WIT?

Their are really no such thing as a members helper, but their is a such thing as a member of the WIT program. Another thing if you are looking for a helper you can ask our Mentor team.


What is member fusion in c plus plus?

If you are asking about member functions. When we declare a function inside a class then that function becomes member function of that class and this function can access the whole class


Can you use the same function name for a member functoin of a class and an outside function in the same program file?

Yes you can use the same function name for a member function and an external function. They are primarily distinguished by the number and type of arguments they accept (the function signature). If they match exactly, then the scope resolution operator (::) is used to differentiate them by namespace. The class namespace is the class name itself. The external function uses global scope unless scoped to another namespace. When the scope is not explicitly stated, then the scope is implied by the call site. Note that whenever there is any ambiguity about which function is implied, the compiler will emit an error indicating where the ambiguity lies, and the program will ultimately fail to compile.


Who was the first member of the Vandal Patrol Program?

The first member of the Vandal Patrol Program was An8thg, the Vandal Patrol Program Coordinator.


In the ordered pair x y the value of y is a member of the?

x is a member of the function's domain, y is a member of the function's range.


What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.