![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
The implementation of C++'s many constructors types.
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
The classic constructor is still available in C++, but they also offer a few additions to allow for shortened code, and fewer constructors. *I will write the following constructors as if they were in a class called Constructor. with 3 global fields to be initialized name, size, and text. Default Constructor: Code:
Constructor() {
name = "";
size = 0;
text = "";
}
Parameter List Constructor: Code:
Constructor(String n, int s, String t){
name = t;
size = s;
text = t;
}
Paramenter List Constructor 2: Constructor(String n, int s, String t):name(n),size(s),text(t) {} -this method utilizes a feature similar to the contructor, it is as if your primitive types now have there own constructors!! Simplifying your code. *There is one more thing you can add to this last modification, it will combine the Default and List Parameter Constructors into one! It will also allow for only partial constructors (eg: only enter a name or name and size only!) List Parameter with Defaults Constructor: Constructor(String n="", int s=0, String t=""):name(n),size(s),text(t) {} -in this case when information is supplied, if only one field is given, the left most field is assumed to be the field supplied. If any fields are left out, or this constructor is called as the default, the values set equal will be used instead!! Now that is all well and good for primatives, but what about objects? C++ to the rescue, there is a copy constructor format wich you can use to initialize an object using another object of the same type! Copy Constructor: Code:
Constructor(const Constructor& c){
this->name = c.name;
this->size = c.size;
this->text = c.text;
}
*Do also NOTE that the copy constuctor requires the use of a default or resonable equivilant to be written to initialize the this object. |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| get startup message: could not load file or 'mom.implementation | Anilrgowda | Microsoft Windows xp error | 1 | 27-Nov-2007 02:11 AM |
| get startup message: could not load file or 'mom.implementation | Anilrgowda | Microsoft windows vista error | 1 | 13-Nov-2007 10:33 PM |
| RTP Player implementation | Iphone | Programming Error ! | 1 | 29-Mar-2007 03:13 AM |
| Limit Your Google Searches to Certain Types of Files | Anilrgowda | 0 | 07-Dec-2006 12:04 AM | |
| The 3 basic types of Keyboards | Anilrgowda | Hardware tutorials | 0 | 08-Sep-2006 02:32 PM |