944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8301
  • C++ RSS
Nov 19th, 2006
0

Newbie Constructor / Array question

Expand Post »
This is my first post - Hello everybody, hopefully you can help get on straight and narrow with regards to c++ Anyway on to question:

What I need to do this is to utilize the
constructor of one class in a second class - thereby optimizing the
code by avoiding duplication.

Take this example:

Constructor of first class:

Class1::Class1( void )
{
a = 0;

}

Instead of duplicating this I would like Class2 to use this same
constructor:

Class2::Class2( void )
{
//I would like code here that would pass an array to the Class1
constructor and if the array was 5 in size then each position in the
array would be initialized with a=0

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andyww14 is offline Offline
1 posts
since Nov 2006
Nov 19th, 2006
0

Re: Newbie Constructor / Array question

a class can have more than one constructor. write another constructor that takes the parameter you want.
C++ Syntax (Toggle Plain Text)
  1. class Class1
  2. {
  3. public:
  4. Class1(); // default constructor
  5. Class1(int array[10], int size);
  6. };
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Nov 19th, 2006
0

Re: Newbie Constructor / Array question

You can create multiple constructors via method overloading.

Each function/method that you create has something called a method signature.

C++ Syntax (Toggle Plain Text)
  1. void myMethod(int j);

That is an example of a method signature, your compiler reads it as "void myMethod(int)". If you would like to overload this method and create another method with the same name but do something slightly different, you must change it's signature.

C++ Syntax (Toggle Plain Text)
  1. void myMethod(String j);

This creates a different signature "void myMethod(String)" and is therefore a different function. Depending on what you pass as a parameter into your method when you use it, depends on which method is used. Be careful of typecasting!

Constructors work in the same way, you can create multiple constructors, and using the "this" keyword you can either reference an object the method is a part of, or call another constructor of the same class.

C++ Syntax (Toggle Plain Text)
  1. class myClass {
  2. private:
  3. int setThis;
  4.  
  5. public:
  6. myClass() { // first constructor calls the second constructor
  7. this(5);
  8. }
  9.  
  10. myClass(int n) { // second constructor, sets the class variable
  11. setThis = n;
  12. }
  13. }

if you compiled this class and called the following code:

C++ Syntax (Toggle Plain Text)
  1. myClass ex = new myClass(5);
  2.  
  3. // OR
  4.  
  5. myClass ex2 = new myClass();

the local variable to myClass (named setThis) would be set to 5 in BOTH examples.
Last edited by Mr Violent; Nov 19th, 2006 at 6:45 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Mr Violent is offline Offline
22 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: VS 97/5 any good?
Next Thread in C++ Forum Timeline: Precompiled Headers & Unexpected EOF error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC