Your Ad Here

Monday, February 4, 2008

Implementation Of Constructors

A constructor is a member function with the same name as its class. For example:

class X
{
public:
X(); // constructor for class X
};

Constructors are used to create, and can initialize, objects of their class type.

You cannot declare a constructor as virtual or static, nor can you declare a constructor as const, volatile, or const volatile.

You do not specify a return type for a constructor. A return statement in the body of a constructor cannot have a return value.


Types?

Default Constructor:

Constructor() {
name = "";
size = 0;
text = "";
}

-takes 0 parameters, and initializes them as any other language could.


Parameter List Constructor:

Constructor(String n, int s, String t){
name = t;
size = s;
text = t;
}

-again similar to other languages, but this kind of work is not necessary, this is one of many places C++ shines.


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:

Constructor(const Constructor& c){
this->name = c.name;
this->size = c.size;
this->text = c.text;
}


/* IMPLEMENTATION OF DIFFERENT TYPES OF CONSTRUCTORS */

#include<>
#include<>

class data
{
private:
int x,y,z;

public:
data(void)
{
x = 0;
y = 0;
z = 0;
cout< <"This Is The First Type Of The Constructor"< < endl;
cout< < " The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a)
{
x = a;
y = 0;
z = 0;
cout< < "This Is The Second Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl< < endl;
}

data(int a,int b)
{
x = a;
y = b;
z = 0;
cout< < "This Is The Third Type Of The Constructor"< < endl;
cout< < "The Three Values Are"< < x< < ","< < y< < ","< < z< < endl < < endl;
}

data(int a,int b,int c)
{
x = a;
y = b;
z = c;
cout< < "This Is The Fourth Type Of The Constructor"< < endl;
cout< <"The Three Values Are"< < x< <" ,"< < y< < ","< < z< < endl;
}
};

void main()
{
data d1();
data d2 = data(9);
data d3(1,2);
data d4(1,2,4);

getch();
}

/* OUTPUT *

This Is The First Type Of The Constructor
The Three Values Are0,0,0

Is The Second Type Of The Constructor
The Three Values Are9,0,0

This Is The Third Type Of The Constructor
The Three Values Are1,2,0

This Is The Fourth Type Of The Constructor
The Three Values Are1,2,4 */

3 comments:

Anonymous said...

good work lionel, can u also give some projects using the concepts of file handling for class XII students

Anonymous said...

I think in c++ void main() is not there...

Anonymous said...

baby ....void main() is there

Your Ad Here