Вы находитесь на странице: 1из 5

PDPU, Gandhinagar 15A Constructors-All Programs

Constr1.cpp
// SOURCE CODE: ADAPTED FROM BOOK: OBJECT ORIENTED PROGRAMMING WITH C++ BY
E BALAGURUSWAMI.

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

int count = 0;

class alpha
{
public:
alpha()
{
count++;
cout << endl << "\tObject no. "<< count <<" created ";
};
~alpha()
{
cout << endl << "\tObject no. "<< count <<" deleted ";
cout << "\n\tPress a key to continue...";
getch();
count--;
};
};

void main()
{
clrscr();
cout << "Inside main ... ";
alpha A1, A2, A3, A4, A5;
{
cout << "\n\nEntered Block 1";
alpha A5;
};
{
cout << "\n\nEntered Block 2";
alpha A6;
};
cout << "\n\nNow again in main()";
};

By Darshit Shah 1
PDPU, Gandhinagar 15A Constructors-All Programs

Constr2.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

// THIS PROGRAM WILL NOT COMPILE AND IT WILL FLASH THE ERROR:
// CAN NOT INITIALIZE A CLASS MEMBER HERE.

class circle
{
private:
int r = 5; //  This line contains an error.
public :
float area(void)
{
return r * r * 22.0 / 7;
}
};

void main()
{
clrscr();
circle a;
cout << a.area();
getch();
};

By Darshit Shah 2
PDPU, Gandhinagar 15A Constructors-All Programs

Constr3.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>

class circle
{
private:
int radius;
float area;
public:
circle()
{
radius = 0;
};
circle(int r)
{
radius = r;
};
circle(circle & c)
{
radius = c.radius;
};
void showval(char nm[])
{
cout << nm << ".Radius = " << radius << endl;
};
};

void main()
{
clrscr();
circle c1; // constructor with no argument will be executed.
circle c2=circle(10);//constructor with one argument will be executed.
circle c3(20); // ANOTHER WAY OF CREATING OBJECT.
circle c4(c3); // COPY CONSTRUCTOR. C3 IS ANOTHER OBJECT.
c1.showval("c1");
c2.showval("c2");
c3.showval("c3");
c4.showval("c4");
getch();
};

By Darshit Shah 3
PDPU, Gandhinagar 15A Constructors-All Programs

Constr4.cpp
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
/* IN THIS PROGRAM, WE HAVE NOT DEFINED COPY CONSTRUCTOR, EVEN THEN
THE PROGRAM IS WORKING PERFECTLY. WHY?
'CAUSE COMPILER SUPPLIES ITS OWN COPY CONSTRUCTOR.
*/
class circle
{
private:
int radius;
float area;
public:
circle(int r)
{
radius = r;
};
void showval(char nm[])
{
cout << nm << ".Radius = " << radius << endl;
};
};

void main()
{
clrscr();
circle c1 = circle(10);
circle c2(c1); // LOOK AT THIS STATEMENT.
c1.showval("c1");
c2.showval("c2");
getch();
};

Constr5.cpp
// SOURCE CODE: ADAPTED FROM BOOK: OBJECT ORIENTED PROGRAMMING WITH C++ BY
E BALAGURUSWAMI.
#include <iostream.h>
#include <string.h>
#include <conio.h>

By Darshit Shah 4
PDPU, Gandhinagar 15A Constructors-All Programs

class String
{
char * name;
int length;
public:
String()
{
length = 0;
name = new char[length + 1];
};
String (char * s)
{
length = strlen(s);
name = new char[length + 1]; /* 1 additional character is
for \0.*/
strcpy(name,s);
};
void display(void)
{
cout << name << "\n";
};
void join(String & a, String & b);
};

void String :: join (String &a, String &b)


{
length = a.length + b.length;
delete name;
name = new char [length + 1]; // dynamic allocation.

strcpy(name, a.name);
strcat(name, b.name);
};
void main()
{
clrscr();
char * first = "Computer ";
String name1(first),name2("Programming "),name3("SPTG"),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
getch();
};

By Darshit Shah 5

Вам также может понравиться