Constructors

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 */

Leave a comment

Filed under C++

Use of Destructor

/* IMPLEMENTATION OF DESTRUCTORS */

#include< iostream.h>
#include< conio.h>

int count = 0;

class data
{
public:
data(void)
{
count++;
cout< < endl< < “The Number of The Object Created Is:”< < count;
}

~data()
{
cout< < endl< < “The Number Of The Object Destroyed Is”< < count;
count–;
}
};

void main()
{
clrscr();
data d1;
data d2;
{
data d3;
data d4;
}
data d5;
data d6;
}

/**********OUTPUT**********
The Number of The Object Created Is: 1
The Number of The Object Created Is: 2
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number of The Object Created Is: 3
The Number of The Object Created Is: 4
The Number Of The Object Destroyed Is 4
The Number Of The Object Destroyed Is 3
The Number Of The Object Destroyed Is 2
The Number Of The Object Destroyed Is 1

*/

Leave a comment

Filed under C++

Friend Function

/* C++ program for the Implementation Of Friend Function */

#include< iostream.h>
#include< conio.h>
class complex
{
float real;
float imag;
public:
void getdata(float x,float y)
{
real=x;
imag=y;
}
friend complex add (complex c1,complex c2);
void display()
{
cout< < “the complex no is”< < real< < “+i”< < imag< < endl;
}

};
complex add (complex c1,complex c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return (c3);

}
void main()
{
clrscr();
complex c1,c2,c3;

c1.getdata(4.2,5.5);
c2.getdata(3.5,5.6);
c3=add(c1,c2);
c3.display();
}

Leave a comment

Filed under C++

Operator Overloading

/* C++ Program for the IMPLEMENTATION OF OPERATOR OVERLOADING(BINARY). The Given Program performs Basic Arithematic operation : Addition, Subtraction, Multiplication and Division for Two Complex Numbers. */

#include< iostream.h>
#include< conio.h>
#include< process.h>
class complex
{
float real;
float imag;
public:
complex()
{}
complex(float x,float y)
{
real=x;
imag=y;
}
complex operator + (complex);
complex operator – (complex);
complex operator * (complex);
complex operator / (complex);
void display(void)
{
cout< < real< < ” +i” < < i mag< < endl;
}

};

complex complex :: operator +(complex c)
{
complex c2;
c2.real=real+c.real;
c2.imag=imag+c.imag;
return (c2);
}

complex complex :: operator -(complex c)
{
complex c2;
c2.real=real-c.real;
c2.imag=imag-c.imag;
return (c2);
}
complex complex :: operator *(complex c)
{
complex c2;
c2.real = ((real * c.real) – (imag * c.imag));
c2.imag = ((real * c.imag) + (imag * c.imag));
return (c2);
}
complex complex :: operator /(complex c)
{
complex c2;
c2.real=((real * c.real) + (imag * c.imag))/((real * c.real) + (imag * c.imag));
c2.imag=((imag * c.real) – (real * c.imag))/((real * c.real) + (imag * c.imag));
return (c2);
}

void main()
{
clrscr();
complex c1,c2,c3;
int op;
char ch,y,Y;

c1 = complex(5.6,2.7);
c2 = complex(3.5,5.6);
cout< < “Two Complex numbers Are :”< < endl;
c1.display();
c2.display();
do
{
cout< < endl< < “******** MENU *********”< < endl;
cout< < “1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exit”< < endl;
cout< < “Enter Your Choice : “;
cin>>op;
switch(op)
{
case 1:
c3 = c1 + c2;
cout< < “Addition of Two complex Nos. :”;
c3.display();
break;

case 2:
c3 = c1 – c2;
cout< < “Subtraction of Two complex Nos. :”;
c3.display();
break;

case 3:
c3 = c1 * c2;
cout< <” Multiplication of Two complex Nos. :”;
c3.display();
break;

case 4:
c3 = c1 / c2;
cout< < “division of Two complex Nos. :”;
c3.display();
break;

case 5:exit(0);

default: cout< < endl< < “Aborting!!!!!!!INVALID CHOICE”< < endl;
}
cout< < ” Do you want to continue(Y/y)”;
cin>>ch;
}
while(ch==’y’||ch==’Y’);
getch();
}
/******************* OUTPUT ********************
Two Complex numbers Are :
5.6 +i2.7
3.5 +i5.6

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 1
Addition of Two complex Nos. :9.1 +i8.3
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 2
Subtraction of Two complex Nos. :2.1 +i-2.9
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 3
Multiplication of Two complex Nos. :4.48 +i46.48
Do you want to continue(Y/y)y

******** MENU *********
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 4
division of Two complex Nos. :1 +i-0.631048
Do you want to continue(Y/y)n
*/

Leave a comment

Filed under C++

Factorial in c++

/* C++ program for calculating factorial of a number entered by the user */

# include < iostream.h>
# include < conio.h>
void fact(int);

void main()
{
clrscr();
int n;
cout < < “Enter limit : “;
cin>>n;
fact(n);
getch();
}
void fact(int x)
{
char a;
int i=1;
for(int j=1;j< =x;j++)
{
i*=j;
}
cout< < “Answer = “< < i< < endl;
cout< < “Want to continue ( y )/or press any key to continue “;
cin>>a;
if(a==’y’)
{
main();
}
else
{
cout< < “Good Bye “;
}
getch();
}

Leave a comment

Filed under C++

Operator Overloading – Unary

/********* IMPLEMENTATION OF OPERATOR OVERLOADING (UNARY)*********/
#include< iostream.h>
#include< conio.h>

