CFanatic

Go Back   CFanatic > Programming > C++ Programming

Join CFanatic Forum Now

A few questions on classes!.. topic posted under C++ Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-10-2006, 10:20 AM
Junior Member
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Natrius is on a distinguished road
| More
A few questions on classes!..

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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Miscellaneous Newbie questions codeunlock C# Programming 1 08-13-2010 12:18 PM
Couple of Questions NameGoesHere C++ Programming 5 12-30-2008 12:45 AM
create 3 classes clinic, pharmacy, surgery dev1910 C++ Programming 0 12-08-2007 12:17 PM
operator+ in derived classes More options JimLangston C++ Programming 0 03-30-2007 01:17 AM
Help with program using classes chaching C++ Programming 0 09-30-2006 04:52 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 01:45 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board