A few questions on classes!..

Discussion in 'C++' started by Natrius, Sep 10, 2006.

  1. Natrius

    Natrius New Member

    Joined:
    Sep 10, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello!
    I just recently registred at this forum and I must say it seems to be a nice and friendly place!
    I'm currently studying a beginners course in C++ and I'm a bit stuck right now.. So I thought perhaps it might be a good idea to ask soem questions here and see if any of you guys could help me out!

    My problems are the following..

    1. I'm making a program with a default constructor and an overloaded constructor. They're both called by different rules and produces a testline of text when called. The assignment is to give the overloaded constructor default arguments to see what happens. I wonder, what is a default argument i an overloaded constructor?

    2. How do you instance an object from a class in different scope?

    3. How do you send an object "by value" to a function?

    4. Here's a "Non class" question.
    How do you make a function which swaps the values of two indexes on a vector (like swaps numbers[1] with numbers[2])?
    I've tried to make this but it only works when showing the results of the swap in the function, not when I show the results in the main function. When doing that it remains unchanged.
    Here's the code I've used. What am I doing wrong?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int swap_values(vector<int>numbers);
    
    int main ()
    {
         vector<int>numbers(3);
        cout << "Let's swap vector values\n";
        cout << "Enter value 1 " << endl;
        cin >> numbers[1];
        cout << "Enter value 2 " << endl;
        cin >> numbers[2];
    
        cout << "vector 1: " << numbers[1] << endl;
        cout << "vector 2: " << numbers[2] << endl;
        cout << "vector 3: " << numbers[3] << endl;
        swap_values(numbers);
        cout << "vector 1: " << numbers[1] << endl;
        cout << "vector 2: " << numbers[2] << endl;
        cout << "vector 3: " << numbers[3] << endl;      
        cin.get();
        cin.get();
        return 0;
    }
    
    
    
    int swap_values(vector<int>numbers)
    {
        
        numbers[3] = numbers[1];
        numbers[1] = numbers[2];
        numbers[2] = numbers[3];
        return 1;
    }
    
    Thanks a lot for helping me! It means a lot!


    Simon
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice