A few questions on classes!..

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 4
Reputation: Natrius is an unknown quantity at this point 
Solved Threads: 0
Natrius Natrius is offline Offline
Newbie Poster

A few questions on classes!..

 
1
  #1
Sep 4th, 2006
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?

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int swap_values(vector<int>numbers);
  6.  
  7. int main ()
  8. {
  9. vector<int>numbers(3);
  10. cout << "Let's swap vector values\n";
  11. cout << "Enter value 1 " << endl;
  12. cin >> numbers[1];
  13. cout << "Enter value 2 " << endl;
  14. cin >> numbers[2];
  15.  
  16. cout << "vector 1: " << numbers[1] << endl;
  17. cout << "vector 2: " << numbers[2] << endl;
  18. cout << "vector 3: " << numbers[3] << endl;
  19. swap_values(numbers);
  20. cout << "vector 1: " << numbers[1] << endl;
  21. cout << "vector 2: " << numbers[2] << endl;
  22. cout << "vector 3: " << numbers[3] << endl;
  23. cin.get();
  24. cin.get();
  25. return 0;
  26. }
  27.  
  28.  
  29.  
  30. int swap_values(vector<int>numbers)
  31. {
  32.  
  33. numbers[3] = numbers[1];
  34. numbers[1] = numbers[2];
  35. numbers[2] = numbers[3];
  36. return 1;
  37. }

Thanks a lot for helping me! It means a lot! :cheesy:


/Simon
Last edited by WolfPack; Sep 4th, 2006 at 4:51 pm. Reason: Removed the link and embedded the code in to the post.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: A few questions on classes!..

 
1
  #2
Sep 4th, 2006
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?

ans: i am sorry but i dont understand what are u trying to convey here. Post the same question with some examples on how you are going to implement it.

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

ans: Just create the object a normal way but just keep in mind that that instance of the object will only be valid for that same scope and not beyond it.

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

ans: Objects and variables are by default sent by value. To send the object by reference you can do pass by reference using the C++ reference mechanism or the old C style pointer mechanism.

4. How do you make a function which swaps the values of two indexes on a vector (like swaps numbers[1] with numbers[2])?

ans: The ans to this lies in the prev question. To make function reflect teh changes done to the variables beyond the function scope you need to pass the variable using the pass by reference mechanism.

I would very much recommend you to get all your objected oriented and C++ basic concepts cleared before attempting something like vectors.

Here is ur updated code which i think will work now. Just look out for the changes i have made.

#include <iostream>
#include <vector>
using namespace std;

bool swap_values(vector<int>& numbers);

int main ()
{
     vector<int> numbers(2);
    cout << "Let's swap vector values\n";
    cout << "Enter value 1 " << endl;
    cin >> numbers[0];
    cout << "Enter value 2 " << endl;
    cin >> numbers[1];

   // indexing starts at 0 and not 1 so the first element is indexed by
  // numbers [0] and not numbers [1].
 
    cout << "vector 1: " << numbers[0] << endl;
    cout << "vector 2: " << numbers[1] << endl;
  
    swap_values(numbers);

    cout << "vector 1: " << numbers[0] << endl;
    cout << "vector 2: " << numbers[1] << endl;
  
   cin.get(); cin.get();

    return 0;
}

// btw what type of swap were u planning to implement using three values ???
// i thought u would want  a swap with 2 values and changed the program likewise.

bool swap_values(vector<int>& numbers)
{

// better make the function return bool instead of the old style "int"

    int temp = numbers[1] ;
    numbers[1] = numbers[0];
    numbers[0] = numbers[1];
    return true;
}

Hope it helped, bye.
Last edited by ~s.o.s~; Sep 4th, 2006 at 5:23 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: A few questions on classes!..

 
1
  #3
Sep 4th, 2006
Originally Posted by Natrius View Post
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?
I believe that's this kinda thing.
#include <iostream>

class T
{
   int a,b,c; // private data
public:
   T(int a_ = 0, int b_ = 0, int c_ = 0) : a(a_), b(b_), c(c_)
   {
      std::cout << "default constructor with default arguments:\n";
      std::cout << "a = " << a << ',';
      std::cout << "b = " << b << ',';
      std::cout << "c = " << c << '\n';
   }
};

int main(int argc, char *argv[])
{
   T t(1), u(1,2,3);
   return 0;
}

/* my output
default constructor with default arguments:
a = 1,b = 0,c = 0
default constructor with default arguments:
a = 1,b = 2,c = 3
*/
[edit]http://www.parashift.com/c++-faq-lit....html#faq-10.3
Last edited by Dave Sinkula; Sep 4th, 2006 at 5:38 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: A few questions on classes!..

 
1
  #4
Sep 4th, 2006
Originally Posted by Natrius View Post
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?
Lets take class A such that
  1. class A
  2. {
  3. int i;
  4. int j;
  5. }
A default constructor is a constructor that doesn't take any arguments.
e.g. A::A();
An overloaded constructor is a constructor that takes arguments.
e.g
  1. A::A(int int_i, int int_j )
  2. {
  3. i = int_i;
  4. j = int_j;
  5. };
you can use the values of int_i and int_j to assign values for class A's member variables i and j.
If you write the overloaded constructor like this,
  1. A::A(int int_i, int int_j = 0)
  2. {
  3. i = int_i;
  4. j = int_j;
  5. };
calling the constructor like A a(2), will create an object a with a.i = 2 and a.j = 0;
calling the constructor like A a(2,9), will create an object a with a.i = 2 and a.j = 9;
If you have a default constructor defined, don't make all the arguments of the overloaded constructor default arguments. That is because the compiler will not be able to tell the difference between the two constructors.

Originally Posted by Natrius View Post
2. How do you instance an object from a class in different scope?
I think you are asking about something like this.
Class B is defined in Class A.So B is defined inside the scope of A. To create an instance of B, you should do something like this.
#include <iostream>

class A
{
public:
    class B
    {
    public:
        int i;
    };
};

int main()
{
    A::B b;
    b.i = 9;
    std::cout << b.i << std::endl;
}
Originally Posted by Natrius View Post
3. How do you send an object "by value" to a function?
like this.
  1. class A
  2. {
  3. ...
  4. }
  5.  
  6. int some_function_that_takes_an_object_of_class_A( A a )
  7. {
  8. .....
  9. }
  10. int main()
  11. {
  12. A a;
  13. some_function_that_takes_an_object_of_class_A( a ); // sends a copy of a. the original a is not changed.
  14. }

~sos~ has answered question 4.
Last edited by WolfPack; Sep 4th, 2006 at 5:41 pm.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: Natrius is an unknown quantity at this point 
Solved Threads: 0
Natrius Natrius is offline Offline
Newbie Poster

Re: A few questions on classes!..

 
0
  #5
Sep 5th, 2006
First of all I'd like to say thanks for all your help! It's been really helpful! :cheesy:

Now I wonder another thing.. I'm making a program and I'm just testing different stuff.. What I want is to add a classvariable that counts the number of instances and that is used by the constructor.. I've made it like the following but only gets a strange number like 2923920 as a result.. What am I doing wrong?
Thanks again for helping a total newbie out!

I highlighted the code for the counter in blue.

Code:

#include <iostream>
using namespace std;
class Person
{
      public:
 
             string first_name;
             string last_name;
             string month;
             int year;
             int day;
 
 
             Person();
             Person(string first_name, string last_name);
             ~Person();
             void getinfo();
             void setinfo();
             void deleter();
             void count_instance();
 
             private:
             int *pointer; 
             int counter;
};
int main ()
{
Person enter;
Person pass;
Person testing();
int *pointer; 
pointer = new int;
enter.setinfo();
cout << "Test: The pointer is: " << *pointer << endl;
enter.getinfo();
enter.deleter();
cout << "Test: The pointer is: " << *pointer << endl; 
enter = Person(enter.first_name, enter.last_name);
 
 cin.get();
 cin.get();
 
 enter.count_instance();
 
  cin.get();
 cin.get();
    return 0;
}
void Person::setinfo()
{
    counter = 0;
    cout << "counter is: " << counter;
    cout << "Name1?" << endl;
    cin >> first_name;
    cout << endl << "Name2?" << endl;
    cin >> last_name;
    cout << endl << "Year?" << endl;
    cin >> year;
    cout << endl << "Month?" << endl;
    cin >> month;
    cout << endl << "Day?" << endl;
    cin >> day;
    }
 
    void Person::getinfo()
{
    cout << first_name << " " << last_name << " " << year << " " << month << " " << day;
    }
 
    void Person::count_instance()
    {
         cout << "counter is: " << counter; 
         }
 
void Person::deleter()
 {
                       delete [] pointer;
}
 
 Person::Person()
 {
                 counter = counter +1;
                 cout << "Def Constructor!" << endl;
                  }
 
 Person::Person(string first_name, string last_name)
 {
                  counter = counter +1;
                  cout << "Overloaded Constructor!" << endl; 
                  }
 
 
Person::~Person()
 {
                       cout << "DESTRUCTOR!" << endl;
}
Last edited by Natrius; Sep 5th, 2006 at 4:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: A few questions on classes!..

 
0
  #6