class unary
{
private:
int x,y,z;
public:

unary(void)
{
cout< < “Enter Any Three Integer Nos. : “;
cin>>x>>y>>z;
}

void display(void)
{
cout< < endl< < ” The Three Nos. Are : “< < x< < ” , “< < y< < ” , “< < z;
}

void operator –()
{
x = –x;
y = –y;
z = –z;
}

void operator ++()
{
x = ++x;
y = ++y;
z = ++z;
}
};

void main()
{
clrscr();
unary s;
s.display();

–s;
cout< < endl< < endl< < endl< < “******* The Decremented Values ********”< < endl;
s.display();

++s;
cout< < endl< < endl< < endl< < “******* The Incremented Values ********”< < endl;
s.display();
cout< < endl< < endl< < “***************************************”;

getch();
}

/************ OUTPUT **************
Enter Any Three Integer Nos. : 4
-8
6

The Three Nos. Are : 4 , -8 , 6

******* The Decremented Values ********

The Three Nos. Are : 3 , -9 , 5

******* The Incremented Values ********

The Three Nos. Are : 4 , -8 , 6

***************************************
*/

Leave a comment

Filed under C++

DIJKSRTRA’S ALGORITHM in C

/*Implementation of Shortest Path Algorithm(DIJKSRTRA’s ALGORITHM) in C*/

#include< stdio.h>
#include< conio.h>
#include< process.h>
#include< string.h>
#include< math.h>
#define IN 99
#define N 6

int dijkstra(int cost[][N], int source, int target);

void main()
{
int cost[N][N],i,j,w,ch,co;
int source, target,x,y;
clrscr();
printf(“\tShortest Path Algorithm(DIJKSRTRA’s ALGORITHM\n\n”);
for(i=1;i< N;i++)
for(j=1;j< N;j++)
cost[i][j] = IN;
for(x=1;x< N;x++)
{
for(y=x+1;y< N;y++)
{
printf(“Enter the weight of the path between node %d and %d: “,x,y);
scanf(“%d”,&w);
cost [x][y] = cost[y][x] = w;
}
printf(“\n”);
}
printf(“\nEnter The Source:”);
scanf(“%d”, &source);
printf(“\nEnter The target”);
scanf(“%d”, &target);
co = dijsktra(cost,source,target);
printf(“\nShortest Path: %d”,co);
getch();
}

int dijsktra(int cost[][N],int source,int target)
{
int dist[N],prev[N],selected[N]={0},i,m,min,start,d,j;
char path[N];
for(i=1;i< N;i++)
{
dist[i] = IN;
prev[i] = -1;
}
start = source;
selected[start]=1;
dist[start] = 0;
while(selected[target] ==0)
{
min = IN;
m = 0;
for(i=1;i< N;i++)
{
d = dist[start] +cost[start][i];
if(d< dist[i]&&selected[i]==0)
{
dist[i] = d;
prev[i] = start;
}
if(min>dist[i] && selected[i]==0)
{
min = dist[i];
m = i;
}
}
start = m;
selected[start] = 1;
}
start = target;
j = 0;
while(start != -1)
{
path[j++] = start+65;
start = prev[start];
}
path[j]=”;
strrev(path);
printf(“%s”, path);
return dist[target];
}
/***** Output *********

Shortest Path Algorithm(DIJKSRTRA’s ALGORITHM

Enter the weight of the path between node 1 and 2: 2
Enter the weight of the path between node 1 and 3: 3
Enter the weight of the path between node 1 and 4: 4
Enter the weight of the path between node 1 and 5: 5

Enter the weight of the path between node 2 and 3: 5
Enter the weight of the path between node 2 and 4: 2
Enter the weight of the path between node 2 and 5: 3

Enter the weight of the path between node 3 and 4: 1
Enter the weight of the path between node 3 and 5: 4

Enter the weight of the path between node 4 and 5: 5

Enter The Source:2

Enter The target4
CE
Shortest Path: 2
*/

Leave a comment

Filed under C

Diamond program in C

/* C program to display a diamond using arrays */

#include< iostream.h>
#include< conio.h>
void main()
{
int i,j;
clrscr();
int no;
cout< < “Enter A Value”;
cin>>no;
for(i=no;i>=1;i–)
{
cout< < endl;
for(int k=1;k< =i;k++)
cout< < ” “;

for(j=i;j< =no;j++)
cout< < “*”;

for(j=i;j< no;j++)
cout< < “*”;
}
//SECOND PART

for(i=no;i>=1;i–)
{
cout< < endl;
cout< < ” “;
for(int k=no;k>=i;k–)
cout<<” “;

for(j=i-1;j>=1;j–)
cout< < “*”;

for(j=i-1;j>1;j–)
cout<<“*”;

}
getch();
}

Leave a comment

Filed under C

2 dimentional array program in C

/* C program to input and display a 2-d array*/

#include< stdio.h>
#include< conio.h>
void main()
{
int num[3][3],i,j;
clrscr();
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
scanf(“%d”,&num[i][j]);
}
}
for(i=0;i< 3;i++)
{
for(j=0;j< 3;j++)
{
printf(“\n%d”,num[i][j]);
}
printf(“\n”);
}
getch();
}

Leave a comment

Filed under C

Modification of a string using Pointers in C

/* Example of pointers in C. This program uses a function to modify a string using pointers */

#include< stdio.h>
#include< conio.h>
void main()
{
void getstr(char *ptr_str,int *ptr_int);
int var=5;
char *pstr=”lionel”;
clrscr();
getstr(pstr,&var);
printf(“The value of var after modification using pointer in a function is %d,var”);
}
void getstr (char *ptr_str,int *ptr_int)
{
printf(“%s\n”,ptr_str);
*ptr_int=6;
getch();
}

Leave a comment

Filed under C