C++ SOURCE CODES
1. SET PASSWORD IN C++
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
char *pass[10],*pass="name";
cout<<"Enter the password:";
goto start:
pass[i]=getch();
putch('*');
if(pass[i]!=13)
{
i++;
goto start;
}
cout<<"your password is"<<pass;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int i=0;
char *pass[10],*pass="name";
cout<<"Enter the password:";
goto start:
pass[i]=getch();
putch('*');
if(pass[i]!=13)
{
i++;
goto start;
}
cout<<"your password is"<<pass;
getch();
}
*********
2. PROGRAMME FOR E.G. OF WIDTH AND SETEW FUNCTIONS
//e.g. of width and setew function...
#include <iostream.h>
#include <iomanip.h>
void main ()
{
int i;
cout << "A list of numbers :" << endl;
for (i = 1; i <= 1024; i *= 2)
{
cout.width (7);
cout << i << endl;
}
cout << "A table of numbers :" << endl;
for (i = 0; i <= 4; i++)
{
cout << setw(3) << i << setw(5) << i * i * i << endl;
}
}
*********
3. A PROGRAM THAT READS FROM A CHARACTER STRING
//A program that reads from a character string :
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
void main ()
{
char a[1024];
istrstream b(a, 1024);
strcpy (a, "45.656");
double k, p;
b.seekg(0); // Start from first character.
b >> k;
k = k + 1;
cout << k << endl;
strcpy (a, "444.23 56.89");
b.seekg(0);
b >> k >> p;
cout << k << ", " << p + 1 << endl;
}
*********
4. HERE IS A PROGRAM THAT WRITES INSIDE A CHARACTER ARRAY
//Here is a program that writes inside a character array :
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
#include <math.h>
void main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus : ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
}
*********
5. PROGRAMME TO SHOW THAT.A CLASS CAN BE DERIVED FROM MORE THAN 1 BASE CLASSES
//a class can be derived from more than 1 base classes
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double surface()
{
return fabs (x * y);
}
};
class number
{
public:
double z;
number (double a)
{
z = a;
}
int is_negative ()
{
if (z < 0) return 1;
else return 0;
}
};
class trivector : public vector, public number
{
public:
trivector(double a=0, double b=0, double c=0) : vector(a,b), number(c)
{
} // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.
double volume()
{
return fabs (x * y * z);
}
};
void main()
{
trivector a(2, 3, -4);
cout << a.volume() << endl;
cout << a.surface() << endl;
cout << a.is_negative() << endl;
}
//a class can be derived from more than 1 base classes
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double surface()
{
return fabs (x * y);
}
};
class number
{
public:
double z;
number (double a)
{
z = a;
}
int is_negative ()
{
if (z < 0) return 1;
else return 0;
}
};
class trivector : public vector, public number
{
public:
trivector(double a=0, double b=0, double c=0) : vector(a,b), number(c)
{
} // The trivector constructor calls the vector
// constructor, then the number constructor,
// and in this example does nothing more.
double volume()
{
return fabs (x * y * z);
}
};
void main()
{
trivector a(2, 3, -4);
cout << a.volume() << endl;
cout << a.surface() << endl;
cout << a.is_negative() << endl;
}
*********
6. PROGRAMME FOR TRI-VECTOR CONSTRUCTOR
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
virtual double module()
{
return sqrt (x*x + y*y);
}
};
class trivector : public vector
{
public:
double z;
trivector (double m = 0, double n = 0, double p = 0)
{
x = m; // Just for the game,
y = n; // here I do not call the vector
z = p; // constructor and I make the
} // trivector constructor do the
// whole job. Same result.
double module ()
{
return sqrt (x*x + y*y + z*z);
}
};
void test (vector &k)
{
cout << "Test result : " << k.module() << endl;
}
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3)" << endl << endl;
vector *r;
r = &a;
cout << "module of vector a : " << r->module() << endl;
r = &b;
cout << "module of trivector b : " << r->module() << endl;
test (a);
test (b);
vector &s = b;
cout << "module of trivector b : " << s.module() << endl;
}
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
virtual double module()
{
return sqrt (x*x + y*y);
}
};
class trivector : public vector
{
public:
double z;
trivector (double m = 0, double n = 0, double p = 0)
{
x = m; // Just for the game,
y = n; // here I do not call the vector
z = p; // constructor and I make the
} // trivector constructor do the
// whole job. Same result.
double module ()
{
return sqrt (x*x + y*y + z*z);
}
};
void test (vector &k)
{
cout << "Test result : " << k.module() << endl;
}
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3)" << endl << endl;
vector *r;
r = &a;
cout << "module of vector a : " << r->module() << endl;
r = &b;
cout << "module of trivector b : " << r->module() << endl;
test (a);
test (b);
vector &s = b;
cout << "module of trivector b : " << s.module() << endl;
}
*********
7. A CLASS MAY BE DERIVED FROM ANOTHER CLASS.
//A class may be DERIVED from another class. The new class INHERITS
//the variables and methods of the BASE CLASS. Additional variables
// and/or methods can be added :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x*x + y*y);
}
double surface()
{
return x * y;
}
};
class trivector : public vector // trivector is derived from vector
{
public:
double z; // added to x and y from vector
trivector (double m=0, double n=0, double p=0) : vector (m, n)
{
z = p; // Vector constructor will
} // be called before trivector
// constructor, with parameters
// m and n.
trivector (vector a) // What to do if a vector is
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}
double module () // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}
double volume ()
{
return this->surface() * z; // or x * y * z
}
};
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
cout << "Surface of a : " << a.surface() << endl;
cout << "Volume of b : " << b.volume() << endl;
cout << "Surface of base of b : " << b.surface() << endl;
cout << "Module of a : " << a.module() << endl;
cout << "Module of b : " << b.module() << endl;
cout << "Module of base of b : " << b.vector::module() << endl;
trivector k;
k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0
vector j;
j = b; // copy of x and y. b.z leaved out
vector *r;
r = &b;
cout << "Surface of r : " << r->surface() << endl;
cout << "Module of r : " << r->module() << endl;
}
//A class may be DERIVED from another class. The new class INHERITS
//the variables and methods of the BASE CLASS. Additional variables
// and/or methods can be added :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x*x + y*y);
}
double surface()
{
return x * y;
}
};
class trivector : public vector // trivector is derived from vector
{
public:
double z; // added to x and y from vector
trivector (double m=0, double n=0, double p=0) : vector (m, n)
{
z = p; // Vector constructor will
} // be called before trivector
// constructor, with parameters
// m and n.
trivector (vector a) // What to do if a vector is
{ // cast to a trivector
x = a.x;
y = a.y;
z = 0;
}
double module () // define module() for trivector
{
return sqrt (x*x + y*y + z*z);
}
double volume ()
{
return this->surface() * z; // or x * y * z
}
};
void main()
{
vector a (4, 5);
trivector b (1, 2, 3);
cout << "a (4, 5) b (1, 2, 3) *r = b" << endl << endl;
cout << "Surface of a : " << a.surface() << endl;
cout << "Volume of b : " << b.volume() << endl;
cout << "Surface of base of b : " << b.surface() << endl;
cout << "Module of a : " << a.module() << endl;
cout << "Module of b : " << b.module() << endl;
cout << "Module of base of b : " << b.vector::module() << endl;
trivector k;
k = a; // thanks to trivector(vector) definition
// copy of x and y, k.z = 0
vector j;
j = b; // copy of x and y. b.z leaved out
vector *r;
r = &b;
cout << "Surface of r : " << r->surface() << endl;
cout << "Module of r : " << r->module() << endl;
}
*********
8. CLASS VARIABLE CAN ALSO BE CONSTANT
//A class variable can also be CONSTANT. That's just like static,
//except it is allocated a value inside the class declaration and
//that value may not be modified :
#include <iostream.h>
class vector
{
public:
double x;
double y;
const double pi = 3.1415927;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double cilinder_volume ()
{
return x * x / 4 * pi * y;
40 of 56 12.03.99 01:19
C++ Tutorial file:///C'/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
}
};
void main(void)
{
cout << "The value of pi : " << vector::pi << endl << endl;
vector k (3, 4);
cout << "Result : " << k.cilinder_volume() << endl;
}
//A class variable can also be CONSTANT. That's just like static,
//except it is allocated a value inside the class declaration and
//that value may not be modified :
#include <iostream.h>
class vector
{
public:
double x;
double y;
const double pi = 3.1415927;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double cilinder_volume ()
{
return x * x / 4 * pi * y;
40 of 56 12.03.99 01:19
C++ Tutorial file:///C'/Eigene Dateien/Manualz/not added/C++ Tutorial for C Users/cppcen.htm
}
};
void main(void)
{
cout << "The value of pi : " << vector::pi << endl << endl;
vector k (3, 4);
cout << "Result : " << k.cilinder_volume() << endl;
}
*********
9. CLASS, VARIABLE CAN BE DECLARED STATIC
//A class' variable can be declared STATIC. Then only one instance of
//that variable exists, shared by all instances of the class :
#include <iostream.h>
class vector
{
public:
double x;
double y;
static int count;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
count = count + 1;
}
~vector()
{
count = count - 1;
}
};
void main ()
{
vector::count = 0;
cout << "Number of vectors :" << endl;
vector a;
cout << vector::count << endl;
vector b;
cout << vector::count << endl;
vector *r, *u;
r = new vector;
cout << vector::count << endl;
u = new vector;
cout << a.count << endl;
delete (r);
cout << vector::count << endl;
delete (u);
cout << b.count << endl;
}
//A class' variable can be declared STATIC. Then only one instance of
//that variable exists, shared by all instances of the class :
#include <iostream.h>
class vector
{
public:
double x;
double y;
static int count;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
count = count + 1;
}
~vector()
{
count = count - 1;
}
};
void main ()
{
vector::count = 0;
cout << "Number of vectors :" << endl;
vector a;
cout << vector::count << endl;
vector b;
cout << vector::count << endl;
vector *r, *u;
r = new vector;
cout << vector::count << endl;
u = new vector;
cout << a.count << endl;
delete (r);
cout << vector::count << endl;
delete (u);
cout << b.count << endl;
}
*********
10.OVERLOADING OF ,+, OPERATOR WITH PROTOTYPE SAME AS NAME OF CLASS NAME
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (a * x, a * y);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
vector &the_vector = *this;
double length = the_vector.module();
x = x / length * a;
y = y / length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector c (3, 5);
vector *r; // r is a pointer to a vector.
r = new vector; // new allocates the memory necessary
cout << *r << endl; // to hold a vectors' variable,
// calls the constructor who will
// initialize it to 0, 0. Then finally
// new returns the address of the vector.
r->x = 94;
r->y = 345;
cout << *r << endl;
*r = vector (94, 343);
cout << *r << endl;
*r = *r - c;
r->set_length(3);
cout << *r << endl;
*r = (-c * 3 + -*r * 4) * 5;
cout << *r << endl;
delete (r); // Calls the vector destructor then
// frees the memory.
r = &c; // r points towards vector c
cout << *r << endl;
r = new vector (78, 345); // Creates a new vector.
cout << *r << endl; // The constructor will initialise
// the vector's x and y at 78 and 345
cout << "x component of r : " << r->x << endl;
cout << "x component of r : " << (*r).x << endl;
delete (r);
r = new vector[4]; // creates an array of 4 vectors
r[3] = vector (4, 5);
cout << r[3].module() << endl;
delete (r); // deletes the array
int n = 5;
r = new vector[n]; // Cute !
r[1] = vector (432, 3);
cout << r[1] << endl;
delete (r);
}
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (a * x, a * y);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
vector &the_vector = *this;
double length = the_vector.module();
x = x / length * a;
y = y / length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector c (3, 5);
vector *r; // r is a pointer to a vector.
r = new vector; // new allocates the memory necessary
cout << *r << endl; // to hold a vectors' variable,
// calls the constructor who will
// initialize it to 0, 0. Then finally
// new returns the address of the vector.
r->x = 94;
r->y = 345;
cout << *r << endl;
*r = vector (94, 343);
cout << *r << endl;
*r = *r - c;
r->set_length(3);
cout << *r << endl;
*r = (-c * 3 + -*r * 4) * 5;
cout << *r << endl;
delete (r); // Calls the vector destructor then
// frees the memory.
r = &c; // r points towards vector c
cout << *r << endl;
r = new vector (78, 345); // Creates a new vector.
cout << *r << endl; // The constructor will initialise
// the vector's x and y at 78 and 345
cout << "x component of r : " << r->x << endl;
cout << "x component of r : " << (*r).x << endl;
delete (r);
r = new vector[4]; // creates an array of 4 vectors
r[3] = vector (4, 5);
cout << r[3].module() << endl;
delete (r); // deletes the array
int n = 5;
r = new vector[n]; // Cute !
r[1] = vector (432, 3);
cout << r[1] << endl;
delete (r);
}
*********
11. WRITE INSIDE A CHARACTER ARRAY
//Here is a program that writes inside a character array :
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
#include <math.h>
void main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus : ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
}
//Here is a program that writes inside a character array :
#include <iostream.h>
#include <strstrea.h>
#include <string.h>
#include <math.h>
void main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus : ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
}
*********
12. PROGRAMME FOR SORTED LINKED LIST...(NOT ONLY LINKED LIST MARK SORTED.)
#include<iostream.h>
#include<process.h>
class link
{
link* next;
int data,no;
public :
link* sort(link*);
void traverse(link*);
//link* dele (link*);
};
link * link::sort(link* temp)
{
link* newlink;
link* dummy1;
newlink=new link;
cout<<"\nEnter Data (only positive number) :";
cin>>no;
dummy1 = temp;
if (temp==NULL '' dummy1->data > no)
{
newlink->data = no;
newlink->next=temp;
temp=newlink;
return temp;
}
else
{
link *cur;
link *dummy;
cur=temp;
dummy = cur->next ;
while(cur->next!=NULL)
{
if(dummy->data > no)
{
newlink->next = cur->next;
newlink->data = no;
cur->next = newlink;
return temp;
}
dummy=dummy->next ;
cur=cur->next;
}
newlink->data = no;
newlink->next=NULL;
cur->next=newlink;
}
return temp;
}
void link::traverse(link *temp)
{
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
#include<iostream.h>
#include<process.h>
class link
{
link* next;
int data,no;
public :
link* sort(link*);
void traverse(link*);
//link* dele (link*);
};
link * link::sort(link* temp)
{
link* newlink;
link* dummy1;
newlink=new link;
cout<<"\nEnter Data (only positive number) :";
cin>>no;
dummy1 = temp;
if (temp==NULL '' dummy1->data > no)
{
newlink->data = no;
newlink->next=temp;
temp=newlink;
return temp;
}
else
{
link *cur;
link *dummy;
cur=temp;
dummy = cur->next ;
while(cur->next!=NULL)
{
if(dummy->data > no)
{
newlink->next = cur->next;
newlink->data = no;
cur->next = newlink;
return temp;
}
dummy=dummy->next ;
cur=cur->next;
}
newlink->data = no;
newlink->next=NULL;
cur->next=newlink;
}
return temp;
}
void link::traverse(link *temp)
{
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void main()
{
link *first=NULL,l1;
int choice;
while(1)
{
cout<<"\n*************** SORTED LINK LIST (For Integer Only) ***************\n\n";
cout<<"Choices Are :-\n=> [1] For Insert \n=> [2] For Traverse \n=> [3] For Exit";
cout<<"\n\nEnter Your choice : ";
cin>>choice;
switch (choice)
{
case 1:
first=l1.sort(first);
break;
case 2:
l1.traverse(first);
break;
case 3:
exit(0);
}
}
}
*********
13. SHELL SORT
#include<iostream.h>
#include<conio.h>
void main()
{
int k,i,temp,n,a[20],swap,gap;
clrscr();
cout<<"enter the no. of elements: ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
gap=n/2;
k=0;
do{
do{
swap=0;
k++;
for(i=1;i<=n-gap;i++)
if(a[i]>a[i+gap])
{
temp=a[i];
a[i]=a[i+gap];
a[i+gap]=temp;
swap=1;
}
}while(swap);
gap=gap/2;
}while(gap);
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
getch();
}
#include<iostream.h>
#include<conio.h>
void main()
{
int k,i,temp,n,a[20],swap,gap;
clrscr();
cout<<"enter the no. of elements: ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
gap=n/2;
k=0;
do{
do{
swap=0;
k++;
for(i=1;i<=n-gap;i++)
if(a[i]>a[i+gap])
{
temp=a[i];
a[i]=a[i+gap];
a[i+gap]=temp;
swap=1;
}
}while(swap);
gap=gap/2;
}while(gap);
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
getch();
}
*********
14. QUICK SORT
#include<iostream.h>
#include<conio.h>
int loc=0;
void main()
{
int i,n,a[20],lower[20],upper[20],top,beg,end;
void quick(int *,int,int,int,int);
clrscr();
cout<<"enter the no. of elements : ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
top=0;
if(n>1)
{
top=top+1;
lower[1]=1;
upper[1]=n;
}
while(top!=0)
{
beg=lower[top];
end=upper[top];
top--;
quick(a,n,beg,end,loc);
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
}
void quick(int a[],int n,int beg, int end, int loc)
{
int left,right,temp;
left=beg;
right=end;
loc=beg;
a:
while((a[loc]<=a[right]) && (loc!=right))
right=right-1;
if (loc==right)
return;
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while((a[left]<=a[loc]) && (left!=loc))
left=left+1;
if(loc==left)
return;
if(a[left]>a[loc])
{
temp=a[left];
a[left]=a[loc];
a[loc]=temp;
loc=left;
goto a;
}
}
#include<iostream.h>
#include<conio.h>
int loc=0;
void main()
{
int i,n,a[20],lower[20],upper[20],top,beg,end;
void quick(int *,int,int,int,int);
clrscr();
cout<<"enter the no. of elements : ";
cin>>n;
cout<<"enter elements: \n";
for(i=1;i<=n;i++)
cin>>a[i];
top=0;
if(n>1)
{
top=top+1;
lower[1]=1;
upper[1]=n;
}
while(top!=0)
{
beg=lower[top];
end=upper[top];
top--;
quick(a,n,beg,end,loc);
if(beg<loc-1)
{
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1<end)
{
top++;
lower[top]=loc+1;
upper[top]=end;
}
}
cout<<"sorted list is: \n";
for(i=1;i<=n;i++)
cout<<a[i]<<endl;
}
void quick(int a[],int n,int beg, int end, int loc)
{
int left,right,temp;
left=beg;
right=end;
loc=beg;
a:
while((a[loc]<=a[right]) && (loc!=right))
right=right-1;
if (loc==right)
return;
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
}
while((a[left]<=a[loc]) && (left!=loc))
left=left+1;
if(loc==left)
return;
if(a[left]>a[loc])
{
temp=a[left];
a[left]=a[loc];
a[loc]=temp;
loc=left;
goto a;
}
}
**********
15. STAR SIMMULATION
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include"apvector.h"
#define LEFT 75
#define RIGHT 77
typedef unsigned char byte;
void set_mode(byte mode)
{
union REGS regs;
regs.h.ah = 0x00;
regs.h.al = mode;
int86(0x10, ®s, ®s);
}
void pixel(int x,int y,byte color)
{
union REGS regs;
regs.h.ah = 0x0C;
regs.h.al = color;
regs.x.cx = x;
regs.x.dx = y;
int86(0x10, ®s, ®s);
}
void opening(void)
{
clrscr();
_setcursortype(_NOCURSOR);
gotoxy(30,1); cout<<"Starfield Simulation";
gotoxy(37,2); cout<<"by Sean Bugel";
gotoxy(2,19); cout<<"+ to speed up star field";
gotoxy(2,20); cout<<"- to slow down star field";
gotoxy(2,21); printf("%c to move the star field right",16);
gotoxy(2,22); printf("%c to move the star field left",17);
gotoxy(2,23); cout<<"'Esc' to quit simulation";
while(!kbhit())
{
gotoxy(1,25);
clreol();
delay(750);
gotoxy(28,25);
cout<<"Press any key to continue...";
delay(1000);
}
clrscr();
}
void main(void)
{
opening();
set_mode(0x13);
randomize();
apvector<int>x(31); //array of x axis for 30 stars
apvector<int>y(31); //array of y axis for 30 stars
int i=8,finish=0,a=0,b=0,inc=2,c,dir=0; //i is star speed, finish is variable for exit from loop
//a & b used for finding random star position loop
//inc slows down second and third field
//dir is variable to choose direction of starfield, 0 left, 1 right
while(a!=30) //allocate 30 x positions
{
x[a]=rand()%320;
a++;
}
while(b!=30) //allocate 30 y positions
{
y[b]=rand()%320;
b++;
}
c=1;
while(c!=10) //place 10 WHITE stars
{
pixel(x[c],y[c],WHITE);
c++;
}
while(!finish)
{
if(kbhit())
{
switch(getch()) //This code is for controlling the speed
{
case 61: //+
{
if(i>=2){i--;}
else break;
} break;
case 43: //+
{
if(i>=2){i--;}
else break;
} break;
case 95: //-
{
if(i<=16){i++;}
else break;
} break;
case 45: //-
{
if(i<=16){i++;}
else break;
} break;
case LEFT:
{
dir=0;
} break;
case RIGHT:
{
dir=1;
} break;
case 27: //Quit
{
finish=1;
} break;
default: break;
}
}
inc++; //variable used to slow down second and third field
c=1;
while(c<=10) //erase first field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=1;
while(c<=10) //checks to see if field 1 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
if(inc%2==0) //second star field algos
{
c=11;
while(c<=20) //erase second field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=11;
while(c<=20) //checks to see if field 2 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
c=11;
while(c<=20) //place 10 LIGHTGRAY stars
{
pixel(x[c],y[c],LIGHTGRAY);
c++;
}
} //end of second star field data
if( inc%5==0) //third star field algos
{
c=21;
while(c<=30) //erase third field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=21;
while(c<=30) //checks to see if field 3 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
c=21;
while(c<=30) //place 10 dark gray stars
{
pixel(x[c],y[c],DARKGRAY);
c++;
}
} //end of third star field data
c=1;
while(c<=10) //place 10 white stars
{
pixel(x[c],y[c],WHITE);
c++;
}
delay(i);
if(inc==500){inc=2;} //to keep the variable from getting too big
}
set_mode(0x03);
_setcursortype(_NORMALCURSOR);
}
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include"apvector.h"
#define LEFT 75
#define RIGHT 77
typedef unsigned char byte;
void set_mode(byte mode)
{
union REGS regs;
regs.h.ah = 0x00;
regs.h.al = mode;
int86(0x10, ®s, ®s);
}
void pixel(int x,int y,byte color)
{
union REGS regs;
regs.h.ah = 0x0C;
regs.h.al = color;
regs.x.cx = x;
regs.x.dx = y;
int86(0x10, ®s, ®s);
}
void opening(void)
{
clrscr();
_setcursortype(_NOCURSOR);
gotoxy(30,1); cout<<"Starfield Simulation";
gotoxy(37,2); cout<<"by Sean Bugel";
gotoxy(2,19); cout<<"+ to speed up star field";
gotoxy(2,20); cout<<"- to slow down star field";
gotoxy(2,21); printf("%c to move the star field right",16);
gotoxy(2,22); printf("%c to move the star field left",17);
gotoxy(2,23); cout<<"'Esc' to quit simulation";
while(!kbhit())
{
gotoxy(1,25);
clreol();
delay(750);
gotoxy(28,25);
cout<<"Press any key to continue...";
delay(1000);
}
clrscr();
}
void main(void)
{
opening();
set_mode(0x13);
randomize();
apvector<int>x(31); //array of x axis for 30 stars
apvector<int>y(31); //array of y axis for 30 stars
int i=8,finish=0,a=0,b=0,inc=2,c,dir=0; //i is star speed, finish is variable for exit from loop
//a & b used for finding random star position loop
//inc slows down second and third field
//dir is variable to choose direction of starfield, 0 left, 1 right
while(a!=30) //allocate 30 x positions
{
x[a]=rand()%320;
a++;
}
while(b!=30) //allocate 30 y positions
{
y[b]=rand()%320;
b++;
}
c=1;
while(c!=10) //place 10 WHITE stars
{
pixel(x[c],y[c],WHITE);
c++;
}
while(!finish)
{
if(kbhit())
{
switch(getch()) //This code is for controlling the speed
{
case 61: //+
{
if(i>=2){i--;}
else break;
} break;
case 43: //+
{
if(i>=2){i--;}
else break;
} break;
case 95: //-
{
if(i<=16){i++;}
else break;
} break;
case 45: //-
{
if(i<=16){i++;}
else break;
} break;
case LEFT:
{
dir=0;
} break;
case RIGHT:
{
dir=1;
} break;
case 27: //Quit
{
finish=1;
} break;
default: break;
}
}
inc++; //variable used to slow down second and third field
c=1;
while(c<=10) //erase first field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=1;
while(c<=10) //checks to see if field 1 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
if(inc%2==0) //second star field algos
{
c=11;
while(c<=20) //erase second field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=11;
while(c<=20) //checks to see if field 2 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
c=11;
while(c<=20) //place 10 LIGHTGRAY stars
{
pixel(x[c],y[c],LIGHTGRAY);
c++;
}
} //end of second star field data
if( inc%5==0) //third star field algos
{
c=21;
while(c<=30) //erase third field
{
pixel(x[c],y[c],BLACK);
c++;
}
c=21;
while(c<=30) //checks to see if field 3 stars hit border
{
if(dir==0) //if direction is left
{
if(x[c]!=1){x[c]--;} else{y[c]=rand()%200;x[c]=320;}
}
else if(dir=1) //if direction is right
{
if(x[c]!=320){x[c]++;} else{y[c]=rand()%200;x[c]=1;}
}
c++;
}
c=21;
while(c<=30) //place 10 dark gray stars
{
pixel(x[c],y[c],DARKGRAY);
c++;
}
} //end of third star field data
c=1;
while(c<=10) //place 10 white stars
{
pixel(x[c],y[c],WHITE);
c++;
}
delay(i);
if(inc==500){inc=2;} //to keep the variable from getting too big
}
set_mode(0x03);
_setcursortype(_NORMALCURSOR);
}
/* Simulate reception of Ctrl-Alt-Del: */
for (;;)
{
*shiftstate '= 0x0C; /* turn on Ctrl & Alt */
reg.h.ah = 0x4F; /* see notes below */
reg.h.al = 0x53; /* 0x53 =Del 's scan code */
reg.x.cflag = 1; /* sentinel for ignoring key */
int86(0x15, ®, ®);
/* If carry flag is still set, we've finished. */
if(reg.x.cflag)
break;
/* Else waste some time before trying again: */
reg.h.ah = 0;
int86(0x1A, ®, ®); /* system time into CX:DX */
ticks = reg.x.dx;
for (time_to_waste = 3; time_to_waste > 0; )
{
reg.h.ah = 0;
int86(0x1A, ®, ®);
if (ticks != reg.x.dx)
ticks = reg.x.dx , --time_to_waste;
}
}
/* Issue a DOS disk reset request: */
reg.h.ah = 0x0D;
int86(0x21, ®, ®);
/* Set boot type and boot: */
*boottype = (want_warm ? 0x1234 : 0);
(*boot)( );
}
void main()
{
bootme(0);
}
for (;;)
{
*shiftstate '= 0x0C; /* turn on Ctrl & Alt */
reg.h.ah = 0x4F; /* see notes below */
reg.h.al = 0x53; /* 0x53 =
reg.x.cflag = 1; /* sentinel for ignoring key */
int86(0x15, ®, ®);
/* If carry flag is still set, we've finished. */
if(reg.x.cflag)
break;
/* Else waste some time before trying again: */
reg.h.ah = 0;
int86(0x1A, ®, ®); /* system time into CX:DX */
ticks = reg.x.dx;
for (time_to_waste = 3; time_to_waste > 0; )
{
reg.h.ah = 0;
int86(0x1A, ®, ®);
if (ticks != reg.x.dx)
ticks = reg.x.dx , --time_to_waste;
}
}
/* Issue a DOS disk reset request: */
reg.h.ah = 0x0D;
int86(0x21, ®, ®);
/* Set boot type and boot: */
*boottype = (want_warm ? 0x1234 : 0);
(*boot)( );
}
void main()
{
bootme(0);
}
*********
16. CALENDER
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<dos.h>
class calendar
{ private:
struct cal
{ int month,day;
} c;
int a[12];
char months[12][15];
public:
int choice,yr;
calendar(); //constructor
void menu();
void display();
void manipulate();
void firstshow();
} ;
//end of class calendar
/* Constructor for initializing the number of days of each month */
calendar::calendar()
{ a[0]=31; a[1]=28; a[2]=31; a[3]=30; a[4]=31; a[5]=30;
a[6]=31; a[7]=31; a[8]=30; a[9]=31; a[10]=30; a[11]=31;
char* yr_month[12]=
{ " JANUARY"," FEBRUARY"," MARCH"," APRIL"," MAY",
" JUNE"," JULY"," AUGUST"," SEPTEMBER","OCTOBER",
"NOVEMBER","DECEMBER"};
for(int j=0;j<12;j++)
strcpy(months[j],yr_month[j]);
}
/* Displays the list of months */
void calendar::menu()
{cout<<"\n\t\t\t.............................\n\n";
cout<<"\t\t\tSELECT A MONTH FROM THIS LIST\n\n";
for(int i=0;i<12;i++)
cout<<"\n\t < "<<i+1<<" > "<<months[i];
cout<<endl;
cout<<"\n\t < 0 > Exit to the main screen\n";
cout<<"\n\tPlz type the corresponding no. : ";
cin>>choice;
if (choice>12)
{ cout<<"\n\tWRONG CHOICE!TRY AGAIN!!";
delay(1010);
clrscr();
menu();
}
}
/* This function is used to find out which day is the 1st of each month
using the data 1 Jan 2001 ---> Monday */
void calendar::manipulate()
{ c.month=1;
c.day=2;
int y=yr-2001;
if (yr%4==0) a[1]=29;
for(int i=0;i<y;i++)
{if((2001+i)%4==0){ c.day=c.day+2; }
else{ c.day=c.day+1; }
}
for(i=0;i<(choice-1);i++)
{ int x=a[i]%7;
c.day=c.day+x;
}
if (c.day>7)c.day=c.day%7;
if(c.day==0)c.day=7;
}
/*Function for accepting the year */
void calendar::firstshow()
{ clrscr();
cout<<"\n\n\n\n\tTHIS SOFTWARE DISPLAYS THE CALENDER FOR ANY YEAR B/W";
cout<<"\n\t.....................................................";
cout<<"\n\t\t\t\t2001 - 2099\n";
cout<<"\t\t\t\t............\n";
cout<<"\n\n\tPress '0' to exit";
cout<<"\n\n\tPlz type the year : ";
cin>>yr;
if (yr!=0)
{ //exception handling
if((yr>2099)''(yr<2001))
{cout<<"\n\n\tPLEASE TYPE IN A YEAR AS SPECIFIED ABOVE\n";
delay(1100);
clrscr();
firstshow();
}
}
}
/* Function for displaying the calendar for that particular month */
void calendar::display()
{ cout<<"\n\n\t\tCALENDER FOR "<<months[choice-1]<<""<<yr<<"\n";
cout<<"\t\t..................................";
cout<<"\n\n\n\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n";
int k=0;
for(int j=1;j<c.day;j++)
{cout<<"\t ";
k++;
}
for(int i=1;i<=a[choice-1];i++)
{ cout<<"\t"<<i;
k++;
if(k==7) { cout<<"\n\n"; k=0; }
}
}
void main()
{clrscr();
calendar cal;
//object creation for calendar class
cal.yr=2001;
cal.firstshow();
while (cal.yr!=0)
{clrscr();
cal.menu();
while(cal.choice!=0)
{cal.manipulate();
clrscr();
cal.display();
getch();
clrscr();
cal.menu();
}
cal.firstshow();
}
}
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<dos.h>
class calendar
{ private:
struct cal
{ int month,day;
} c;
int a[12];
char months[12][15];
public:
int choice,yr;
calendar(); //constructor
void menu();
void display();
void manipulate();
void firstshow();
} ;
//end of class calendar
/* Constructor for initializing the number of days of each month */
calendar::calendar()
{ a[0]=31; a[1]=28; a[2]=31; a[3]=30; a[4]=31; a[5]=30;
a[6]=31; a[7]=31; a[8]=30; a[9]=31; a[10]=30; a[11]=31;
char* yr_month[12]=
{ " JANUARY"," FEBRUARY"," MARCH"," APRIL"," MAY",
" JUNE"," JULY"," AUGUST"," SEPTEMBER","OCTOBER",
"NOVEMBER","DECEMBER"};
for(int j=0;j<12;j++)
strcpy(months[j],yr_month[j]);
}
/* Displays the list of months */
void calendar::menu()
{cout<<"\n\t\t\t.............................\n\n";
cout<<"\t\t\tSELECT A MONTH FROM THIS LIST\n\n";
for(int i=0;i<12;i++)
cout<<"\n\t < "<<i+1<<" > "<<months[i];
cout<<endl;
cout<<"\n\t < 0 > Exit to the main screen\n";
cout<<"\n\tPlz type the corresponding no. : ";
cin>>choice;
if (choice>12)
{ cout<<"\n\tWRONG CHOICE!TRY AGAIN!!";
delay(1010);
clrscr();
menu();
}
}
/* This function is used to find out which day is the 1st of each month
using the data 1 Jan 2001 ---> Monday */
void calendar::manipulate()
{ c.month=1;
c.day=2;
int y=yr-2001;
if (yr%4==0) a[1]=29;
for(int i=0;i<y;i++)
{if((2001+i)%4==0){ c.day=c.day+2; }
else{ c.day=c.day+1; }
}
for(i=0;i<(choice-1);i++)
{ int x=a[i]%7;
c.day=c.day+x;
}
if (c.day>7)c.day=c.day%7;
if(c.day==0)c.day=7;
}
/*Function for accepting the year */
void calendar::firstshow()
{ clrscr();
cout<<"\n\n\n\n\tTHIS SOFTWARE DISPLAYS THE CALENDER FOR ANY YEAR B/W";
cout<<"\n\t.....................................................";
cout<<"\n\t\t\t\t2001 - 2099\n";
cout<<"\t\t\t\t............\n";
cout<<"\n\n\tPress '0' to exit";
cout<<"\n\n\tPlz type the year : ";
cin>>yr;
if (yr!=0)
{ //exception handling
if((yr>2099)''(yr<2001))
{cout<<"\n\n\tPLEASE TYPE IN A YEAR AS SPECIFIED ABOVE\n";
delay(1100);
clrscr();
firstshow();
}
}
}
/* Function for displaying the calendar for that particular month */
void calendar::display()
{ cout<<"\n\n\t\tCALENDER FOR "<<months[choice-1]<<""<<yr<<"\n";
cout<<"\t\t..................................";
cout<<"\n\n\n\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\n\n";
int k=0;
for(int j=1;j<c.day;j++)
{cout<<"\t ";
k++;
}
for(int i=1;i<=a[choice-1];i++)
{ cout<<"\t"<<i;
k++;
if(k==7) { cout<<"\n\n"; k=0; }
}
}
void main()
{clrscr();
calendar cal;
//object creation for calendar class
cal.yr=2001;
cal.firstshow();
while (cal.yr!=0)
{clrscr();
cal.menu();
while(cal.choice!=0)
{cal.manipulate();
clrscr();
cal.display();
getch();
clrscr();
cal.menu();
}
cal.firstshow();
}
}
*********
17. HEAP OF CIRCLES
// PROGRAM TO MAKE A HEAP OF CIRCLES
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,n;
void draw(int);
char ch;
initgraph(&gd,&gm,"");
start:
cleardevice();
gotoxy(20,5);
printf("enter the no of circles you want to draw : ");
scanf("%d",&n);
if(n<=1)
{
puts("\n\n\t\tplz enter the number greater than 1 ");
getch();
goto start;
}
draw(n);
gotoxy(2,1);
printf("\n\twant more ? Press ENTER for YES ");
ch=getch();
if(ch==13)
goto start;
closegraph();
}
void draw(int n)
{
int x,y,midx,midy,x1,i,j,no=1;
cleardevice();
midx=getmaxx()/2;
midy=60;
y=midy;
setfillstyle(1,2);
for(;n>0;n--)
{
x=x1=midx;
i=j=no;
delay(200-n);
for(;i>0;i--)
{
circle(x1,y,10); //left
floodfill(x1,y,15);
x1-=20;
}
for(;j>0;j--)
{
circle(x,y,10); //right
floodfill(x,y,15);
x+=20;
}
no++;
y+=20;
}
}
// PROGRAM TO MAKE A HEAP OF CIRCLES
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,n;
void draw(int);
char ch;
initgraph(&gd,&gm,"");
start:
cleardevice();
gotoxy(20,5);
printf("enter the no of circles you want to draw : ");
scanf("%d",&n);
if(n<=1)
{
puts("\n\n\t\tplz enter the number greater than 1 ");
getch();
goto start;
}
draw(n);
gotoxy(2,1);
printf("\n\twant more ? Press ENTER for YES ");
ch=getch();
if(ch==13)
goto start;
closegraph();
}
void draw(int n)
{
int x,y,midx,midy,x1,i,j,no=1;
cleardevice();
midx=getmaxx()/2;
midy=60;
y=midy;
setfillstyle(1,2);
for(;n>0;n--)
{
x=x1=midx;
i=j=no;
delay(200-n);
for(;i>0;i--)
{
circle(x1,y,10); //left
floodfill(x1,y,15);
x1-=20;
}
for(;j>0;j--)
{
circle(x,y,10); //right
floodfill(x,y,15);
x+=20;
}
no++;
y+=20;
}
}
*********
18. DRAW A HUT IN GRAPHICS
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cleardevice();
fflush(stdout);
line(200,150,350,150);
line(140,200,200,150);
line(140,330,140,200);
line(250,200,140,200);
line(200,150,250,200);
circle(196,180,8);
setfillstyle(2,4);
floodfill(196,180,15);
setfillstyle(1,2);
line(350,150,400,200);
floodfill(210,180,15);
line(400,200,400,330);
line(140,330,400,330);
line(250,200,250,330);
line(250,200,400,200); //hut
setfillstyle(5,7);
floodfill(260,180,15);
line(170,260,170,330);
line(170,260,210,260);
setfillstyle(10,9);
floodfill(180,250,15);
line(210,260,210,330);
setfillstyle(9,9);
floodfill(210,250,15);
line(290,110,290,150);
line(310,110,310,150);
ellipse(300,110,0,360,10,3); //chimney
setfillstyle(6,8);
floodfill(300,120,15);
line(300,250,350,250);
line(300,280,350,280);
line(350,250,350,280);
line(300,280,300,250);
setfillstyle(9,9);
floodfill(252,300,15);
setfillstyle(8,9);
floodfill(342,270,15);
setcolor(4);
settextstyle(7,0,5);
outtextxy(100,390,"Home Sweet Home");
getch();
closegraph();
}
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
cleardevice();
fflush(stdout);
line(200,150,350,150);
line(140,200,200,150);
line(140,330,140,200);
line(250,200,140,200);
line(200,150,250,200);
circle(196,180,8);
setfillstyle(2,4);
floodfill(196,180,15);
setfillstyle(1,2);
line(350,150,400,200);
floodfill(210,180,15);
line(400,200,400,330);
line(140,330,400,330);
line(250,200,250,330);
line(250,200,400,200); //hut
setfillstyle(5,7);
floodfill(260,180,15);
line(170,260,170,330);
line(170,260,210,260);
setfillstyle(10,9);
floodfill(180,250,15);
line(210,260,210,330);
setfillstyle(9,9);
floodfill(210,250,15);
line(290,110,290,150);
line(310,110,310,150);
ellipse(300,110,0,360,10,3); //chimney
setfillstyle(6,8);
floodfill(300,120,15);
line(300,250,350,250);
line(300,280,350,280);
line(350,250,350,280);
line(300,280,300,250);
setfillstyle(9,9);
floodfill(252,300,15);
setfillstyle(8,9);
floodfill(342,270,15);
setcolor(4);
settextstyle(7,0,5);
outtextxy(100,390,"Home Sweet Home");
getch();
closegraph();
}
19. EXAMPLE OF A FULL CLASS DECLARATION
//Here is an example of a full class declaration :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double a);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (x * a, y * a);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
double length = this->module();
x = x / length * a;
y = y / length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector a;
vector b;
vector c (3, 5);
a = c * 3;
a = b + c;
c = b - c + a + (b - a) * 7;
c = -c;
cout << "The module of vector c : " << c.module() << endl;
cout << "The content of vector a : " << a << endl;
cout << "The oposite of vector a : " << -a << endl;
c.set_length(2); // Transforms c in a vector of size 2.
a = vector (56, -3);
b = vector (7, c.y);
b.set_length(); // Transforms b in an unitary vector.
cout << "The content of vector b : " << b << endl;
double k;
k = vector(1, 1).module(); // k will contain 1.4142.
cout << "k contains : " << k << endl;
}
//Here is an example of a full class declaration :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double = 0, double = 0);
vector operator + (vector);
vector operator - (vector);
vector operator - ();
vector operator * (double a);
double module();
void set_length (double = 1);
};
vector::vector (double a, double b)
{
x = a;
y = b;
}
vector vector::operator + (vector a)
{
return vector (x + a.x, y + a.y);
}
vector vector::operator - (vector a)
{
return vector (x - a.x, y - a.y);
}
vector vector::operator - ()
{
return vector (-x, -y);
}
vector vector::operator * (double a)
{
return vector (x * a, y * a);
}
double vector::module()
{
return sqrt (x * x + y * y);
}
void vector::set_length (double a)
{
double length = this->module();
x = x / length * a;
y = y / length * a;
}
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector a;
vector b;
vector c (3, 5);
a = c * 3;
a = b + c;
c = b - c + a + (b - a) * 7;
c = -c;
cout << "The module of vector c : " << c.module() << endl;
cout << "The content of vector a : " << a << endl;
cout << "The oposite of vector a : " << -a << endl;
c.set_length(2); // Transforms c in a vector of size 2.
a = vector (56, -3);
b = vector (7, c.y);
b.set_length(); // Transforms b in an unitary vector.
cout << "The content of vector b : " << b << endl;
double k;
k = vector(1, 1).module(); // k will contain 1.4142.
cout << "k contains : " << k << endl;
}
*********
20. DECLARE ARRAYS OF OBJECTS
// it is possible to declare arrays of objects :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module ()
{
return sqrt (x * x + y * y);
}
};
void main ()
{
vector s[1000];
vector t[3] = {vector(4, 5), vector(5, 5), vector(2, 4)};
s[23] = t[2];
cout << t[0].module() << endl;
}
// it is possible to declare arrays of objects :
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module ()
{
return sqrt (x * x + y * y);
}
};
void main ()
{
vector s[1000];
vector t[3] = {vector(4, 5), vector(5, 5), vector(2, 4)};
s[23] = t[2];
cout << t[0].module() << endl;
}
*********
21. PROGRAMME FOR CONSTRUCTOR "VECTOR" WITH ARGUMENTS
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x * x + y * y);
}
void set_length (double a = 1)
{
double length;
length = this->module();
x = x / length * a;
y = y / length * a;
}
};
void main ()
{
vector c (3, 5);
cout << "The module of vector c : " << c.module() << endl;
c.set_length(2); // Transforms c in a vector of size 2.
cout << "The module of vector c : " << c.module() << endl;
c.set_length(); // Transforms b in an unitary vector.
cout << "The module of vector c : " << c.module() << endl;
}
#include <iostream.h>
#include <math.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
double module()
{
return sqrt (x * x + y * y);
}
void set_length (double a = 1)
{
double length;
length = this->module();
x = x / length * a;
y = y / length * a;
}
};
void main ()
{
vector c (3, 5);
cout << "The module of vector c : " << c.module() << endl;
c.set_length(2); // Transforms c in a vector of size 2.
cout << "The module of vector c : " << c.module() << endl;
c.set_length(); // Transforms b in an unitary vector.
cout << "The module of vector c : " << c.module() << endl;
}
*********
22. PROGRAMME ON CLASS "VECTOR" WITH INFORMATION:THE ; AND NO {} SHOWS A PROTOTYPE
#include <iostream.h>
class vector
{
public:
double x;
double y;
double surface(); // The ; and no {} shows it is a prototype
};
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
void main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface : " << k.surface() << endl;
}
#include <iostream.h>
class vector
{
public:
double x;
double y;
double surface(); // The ; and no {} shows it is a prototype
};
double vector::surface()
{
double s = 0;
for (double i = 0; i < x; i++)
{
s = s + y;
}
return s;
}
void main ()
{
vector k;
k.x = 4;
k.y = 5;
cout << "Surface : " << k.surface() << endl;
}
*********
23. A PROGRAMME FOR STARTING KNOWLEDGE ABOUT THE COPY CONSTRUCTOR
#include<iostream.h>
#include <string.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100];
strcpy (name, n);
age = a;
}
person (person &s) // The COPY CONSTRUCTOR
{
strcpy (name, s.name);
age = s.age;
}
~person ()
{
delete (name);
}
};
void main ()
{
person p;
cout << p.name << ", age " << p.age << endl << endl;
person k ("John", 56);
cout << k.name << ", age " << k.age << endl << endl;
p = k;
cout << p.name << ", age " << p.age << endl << endl;
p = person ("Bob", 10);
cout << p.name << ", age " << p.age << endl << endl;
}
#include<iostream.h>
#include <string.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100];
strcpy (name, n);
age = a;
}
person (person &s) // The COPY CONSTRUCTOR
{
strcpy (name, s.name);
age = s.age;
}
~person ()
{
delete (name);
}
};
void main ()
{
person p;
cout << p.name << ", age " << p.age << endl << endl;
person k ("John", 56);
cout << k.name << ", age " << k.age << endl << endl;
p = k;
cout << p.name << ", age " << p.age << endl << endl;
p = person ("Bob", 10);
cout << p.name << ", age " << p.age << endl << endl;
}
*********
24. IN CASE OF DYNAMIC MEMORY ALLOCATION ::NEW IS BETTER THAN MALLOC
#include <iostream.h>
#include <string.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100]; // better than malloc !
strcpy (name, n);
age = a;
cout << "Instance initialized, 100 bytes allocated" << endl;
}
~person () // The destructor
{
delete (name); // instead of free !
cout << "Instance going to be deleted, 100 bytes freed" << endl;
}
};
void main ()
{
cout << "Hello !" << endl << endl;
person a;
cout << a.name << ", age " << a.age << endl << endl;
person b ("John");
cout << b.name << ", age " << b.age << endl << endl;
b.age = 21;
cout << b.name << ", age " << b.age << endl << endl;
person c ("Miki", 45);
cout << c.name << ", age " << c.age << endl << endl;
cout << "Bye !" << endl << endl;
}
#include <iostream.h>
#include <string.h>
class person
{
public:
char *name;
int age;
person (char *n = "no name", int a = 0)
{
name = new char[100]; // better than malloc !
strcpy (name, n);
age = a;
cout << "Instance initialized, 100 bytes allocated" << endl;
}
~person () // The destructor
{
delete (name); // instead of free !
cout << "Instance going to be deleted, 100 bytes freed" << endl;
}
};
void main ()
{
cout << "Hello !" << endl << endl;
person a;
cout << a.name << ", age " << a.age << endl << endl;
person b ("John");
cout << b.name << ", age " << b.age << endl << endl;
b.age = 21;
cout << b.name << ", age " << b.age << endl << endl;
person c ("Miki", 45);
cout << c.name << ", age " << c.age << endl << endl;
cout << "Bye !" << endl << endl;
}
*********
25. DECLARE ONLY ONE CONSTRUCTOR AND GIVE IT DEFAULT PARAMETERS WHEREVER POSSIBLE INSTEAD OF OVERLOADING CONSTRUCTORS
//It is a good practice to try not to overload the constructors.
//Best is to declare only one constructor and give it default parameters
//wherever possible :
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
};
void main ()
{
vector k;
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
vector m (45, 2);
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
vector p (3);
cout << "vector p : " << p.x << ", " << p.y << endl << endl;
}
//It is a good practice to try not to overload the constructors.
//Best is to declare only one constructor and give it default parameters
//wherever possible :
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector (double a = 0, double b = 0)
{
x = a;
y = b;
}
};
void main ()
{
vector k;
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
vector m (45, 2);
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
vector p (3);
cout << "vector p : " << p.x << ", " << p.y << endl << endl;
}
*********
26. EXAMPLE OF A CLASS DEFINITION WITH TWO OVERLOADED CONSTRUCTORS
//Here is an example of a class definition with two overloaded constructors.
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector () // same name as class
{
x = 0;
y = 0;
}
vector (double a, double b)
{
x = a;
y = b;
}
};
void main ()
{
vector k; // vector () is called
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
vector m (45, 2); // vector (double, double) is called
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
k = vector (23, 2); // vector created, copied to k, then erased
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
}
//Here is an example of a class definition with two overloaded constructors.
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector () // same name as class
{
x = 0;
y = 0;
}
vector (double a, double b)
{
x = a;
y = b;
}
};
void main ()
{
vector k; // vector () is called
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
vector m (45, 2); // vector (double, double) is called
cout << "vector m : " << m.x << ", " << m.y << endl << endl;
k = vector (23, 2); // vector created, copied to k, then erased
cout << "vector k : " << k.x << ", " << k.y << endl << endl;
}
*********
27. METHOD IS ALLOWED TO CHANGE THE VARIABLES OF THE INSTANCE IT IS ACTING UPON
//A method is allowed to change the variables of the instance it is acting upon :
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector its_oposite()
{
vector r;
r.x = -x;
r.y = -y;
return r;
}
void be_oposited()
{
x = -x;
y = -y;
}
void be_calculated (double a, double b, double c, double d)
{
x = a - c;
y = b - d;
}
vector operator * (double a)
{
vector r;
r.x = x * a;
r.y = y * a;
return r;
}
};
void main (void)
{
vector a, b;
a.x = 3;
b.y = 5;
b = a.its_oposite();
cout << "Vector a : " << a.x << ", " << a.y << endl;
cout << "Vector b : " << b.x << ", " << b.y << endl;
b.be_oposited();
cout << "Vector b : " << b.x << ", " << b.y << endl;
a.be_calculated (7, 8, 3, 2);
cout << "Vector a : " << a.x << ", " << a.y << endl;
a = b * 2;
cout << "Vector a : " << a.x << ", " << a.y << endl;
a = b.its_oposite() * 2;
cout << "Vector a : " << a.x << ", " << a.y << endl;
cout << "x of oposite of a : " << a.its_oposite().x << endl;
}
//A method is allowed to change the variables of the instance it is acting upon :
#include <iostream.h>
class vector
{
public:
double x;
double y;
vector its_oposite()
{
vector r;
r.x = -x;
r.y = -y;
return r;
}
void be_oposited()
{
x = -x;
y = -y;
}
void be_calculated (double a, double b, double c, double d)
{
x = a - c;
y = b - d;
}
vector operator * (double a)
{
vector r;
r.x = x * a;
r.y = y * a;
return r;
}
};
void main (void)
{
vector a, b;
a.x = 3;
b.y = 5;
b = a.its_oposite();
cout << "Vector a : " << a.x << ", " << a.y << endl;
cout << "Vector b : " << b.x << ", " << b.y << endl;
b.be_oposited();
cout << "Vector b : " << b.x << ", " << b.y << endl;
a.be_calculated (7, 8, 3, 2);
cout << "Vector a : " << a.x << ", " << a.y << endl;
a = b * 2;
cout << "Vector a : " << a.x << ", " << a.y << endl;
a = b.its_oposite() * 2;
cout << "Vector a : " << a.x << ", " << a.y << endl;
cout << "x of oposite of a : " << a.its_oposite().x << endl;
}
*********
28. ALLOCATION OF MEMORY AND DEALLOCATION OF MEMORY DURING RUN-TIME
#include <iostream.h>
#include <string.h>
void main ()
{
double *d; // d is a variable whose purpose
// is to contain the address of a
// zone where a double is located
d = new double; // new allocates a zone of memory
// large enough to contain a double
// and returns its address.
// That address is stored in d.
*d = 45.3; // The number 45.3 is stored
// inside the memory zone
// whose address is given by d.
cout << "Type a number : ";
cin >> *d;
*d = *d + 5;
cout << "Result : " << *d << endl;
delete (d); // delete deallocates the
// zone of memory whose address
// is given by pointer d.
// Now we can no more use that zone.
d = new double[15]; // allocates a zone for an array
// of 15 doubles
d[0] = 4456;
d[1] = d[0] +567;
cout << "Content of d[1] : " << d[1] << endl;
delete (d);
int n = 30;
d = new double[n]; // new can be used to allocate an
// array of random size.
for (int i = 0; i < n; i++)
{
d[i] = i;
}
delete (d);
char *s;
s = new char[100];
strcpy (s, "Hello !");
cout << s << endl;
delete (s);
}
#include <iostream.h>
#include <string.h>
void main ()
{
double *d; // d is a variable whose purpose
// is to contain the address of a
// zone where a double is located
d = new double; // new allocates a zone of memory
// large enough to contain a double
// and returns its address.
// That address is stored in d.
*d = 45.3; // The number 45.3 is stored
// inside the memory zone
// whose address is given by d.
cout << "Type a number : ";
cin >> *d;
*d = *d + 5;
cout << "Result : " << *d << endl;
delete (d); // delete deallocates the
// zone of memory whose address
// is given by pointer d.
// Now we can no more use that zone.
d = new double[15]; // allocates a zone for an array
// of 15 doubles
d[0] = 4456;
d[1] = d[0] +567;
cout << "Content of d[1] : " << d[1] << endl;
delete (d);
int n = 30;
d = new double[n]; // new can be used to allocate an
// array of random size.
for (int i = 0; i < n; i++)
{
d[i] = i;
}
delete (d);
char *s;
s = new char[100];
strcpy (s, "Hello !");
cout << s << endl;
delete (s);
}
*********
29. OPERATOR OVERLOADING OF OPERATOR"<<"
//operator overloading
#include <iostream.h>
struct vector
{
double x;
double y;
};
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector a;
a.x = 35;
a.y = 23;
cout << a << endl;
}
//operator overloading
#include <iostream.h>
struct vector
{
double x;
double y;
};
ostream& operator << (ostream& o, vector a)
{
o << "(" << a.x << ", " << a.y << ")";
return o;
}
void main ()
{
vector a;
a.x = 35;
a.y = 23;
cout << a << endl;
}
*********
30. "OPERATORS OVERLOAD" CAN BE USED TO DEFINE THE BASIC SYMBOLIC OPERATORS FOR NEW SORTS OF PARAMETERS
//The "operators overload" can be used to define the basic
//symbolic operators for new sorts of parameters :
#include <iostream.h>
struct vector
{
double x;
double y;
};
vector operator * (double a, vector b)
{
vector r;
r.x = a * b.x;
r.y = a * b.y;
return r;
}
void main ()
{
vector k, m; // No need to type "struct vector"
k.x = 2; // Keep cool, soon you'll be able
k.y = -1; // to write k = vector (45, -4).
m = 3.1415927 * k; // Magic !
cout << "(" << m.x << ", " << m.y << ")" << endl;
}
//The "operators overload" can be used to define the basic
//symbolic operators for new sorts of parameters :
#include <iostream.h>
struct vector
{
double x;
double y;
};
vector operator * (double a, vector b)
{
vector r;
r.x = a * b.x;
r.y = a * b.y;
return r;
}
void main ()
{
vector k, m; // No need to type "struct vector"
k.x = 2; // Keep cool, soon you'll be able
k.y = -1; // to write k = vector (45, -4).
m = 3.1415927 * k; // Magic !
cout << "(" << m.x << ", " << m.y << ")" << endl;
}
*********
31. IT IS POSSIBLE TO DEFINE DEFAULT PARAMETERS FOR FUNCTIONS
//It is possible to define default parameters for functions :
#include <iostream.h>
double test (double a, double b = 7)
{
return a - b;
}
void main ()
{
cout << test (14, 5) << endl;
cout << test (14) << endl;
}
//It is possible to define default parameters for functions :
#include <iostream.h>
double test (double a, double b = 7)
{
return a - b;
}
void main ()
{
cout << test (14, 5) << endl;
cout << test (14) << endl;
}
*********
32. REFERENCES ARE VERY USEFUL TO UN-POINTER VARIABLES
//To end with, for people who have to deal with pointers
//yet do not like it, references are very useful to un-pointer variables :
#include <iostream.h>
double *silly_function () // This function returns a pointer to a double
{
static double r = 342;
return &r;
}
void main()
{
double *a;
a = silly_function();
double &b = *a; // Now b IS the double towards which a points !
b += 1; // Great !
b = b * b; // No need to write *a everywhere !
b += 4;
cout << "Content of *a, b, r : " << b << endl;
}
//To end with, for people who have to deal with pointers
//yet do not like it, references are very useful to un-pointer variables :
#include <iostream.h>
double *silly_function () // This function returns a pointer to a double
{
static double r = 342;
return &r;
}
void main()
{
double *a;
a = silly_function();
double &b = *a; // Now b IS the double towards which a points !
b += 1; // Great !
b = b * b; // No need to write *a everywhere !
b += 4;
cout << "Content of *a, b, r : " << b << endl;
}
*********
33. REFERENCE CAN BE USED TO LET A FUNCTION RETURN A VARIABLE
//A reference can be used to let a function return a variable :
#include <iostream.h>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else return s;
}
void main ()
{
double k = 3;
double m = 7;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
biggest (k, m) = 10;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
biggest (k, m) ++;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
}
//A reference can be used to let a function return a variable :
#include <iostream.h>
double &biggest (double &r, double &s)
{
if (r > s) return r;
else return s;
}
void main ()
{
double k = 3;
double m = 7;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
biggest (k, m) = 10;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
biggest (k, m) ++;
cout << "k : " << k << endl;
cout << "m : " << m << endl;
cout << endl;
}
*********
34. AN IMPORTANT PROGRAMME ON POINTERS
//If you are used at pointers in C and wonder how exactly the program
//above works, here is how the C++ compiler translates
#include <iostream.h>
void change (double *r, double s)
{
*r = 100;
s = 200;
}
void main ()
{
double k, m;
k = 3;
m = 4;
change (&k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
}
//If you are used at pointers in C and wonder how exactly the program
//above works, here is how the C++ compiler translates
#include <iostream.h>
void change (double *r, double s)
{
*r = 100;
s = 200;
}
void main ()
{
double k, m;
k = 3;
m = 4;
change (&k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
}
*********
35. REFERENCES CAN BE USED TO ALLOW A FUNCTION TO MODIFY A CALLING VARIABLE
//References can be used to allow a function to modify a calling variable :
#include <iostream.h>
void change (double &r, double s)
{
r = 100;
s = 200;
}
void main ()
{
double k, m;
k = 3;
m = 4;
change (k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
}
//References can be used to allow a function to modify a calling variable :
#include <iostream.h>
void change (double &r, double s)
{
r = 100;
s = 200;
}
void main ()
{
double k, m;
k = 3;
m = 4;
change (k, m);
cout << k << ", " << m << endl; // Displays 100, 4.
}
*********
36. IT IS POSSIBLE TO MAKE ONE VARIABLE BE ANOTHER
//It is possible to make one variable be another :
#include <iostream.h>
void main ()
{
double a = 3.1415927;
double &b = a; // b IS a
b = 89;
cout << "a contains : " << a << endl; // Displays 89.
}
//It is possible to make one variable be another :
#include <iostream.h>
void main ()
{
double a = 3.1415927;
double &b = a; // b IS a
b = 89;
cout << "a contains : " << a << endl; // Displays 89.
}
*********
37. IMPORTANT POINT ON GLOBAL VS LOCAL VARIABLE
//A global variable can be accessed even if another variable with the same
//name has been declared inside the function :
#include <iostream.h>
double a = 128;
void main ()
{
double a = 256;
cout << "Local a : " << a << endl;
cout << "Global a : " << ::a << endl;
}
//A global variable can be accessed even if another variable with the same
//name has been declared inside the function :
#include <iostream.h>
double a = 128;
void main ()
{
double a = 256;
cout << "Local a : " << a << endl;
cout << "Global a : " << ::a << endl;
}
*********
38. IMPORTANT POINT ON VARIABLE DECLARATION
//C++ allows to declare a variable inside the for loop declaration.
//It's like if the variable had been declared just before the loop :
#include <iostream.h>
void main ()
{
for (int i = 0; i < 4; i++)
{
cout << i << endl;
}
cout << "i contains : " << i << endl;
for (i = 0; i < 4; i++)
{
for (int i = 0; i < 4; i++) // we're between
{ // previous for's hooks
cout << i;
}
cout << endl;
}
}
//C++ allows to declare a variable inside the for loop declaration.
//It's like if the variable had been declared just before the loop :
#include <iostream.h>
void main ()
{
for (int i = 0; i < 4; i++)
{
cout << i << endl;
}
cout << "i contains : " << i << endl;
for (i = 0; i < 4; i++)
{
for (int i = 0; i < 4; i++) // we're between
{ // previous for's hooks
cout << i;
}
cout << endl;
}
}
*********
39. LIKE IN C, VARIABLES CAN BE ENCAPSULATED BETWEEN HOOKS
//Like in C, variables can be encapsulated between hooks :
#include <iostream.h>
void main()
{
double a;
cout << "Type a number : ";
cin >> a;
{
int a = 1;
a = a * 10 + 4;
cout << "Local number : " << a << endl;
}
cout << "You typed : " << a << endl;
}
//Like in C, variables can be encapsulated between hooks :
#include <iostream.h>
void main()
{
double a;
cout << "Type a number : ";
cin >> a;
{
int a = 1;
a = a * 10 + 4;
cout << "Local number : " << a << endl;
}
cout << "You typed : " << a << endl;
}
*********
40. A VARIABLE CAN BE INITIALIZED BY A CALCULATION
//A variable can be initialized by a calculation :
#include <iostream.h>
void main ()
{
double a = 12 * 3.25;
double b = a + 1.112;
cout << "a contains : " << a << endl;
cout << "b contains : " << b << endl;
a = a * 2 + b;
double c = a + b * a;
cout << "c contains : " << c << endl;
}
//A variable can be initialized by a calculation :
#include <iostream.h>
void main ()
{
double a = 12 * 3.25;
double b = a + 1.112;
cout << "a contains : " << a << endl;
cout << "b contains : " << b << endl;
a = a * 2 + b;
double c = a + b * a;
cout << "c contains : " << c << endl;
}
*********
41. VARIABLES CAN BE DECLARED EVERYWHERE
//Variables can be declared everywhere :
#include <iostream.h>
void main ()
{
double a;
cout << "Hello, this is a test program." << endl;
cout << "Type parameter a : ";
cin >> a;
a = (a + 1) / 2;
double c;
c = a * 5 + 1;
cout << "c contains : " << c << endl;
int i, j;
i = 0;
j = i + 1;
cout << "j contains : " << j << endl;
}
//Variables can be declared everywhere :
#include <iostream.h>
void main ()
{
double a;
cout << "Hello, this is a test program." << endl;
cout << "Type parameter a : ";
cin >> a;
a = (a + 1) / 2;
double c;
c = a * 5 + 1;
cout << "c contains : " << c << endl;
int i, j;
i = 0;
j = i + 1;
cout << "j contains : " << j << endl;
}
*********
42. INPUT FROM KEYBOARD AND OUTPUT TO SCREEN CAN BE PERFORMED THROUGH COUT AND CIN
//Input from keyboard and output to screen can be performed through cout and cin :
#include <iostream.h>
void main()
{
int a; // a is an integer variable
char s [100]; // s points to a string of 99 characters
cout << "This is a sample program." << endl;
cout << endl; // Line feed (end of line)
cout << "Type your age : ";
cin >> a;
cout << "Type your name : ";
cin >> s;
cout << endl;
cout << "Hello " << s << " you're " << a << " old." << endl;
cout << endl << endl << "Bye !" << endl;
}
//Input from keyboard and output to screen can be performed through cout and cin :
#include <iostream.h>
void main()
{
int a; // a is an integer variable
char s [100]; // s points to a string of 99 characters
cout << "This is a sample program." << endl;
cout << endl; // Line feed (end of line)
cout << "Type your age : ";
cin >> a;
cout << "Type your name : ";
cin >> s;
cout << endl;
cout << "Hello " << s << " you're " << a << " old." << endl;
cout << endl << endl << "Bye !" << endl;
}
*********
43. PROGRAMME FOR USE OF ,//,
//You can use / / to type a remark :
#include <iostream.h> // This library is often used
void main () // The program's main routine.
{
double a; // Declaration of variable a.
a = 456;
a = a + a * 21.5 / 100; // A calculation.
cout << a; // Display the content of a.
}
//You can use / / to type a remark :
#include <iostream.h> // This library is often used
void main () // The program's main routine.
{
double a; // Declaration of variable a.
a = 456;
a = a + a * 21.5 / 100; // A calculation.
cout << a; // Display the content of a.
}
*********
44. A SIMPLE EXAMPLE OF USE OF FPUTCHAR() FUNCTION
#include <stdio.h>
int main(void)
{
char msg[] = "This is a test";
int i = 0;
while (msg[i])
{
fputchar(msg[i]);
i++;
}
return 0;
}
#include <stdio.h>
int main(void)
{
char msg[] = "This is a test";
int i = 0;
while (msg[i])
{
fputchar(msg[i]);
i++;
}
return 0;
}
output
int main()
{
char a[10]={'mayur'}
for(int i=0;i<=10;i++)
{
cout<<a[i];
cout<<"\n";
}
getch();
return 0;
}
int main()
{
char a[10]={'mayur'}
for(int i=0;i<=10;i++)
{
cout<<a[i];
cout<<"\n";
}
getch();
return 0;
}
*********
45. PALINDROME
int main()
{
int n;
char a[20];
cout<<"enter how many digit string u want 2 enter";
cin>>n;
cout<<"enter the string";
cin>>a;
for(inti=0;i<=n;i++)
{
for(int j=n;j>=0;j--)
if (a[i]==a[j])
s=0;
else
s=1;
}
if(s==0)
cout<<"no.is palindrome ";
else
cout<<"no. is not palindrome";
getch()
return 0;
}
int main()
{
int n;
char a[20];
cout<<"enter how many digit string u want 2 enter";
cin>>n;
cout<<"enter the string";
cin>>a;
for(inti=0;i<=n;i++)
{
for(int j=n;j>=0;j--)
if (a[i]==a[j])
s=0;
else
s=1;
}
if(s==0)
cout<<"no.is palindrome ";
else
cout<<"no. is not palindrome";
getch()
return 0;
}
*********
46. EVEN NO. PROGRAM
int main()
{
int n;
cout<<"enter the number";
cin>>n;
if(n%2==0)
cout<<"number entered is even";
else
cout<<"number is not even";
getch();
return 0;
}
int main()
{
int n;
cout<<"enter the number";
cin>>n;
if(n%2==0)
cout<<"number entered is even";
else
cout<<"number is not even";
getch();
return 0;
}
*********
47. INITIALS
int main()
{
char a[20];
cout<<"enter ur name";
cin>>a
for(int i=1;i<=10;i++)
{
cout<<a;
}
getch();
}
int main()
{
char a[20];
cout<<"enter ur name";
cin>>a
for(int i=1;i<=10;i++)
{
cout<<a;
}
getch();
}
*********
48. MATRIX
int main()
{
int a[3][3]={1,1,1,1,1,1,1,1,1};
int b[3][3]={2,2,2,2,2,2,2,2,2}
int c[3][3]={0,0,0,0,0,0,0,0,0};
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
c[i][j]=a[i][j]+ b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<\n;
}
getch();
}
int main()
{
int a[3][3]={1,1,1,1,1,1,1,1,1};
int b[3][3]={2,2,2,2,2,2,2,2,2}
int c[3][3]={0,0,0,0,0,0,0,0,0};
for(int i=1;i<=3;i++)
{
for(int j=1;j<=3;j++)
{
c[i][j]=a[i][j]+ b[i][j];
cout<<c[i][j]<<"\t";
}
cout<<\n;
}
getch();
}
class
class m
{
public void show()
{
cout<<"my name is mayur";
}
};
int main()
{
m a;
a.show();
getch();
}
class m
{
public void show()
{
cout<<"my name is mayur";
}
};
int main()
{
m a;
a.show();
getch();
}
*********
0 comments:
Post a Comment