这是我在这个网站的第一个问题..引用我的朋友,,他从这里学到很多;),现在它的'我轮到我想.. :)处理指向私有类中的数组的指针。
尊敬的尊敬的成员..在这一点上,我我完全紧张,很沮丧地发现,我该如何处理我的代码才能工作..! 嗯,我是一名具有C++基础知识的学生,在本学期我们手中的面向对象编程。 这个任务是为了阐明类(私有和公共)的概念,此外,初始化,排序数组,寻找密钥等
我知道,当我们使用动态内存,我们必须编写构造函数,析构函数等,&我对此感到疑惑..我认为问题在参数或复制构造函数,深,浅拷贝。 我不知道它应该做什么! 嗯,我不是专业的先生......所以我很抱歉。 !
如果有人指导我用我的代码,关于数组,指针类的小例子,我该如何处理。 。 。 :(:(这是非常赞赏的先生。
// Constructor.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
class IntArray
{
private:
int size;
int *values;
public:
void InputData() const;
void OutputData() const;
void Search() const;
void Bubble() const;
IntArray(int size)
{
values = new int[size];
}
};
void IntArray::InputData() const
{
cout << "==> Entering Data <==" << endl;
for (int i = 0; i < size; ++i)
{
cout << ">> Enter Element #" << i+1 << ": ";
cin >> values[i];
}
}
void IntArray::OutputData() const
{
cout << "==> Printing Data <==" << endl;
for (int i = 0; i < size; ++i)
{
cout << values[i] << " ,";
}
cout << "\n";
}
void IntArray::Search() const
{
int key;
cout << "Please Enter A KEY To Find: ";
cin >> key;
int index = 0;
bool found = false;
for (int i = 0; i < size; ++i)
{
if ((!found) && (key == values[i]))
{
index = i;
found = true;
}
}
if (found)
{
cout << "KEY Found At The Index: " << index+1 << endl;
}
else
{
cout << "KEY is not present." << endl;
}
cout << '\n' << endl;
}
void IntArray::Bubble() const
{
for (int i = 1; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
if (values[j] > values[i])
{
int temp = values[j];
values[j] = values[i];
values[i] = temp;
}
}
}
}
int main()
{
const int size = 5;
IntArray u(size);
u.InputData();
u.OutputData();
u.Search();
u.Bubble();
system("pause");
return 0;
}
什么是您确切的问题?这不是一般性问题的网站,而是针对有针对性的问答。 – eriknelson 2013-04-30 18:30:34
是的兄弟。这段代码从来没有给我任何输出..?! 我不知道该怎么办.. – usman 2013-04-30 18:34:26
@ user2337036根本没有输出? – 2013-04-30 18:36:20