Talk:Constructor (object-oriented programming)
Is a constructor really a method? I'd rather propose to say: a constructor is an operation AND a method is an operation, too. I.E. a method may have different return types whereas a constructor's return type should be clear. You might define a constructor as a special method with a constraint about its return type but defining them as different metaclasses seems to be a better (and proper) solution.
Constructors should not be considered as a method as the declaration syntax is very different from normal methods, and it cannot be called by other methods directly (it can be called indirectly by creating a new instance object). T4bits 15:24, 8 November 2005 (UTC)
Constructors in other languages
Does anyone know about contructors in other languages? It would be a great help, especially since the current article is mostly based on C++
Hello. I added a php one.. hope it helps. :-)
constructor
dd
Totall innaccurate and doesn't cite sources
I hope the authors realize there are more than 4 languages out there and many constructors return the object itself as a return value. Also there are no sources or citations. --Quirex 03:02, 10 December 2006 (UTC)
Functional programming
This article proceeds as-if constructors are a feature peculiar to OOLs, but constructors are also a feature of functional languages, such as Haskell. —SlamDiego 05:52, 13 December 2006 (UTC)
Constructors that use inheritence
I have moved this section from the article as it has a number of problems that should be sorted first. The areas that could be improved are:
- use of parent/child uncommon terminology for class inheritance
- dubious programming practice in the example
- example code formattting
- code in the penultimate paragraph won't compile
Rich257 13:19, 4 January 2007 (UTC)
Constructors that use inheritence
Constructors can use a parent/child system, in a similar way to that of classes. if a class "student" is a child of class "Person" then any variables that student uses (which are contained within person, ie, name, age, sex) will require a constructor in person in order to use them. This can be done with a default constructor (one that requires no parameters) or it can be done by calling the person constructor, on the same line as the definition for the student constructor.
Example
//person class public class person { public: int mAge; char* mSex; String* mName; person(int age, char* sex, String* name) { mAge=age; mSex=sex; mName=name; } }; //student class public class student : public person { public: String* mSchool; //student constructor containing a call to person constructor. student(int age, char* sex, String* name, String* school) : person(age,sex,name) { mSchool=school; } };
using this code, a new student can be made in this way.
student fred= new student(18,"M","fred flintstone","Exeter College");
when this is done the constructor for student is called, then before "school" is set, person is called and age,sex and name are set.