Beginner help with OOP C++?

closed account (iNhpfSEw)
So I need to write a code to create an array of 10 random integers and sort that array. Having done Java, I wanted to do Object Oriented Programming so I could do multiple instances if I desired. Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;

class ArrayProject 
{
    public:
    int sort ();
    int arrayValue;
    int array [10];
};

ArrayProject* myArray = new ArrayProject ();
     
/**
* Compares the value of two elements in myArray[]
* and sorts them in order by value
*/
void sort () 
{
    for(int y = 0; y < 10; y ++)
    {
        for(int j = 0; j < 10; j ++)
        {
            if (myArray[j+1].array > myArray[j].array)      
            { 
                int      temp           = myArray[j].arrayValue;            
                myArray[j].arrayValue   = myArray[j+1].arrayValue;
                myArray[j+1].arrayValue = temp;
            }            
        }
    }

    for(int t = 0; t < 10; t ++)
    {
        cout << myArray[t].arrayValue;
        cout << " ";
    }
    while(1);
}
    
/**
* The main () method starts everything
*/
int main ()
{
    for(int x = 0; x < 10; x ++)
    {
        myArray[x].arrayValue    = rand() % 100;
        
        cout << myArray[x].arrayValue;
        cout << " ";
    }
        
    cout << "  ";
    sort ();  // Runs the sort () method
}



When I do this, however, I get a big problem and I get some 10 or so digit number that wasnt there originally. How can I make a 'new' array so I can get different random numbers, as well as fix this problem with the sort???

There is no such operation as > for arrays. So this code in invalid

if (myArray[j+1].array > myArray[j].array)
myArray is a pointer to a single ArrayProject object (not an array). Not sure if you mean myArray->array[x]; instead of myArray[x].arrayValue.
You have to manually delete "myArray". There is no need for it to be a pointer anyway.

The program should look more like this:

Edit: I'm also going to use "this" to make things more clear (I think). Its the same as Java, but it is a pointer, needing "->"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class ArrayProject 
{
    public:
    int sort ();
    int array [10];
};

int ArrayProject::sort() // You must use scope resolution to use ArrayProject's sort
{
  for (int i = 0; i < 9; i++)
    for (int j = i + 1; j < 10; j++)
      if (this->array[i] > this->array[j])
         //swap
}

int main()
{
  ArrayProject *myArray = new ArrayProject();  // There's really no need to be a pointer.

  for (int i = 0; i < 10; i++)
    myArray->array[i] = randomNumber;

  // print contents of pre sorted array

  myArray->sort();

  // print contents of post sorted array


  delete myArray;  // You must do this yourself, no garbage collection in STD c++
  return 0;
}
Last edited on
closed account (iNhpfSEw)
To be honest, I'm not sure what I mean. This is the first time I've ever worked with C++ so I'm in the dark about 95% of it. So by the looks of it, it seems like I was on the right track, but I wasn't using a 'legal' pointer and needed -> instead? And just to move a few things around to make it easier?
Last edited on
Java you might have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ArrayProject
{
  private int [] array = new array[10];  // Because arrays are objects in Java
  
  public void sort()
  {
     // sort algoritm
  }
 
  public static void main(String args[])
  {
    ArrayProject me = new ArrayProject();  // Instantiate ArrayProject

   // Fill/Print Array

    me.sort();

    // Print sorted array

    return 0;
  }
}


The C++ equivalent is pretty much what I posted, except for the "new".


You don't need to make new objects, they are instantiated upon declaration:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>

using namespace std;

class Name
{
private:
  string myName;
  
public:

  Name(string n)
  {
    myName = n;
    cout << "Name Constructed with name: " << myName << '\n';
  }
  ~Name(void)  // The desctructor is called when the object goes out of scope
  {
    cout << myName << " Destructed\n";
  }

  void speak(void)
  {
    cout << myName << " says \"Hello\"\n";
  }
};

int main(void)
{
  cout << "Declaring and Instantiating John:\n";
  Name john("John");
  
  cout << "\declaring Mary\n";
  Name *mary;
  
  cout << "Instantiating Mary\n";
  mary = new Name("Mary");
  
  john.speak();
  mary->speak();

  std::cout << '\n';
  // Objects not going out of scope is a memory leak, and a problem
  // In order for Mary to go out of scope, you need this next line
  // You can comment it out to see the difference
  delete mary;
  
  // Note that Mary's destructor is called first.
  // John goes out of scope at the end of main()  
  return 0;
}
Last edited on
C++ isn't that much different from Java because C++ had such a huge influence on Java.

Some quick tips:

- learn how to use pointers

- replace Java's interface keyword with abstract base classes in C++

- remember to always use a "delete" when you use a "new"

- learn how to use the dynamic_cast to check for class types in a hierarchy

- try to understand exactly how polymorphism in C++ works (in Java it's automatic)

- learn when to use -> instead of .

Other than that it's pretty much identical save a few rare instances.
Topic archived. No new replies allowed.