Thread: New to C++

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    30

    New to C++

    First of all I would like to say hello to everyone,

    I'm trying to learn C++. I have a pretty decent background in Java, and wanted to move over to C++.

    I was working on writing insertion sort in order to learn C++, and I keep getting complier errors.

    Code:
    #include<iostream>
    
    using namespace std;
    
    class Demo
    {
    public:
    	void insertionSort(int[]);
    
    private:
    	InsertionSort sort;
    
    };
    
    void Demo::insertionSort(int anArray[])
    {
    	Demo::sort::sort(anArray);
    	cout << sort::toString();
    }
    
    public int main()
    {
    	Demo demo;
    	int myArray [6] = {5,3,7,10,54,43};
    
    	demo.insertionSort(myArray);
    }
    and

    Code:
    class InsertionSort
    {
    public:
    	void sort(int[]);
    	String toString();
    private:
    	int[] myArray;
    };
    
    void InsertionSort::sort(int [] anArray)
    {
    	myArray = anArray;
    	for(int i = 1; i < myArray.length; i++)
    		{
    			int temp = myArray[i];
    			int k = i - 1;
    			while(k >= 0 && temp < myArray[k])
    			{
    				myArray[k + 1] = myArray[k];
    				k--;
    			}
    			myArray[k + 1] = temp;
    		}
    
    }
    
    String InsertionSort::toString()
    {
    	String tempStr = "";
    	for(int i = 0; i < myArray.length; i++)
    	{
    		temp += myArray[i] + " ";
    	}
    }
    I would like to say thank you for advance.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Could you post the compile errors as well?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, there are a lot of errors. There is no String (its std::string in header <string>, you can't use arrays like that (int [] myArray or int myArray[] for that matter). Arrays are not objects and don't have a length member (you'd have to pass the length separately).

    There is also no need to make everything a class. In this case, perhaps 2 free functions would be enough:
    Code:
    void InsertionSort(int array[], unsigned length);
    void PrintArray(int array[], unsigned length);
    
    //or to be more conforming to STL conventions (could be templated later):
    void InsertionSort(int* first, int* last);
    void PrintArray(int* first, int* last);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Get a good book, like Thinking in C++.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    30
    Is there an API like the one in Java that I could look at. I understand I don't need so many classes I just wanted to use multiple classes to understand how to use them in C++. Like I said I have a good amount of background in Java.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You really need to use a book. There are more than just small syntax differences between C++ and Java, and if you try to use Java mentality in your C++ program you will have troubles.

    cppreference.com is a decent online reference, and you can check library vendor documentation for more complete (but not official) API information. Dinkumware, SGI and Roguewave are all possible places to find that.

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    30
    Ok thank you, I will probably just look up tutorials and what not.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Thinking in C++ is freely available online. If you can't/won't pay for a book, at least use the free book. It will help a lot more than a tutorial.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by Daved View Post
    Thinking in C++ is freely available online. If you can't/won't pay for a book, at least use the free book. It will help a lot more than a tutorial.
    I don't quite understand why people seem to embrace learning without a good, highly recommended book and rather learn by the seat of their pants.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by medievalelks View Post
    I don't quite understand why people seem to embrace learning without a good, highly recommended book and rather learn by the seat of their pants.
    Because they're cheap.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    For the fun of it.

  12. #12
    Registered User
    Join Date
    May 2008
    Posts
    30
    No next year I'm taking a Software development class that is in c++. The class starts like you never programmed in c++. I just wanted a head start.

Popular pages Recent additions subscribe to a feed