| | |
Newbie Constructor / Array question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2006
Posts: 1
Reputation:
Solved Threads: 0
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
}
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
}
a class can have more than one constructor. write another constructor that takes the parameter you want.
C++ Syntax (Toggle Plain Text)
class Class1 { public: Class1(); // default constructor Class1(int array[10], int size); };
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2005
Posts: 22
Reputation:
Solved Threads: 0
You can create multiple constructors via method overloading.
Each function/method that you create has something called a method signature.
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.
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.
if you compiled this class and called the following code:
the local variable to myClass (named setThis) would be set to 5 in BOTH examples.
Each function/method that you create has something called a method signature.
C++ Syntax (Toggle Plain Text)
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)
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)
class myClass { private: int setThis; public: myClass() { // first constructor calls the second constructor this(5); } myClass(int n) { // second constructor, sets the class variable setThis = n; } }
if you compiled this class and called the following code:
C++ Syntax (Toggle Plain Text)
myClass ex = new myClass(5); // OR 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.
![]() |
Similar Threads
- Array Question (C++)
- C++ Array Question (C++)
- constructor with an array as an argument (C++)
- Class with dynamic array how? (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: VS 97/5 any good?
- Next Thread: Precompiled Headers & Unexpected EOF error
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