Sep 5th, 2006
Read the part called static members.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: A few questions on classes!..

 
0
  #7
Sep 5th, 2006
The classic and the best way to keep track of the instances created is using the static int counter; member variable in your class. The speciality of "static" variable is that for any number of instances of the class created they all have a common copy of the instance variable. So any changes made to the instance variable using one object will be refleceted when the variable is accessed using the next object.

Maybe something like

#include <iostream>
using namespace std;
class Person
{
      public:
 
             string first_name;
             string last_name;
             string month;
             int year;
             int day;
 
 
             Person();
             Person(string first_name, string last_name);
             ~Person();
             void getinfo();
             void setinfo();
             void deleter();
             void count_instance();

             static int getCount ( ); 
             static void incrCount ( ); 
 
      private:
             int *pointer; 
             static int counter;
};
int main ()
{
Person enter;
Person pass;
Person testing();
int *pointer; 
pointer = new int;
enter.setinfo();
cout << "Test: The pointer is: " << *pointer << endl;
enter.getinfo();
enter.deleter();
cout << "Test: The pointer is: " << *pointer << endl; 
enter = Person(enter.first_name, enter.last_name);
 
cout << "\nThe object count is" << Person::getCount(); // call to static function of class
 
 cin.get();
 return 0;
}


void Person::setinfo()
{
    counter = 0;
    cout << "counter is: " << counter;
    cout << "Name1?" << endl;
    cin >> first_name;
    cout << endl << "Name2?" << endl;
    cin >> last_name;
    cout << endl << "Year?" << endl;
    cin >> year;
    cout << endl << "Month?" << endl;
    cin >> month;
    cout << endl << "Day?" << endl;
    cin >> day;
    }
 
    void Person::getinfo()
{
    cout << first_name << " " << last_name << " " << year << " " << month << " " << day;
    }
 
    void Person::count_instance()
    {
         cout << "counter is: " << counter; 
         }
 
void Person::deleter()
 {
                       delete [] pointer;
}
 
 Person::Person()
 {
                 Person::incrCount ( );
                 cout << "Def Constructor!" << endl;
                  }
 
 Person::Person(string first_name, string last_name)
 {
                  Person::inctCount ( );
                  cout << "Overloaded Constructor!" << endl; 
                  }
 
 
Person::~Person()
 {
                       cout << "DESTRUCTOR!" << endl;
}

 static void incrCount ( )
{ 
  Person::count ++ ;
}

static int getCount ( )
{
 return Person::count ;
}
HOpe it helped, bye.
Last edited by ~s.o.s~; Sep 5th, 2006 at 4:48 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 4
Reputation: Natrius is an unknown quantity at this point 
Solved Threads: 0
Natrius Natrius is offline Offline
Newbie Poster

Re: A few questions on classes!..

 
0
  #8
Sep 6th, 2006
~s.o.s~ --> I tried running your modified program, but I got some errors when running it in Bloodshed Dev-C++.
This is what the log says:

In constructor `Person:erson(std::string, std::string)':
Line 89 `inctCount' is not a member of `Person'
In function `void incrCount()':
Line 101 `count' is not a member of `Person'
In function `int getCount()':
Line 106 `count' is not a member of `Person'

Any idea why it does this?

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: A few questions on classes!..

 
0
  #9
Sep 6th, 2006
There is a spelling mistake in the constructor.
Change this
Person::Person(string first_name, string last_name)
{
Person::inctCount ( );
cout << "Overloaded Constructor!" << endl; 
}
to this.
Person::Person(string first_name, string last_name)
{
Person::incrCount ( );
cout << "Overloaded Constructor!" << endl; 
}
Change these also
 static void incrCount ( )
{ 
  Person::counter ++ ;
}

static int getCount ( )
{
 return Person::counter ;
}
Last edited by WolfPack; Sep 6th, 2006 at 10:07 pm.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: A few questions on classes!..

 
0
  #10
Sep 7th, 2006
Originally Posted by Natrius View Post
~s.o.s~ --> I tried running your modified program, but I got some errors when running it in Bloodshed Dev-C++.
This is what the log says:

In constructor `Person:erson(std::string, std::string)':
Line 89 `inctCount' is not a member of `Person'
In function `void incrCount()':
Line 101 `count' is not a member of `Person'
In function `int getCount()':
Line 106 `count' is not a member of `Person'

Any idea why it does this?

Thanks!
Oh i am very sorry, it was a spelling mistake on my part.

Anyways as you can see Mod. Wolfpack has already corrected the mistake for me (thanks to him for that).

If any problem persists, dont hesitate to post again.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC