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

1) A simple class called Point, with all necessary functions

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

class point
{
int x,y,z;
public:
point()
{
x=y=z=0;
}
point(int i,int j,int k)
{
x=i;
y=j;
z=k;
}
point(point &a)
{
x=a.x;
y=a.y;
z=a.z;
}

negate()
{
x=-x;
y=-y;
z=-z;
}
void print()
{
cout<<"("<<x<<","<<y<<","<<z<<")";
}
int norm()
{
return(sqrt(x*x+y*y+z*z));
}
};
void main()
{
clrscr();
point p(2,3,4),p1(p);

cout<<"The point has the coordinates ";


p.print();
cout<<"
The point coordinates after negation ";
p.negate();
p.print();
cout<<"Normal Distance of the point from (0,0,0) is "<<p.norm();
cout<<"The coordinates of the point p1 after copy constructor ";
p1.print();
getch();}
2) Accessing Private Data Members in C++. This is a flaw in the
language

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

class bestcoder
{
private:

char name[40];
char grade;
int age;

public:

bestcoder(char* nam="Some Dude",char gr='A',int saal=25)


{
strcpy(name,nam);
grade=gr;
age=saal;
}

friend ostream& operator <<(ostream& s,bestcoder b);


};

ostream& operator <<(ostream& s,bestcoder b)


{
s<<"Best Coder :"<<b.name<<endl
<<"His Rating :"<<b.grade<<endl
<<"Current Age :"<<b.age<<"

";
return s;
}

struct hackit
{
char name[40];
char grade;
char age;
};

void main()
{
bestcoder bc;
cout<<bc;
void* ptr=&bc;
struct hackit* bettercoder=(hackit*)ptr;
bettercoder->grade='F';
bettercoder->age=56;
cout<<bc;
strcpy(bettercoder->name,"xxx");
bettercoder->age=14;
bettercoder->grade='A';
cout<<bc;
getch();}
3) Class for Library

# include<iostream.h>
# include<conio.h>
// Creating a basic template for book and magazine
class lib
{
private:
char title[20];
char pub[20];
unsigned int acc_no;
public:
//method for getting inputs
void get_details()
{
cout<<"Enter the book title"<<endl;
cin>>title;
cout<<"Enter the publisher name"<<endl;
cin>>pub;
cout<<"Enter the accession number"<<endl;
cin>>acc_no;
}
//method for showing output
void show_details()
{
cout<<"Title : "<<title<<endl;
cout<<"Publisher : "<<pub<<endl;
cout<<"Accession No. : "<<acc_no<<endl;
}
};
// Class Book derived from lib
class book : private lib
{
private:
char author[20];
public:
void get_details()
{
lib::get_details();
cout<<"Enter the author's name: "<<endl;
cin>>author;
}
void show_details()
{
lib::show_details();
cout<<"Autohr : "<<author<<endl;
}
};
//Class for Magazine derived from lib
class magz : private lib
{
private:
char editor[20];
public:
void get_details()
{
lib::get_details();
cout<<"Enter the editor's name: "<<endl;
cin>>editor;
}
void show_details()
{
lib::show_details();
cout<<"editor : "<<editor<<endl;
}
};
void main(void)
{
clrscr();
//creating objects
book b;
magz m;
b.get_details();
m.get_details();
b.show_details();
m.show_details();
getch();
}
4) Convert a number into words

#include <conio.h> // For getch() function only


#include <iostream>
using namespace std;

void numword1(int);
void numword2(int);

int main()
{
long unsigned int number,temp;
int mult,i,digit,digits,last_two,hundred,thousand,lakh,crore;
digits=last_two=hundred=thousand=lakh=crore=0;

cout<<"Enter a number(lesser than 99,99,99,999)


";
cin>>number;

if(number>999999999)
{
cout<<"Number out of range!";
getch();
exit(0);
}

if(number==0)
{
cout<<"Zero";
getch();
exit(0);
}

temp=number;

digit=number%10; // Extracting last two digts


last_two=digit;
number=number/10;
digit=number%10;
last_two=(digit*10)+last_two;

number=number/10; // Extract hundreds


digit=number%10;
hundred=digit;

number=number/10; // Extract thousands


digit=number%10;
thousand=digit;
number=number/10;
digit=number%10;
thousand=(digit*10)+thousand;

number=number/10; // Extract lakhs


digit=number%10;
lakh=digit;
number=number/10;
digit=number%10;
lakh=(digit*10)+lakh;

number=number/10; // Extract crores


digit=number%10;
crore=digit;
number=number/10;
digit=number%10;
crore=(digit*10)+crore;

while(temp!=0) // Calculate number of digits in


the number
{
temp=temp/10;
digits++;
}

cout<<"The number in words is:


";

// Printing the number in words

if(digits>=8)
{
numword2(crore);
cout<<"crores ";
}
if(digits>=6)
{
if(lakh!=0)
{
numword2(lakh);
cout<<"lakh ";
}
}
if(digits>=4)
{
if(thousand!=0)
{
numword2(thousand);
cout<<"Thousand ";
}
}
if(digits>=3)
{
if(hundred!=0)
{
numword2(hundred);
cout<<"Hundred ";
}
}

numword2(last_two);

getch();
return 0;
}
void numword1(int num)
{
switch(num)
{
case 0: break;

case 1: cout<<"One ";


break;
case 2: cout<<"Two ";
break;
case 3: cout<<"Three ";
break;
case 4: cout<<"Four ";
break;
case 5: cout<<"Five ";
break;
case 6: cout<<"Six ";
break;
case 7: cout<<"Seven ";
break;
case 8: cout<<"Eight ";
break;
case 9: cout<<"Nine ";
break;
case 10: cout<<"Ten ";
break;
case 11: cout<<"Eleven ";
break;
case 12: cout<<"Twelve ";
break;
case 13: cout<<"Thirteen ";
break;
case 14: cout<<"Fourteen ";
break;
case 15: cout<<"Fifteen ";
break;
case 16: cout<<"Sixteen ";
break;
case 17: cout<<"Seventeen ";
break;
case 18: cout<<"Eighteen ";
break;
case 19: cout<<"Nineteen ";
break;
}
return;
}

void numword2(int num)


{
if(num>=90)
{
cout<<"Ninety ";
numword1(num-90);
}
else if(num>=80)
{
cout<<"Eighty ";
numword1(num-80);
}
else if(num>=70)
{
cout<<"Seventy ";
numword1(num-70);
}
else if(num>=60)
{
cout<<"Sixty ";
numword1(num-60);
}
else if(num>=50)
{
cout<<"Fifty ";
numword1(num-50);
}
else if(num>=40)
{
cout<<"Fourty ";
numword1(num-40);
}
else if(num>=30)
{
cout<<"Thirty ";
numword1(num-30);
}
else if(num>=20)
{
cout<<"Twenty ";
numword1(num-20);
}
else
numword1(num);

return;
}
5) Forever Calender in C++

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

int step1 (int);


void month_name(int);
int no_days(int,int);
int leap(int);

void main()
{
restart:
clrscr();
cout<<"Enter year : ";
unsigned int y,m;
cin>>y;
int x;
x=step1(y);
int month[14][12]= {1,4,4,7,2,5,7,3,6,1,4,6,
2,5,5,1,3,6,1,4,7,2,5,7,
3,6,6,2,4,7,2,5,1,3,6,1,
4,7,7,3,5,1,3,6,2,4,7,2,
5,1,1,4,6,2,4,7,3,5,1,3,
6,2,2,5,7,3,5,1,4,6,2,4,
7,3,3,6,1,4,6,2,5,7,3,5,
1,4,5,1,3,6,1,4,7,2,5,7,
2,5,6,2,4,7,2,5,1,3,6,1,
3,6,7,3,5,1,3,6,2,4,7,2,
4,7,1,4,6,2,4,7,3,5,1,3,
5,1,2,5,7,3,5,1,4,6,2,4,
6,2,3,6,1,4,6,2,5,7,3,5,
7,3,4,7,2,5,7,3,6,1,4,6};
cout<<"Enter month (1 - 12) : ";
month_input:
cin>>m;
if(m<1 || m>12)
{
cout<<"Enter a valid month (1 - 12) : ";
goto month_input;
}
cout<<"

";
month_name(m);
cout<<' '<<y<<"

Mon Tue Wed Thu Fri Sat Sun


";
int j=month[x-1][m-1], days=no_days(m,y);
for (int i=0; i<j-1; i++)
cout<<' ';
for(i=1; i<=days; i++)
{
cout<<i<<' ';
if(j==7)
{
cout<<'
';
j=1;
}
else
j++;
}
cout<<"

Try for another month? (y/n) : ";


char ch;
cin>>ch;
if(ch=='y' || ch=='Y')
goto restart;
}
int step1 (int y)
{
int x=y%7;
x+=(y-1)/4;
x-=(y-1)/100;
x+=(y-1)/400;
x=x%7;
if(x==0)
x=7;
if(leap(y))
x+=7;
return x;
}
void month_name(int m)
{
switch(m)
{
case 1 : cout<<"January";
break;
case 2 : cout<<"February";
break;
case 3 : cout<<"March";
break;
case 4 : cout<<"April";
break;
case 5 : cout<<"May";
break;
case 6 : cout<<"June";
break;
case 7 : cout<<"July";
break;
case 8 : cout<<"August";
break;
case 9 : cout<<"September";
break;
case 10: cout<<"October";
break;
case 11: cout<<"November";
break;
case 12: cout<<"December";
break;
}
}
int no_days(int mon, int y)
{
int d;
switch(mon)
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10:
case 12: d=31;
break;
case 4 :
case 6 :
case 9 :
case 11: d=30;
break;
case 2 : if(leap(y))
d=29;
else
d=28;
break;
}
return d;
}
int leap (int y)
{
if (y%100==0)
if (y%400==0)
return 1;
else
return 0;
else
if (y%4==0)
return 1;
else
return 0;
}
6)Program for Overloading the difference operator for complex
arithmetic

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

class complex
{
float real;
float img;
public:
complex()
{
real = 0; img = 0;
}
complex(float a,float b)
{
real =a ; img = b;
}
complex operator -(complex );
void disp();
};
// Fn.for overloading of - operator for complex arithmetic
complex complex::operator-(complex a)
{
return complex(real-a.real,img-a.img);
}
// function for display of Real & Imaginary Parts
void complex::disp()
{
cout<<"
The real part is : "<<real;
cout<<"
The imaginary part is: "<<img;
}
void main(void)
{
complex c1(12.0,4.5),c2(8,6),c3;
cout<<"
The value of c1 is:
";
c1.disp();
cout<<"
The value of c2 is:
";
c2.disp();
cout<<"
The value of c3 is:
";
c3.disp();
c3=c1-c2;
cout<<"

After c3=c1-c2, c3 is :
";
c3.disp();
getch();
}
7) Program To Know the Day of Birth from Date of Birth

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

main()
{
clrscr();
int d,m,y,year,month,day,i,n;
printf("Enter how many times you want to run this program : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("

Enter the date : ");


scanf("%d%d%d",&d,&m,&y);
if( d>31 || m>12 || (y<1900 || y>=2000) )
{
printf("

INVALID INPUT
");
getch();
exit(0);
}
year = y-1900;
year = year/4;
year = year+y-1900;
switch(m)
{
case 1:
case 10:
month = 1;
break;
case 2:
case 3:
case 11:
month = 4;
break;
case 7:
case 4:
month = 0;
break;
case 5:
month = 2;
break;
case 6:
month = 5;
break;
case 8:
month = 3;
break;
case 9:
case 12:
month = 6;
break;
}
year = year+month;
year = year+d;
day = year%7;
switch(day)
{
case 0:
printf("

Day is SATURDAY
");
break;
case 1:
printf("

Day is SUNDAY
");
break;
case 2:
printf("

Day is MONDAY
");
break;
case 3:
printf("

Day is TUESDAY
");
break;
case 4:
printf("

Day is WEDNESDAY
");
break;
case 5:
printf("

Day is THURSDAY
");
break;
case 6:
printf("

Day is FRIDAY
");
break;
}
}
getch();
return 0;
}
8) Simple Calculator in C++

#include"stdio.h"
#include"conio.h"
#include"dos.h"
#include"stdlib.h"
#include"string.h"

void DISPNUM(char *);


void main()
{
clrscr();

_setcursortype(0);
gotoxy(30,19);
textcolor(GREEN+BLINK);
cprintf("SIMPLE CALCULATOR");
gotoxy(50,21);
textcolor(BLUE+BLINK);
cprintf("Hello World");
getch();
// clrscr();

int x=30,y=10;

textcolor(WHITE);
gotoxy(x,y);
cprintf("7 8 9");
gotoxy(x,y+2);
cprintf("4 5 6");
gotoxy(x,y+4);
cprintf("1 2 3");
gotoxy(x,y+6);
cprintf("0");

textcolor(RED);
gotoxy(x+5,y+6);
cprintf(". =");

textcolor(GREEN);
x=x+15;
gotoxy(x,y);
cprintf("/");

gotoxy(x,y+2);
cprintf("*");

gotoxy(x,y+4);
cprintf("-");

gotoxy(x,y+6);
cprintf("+");

//Draw The For Calc//


x=28;y=5;
gotoxy(x,y);
textcolor(WHITE);

// Ú & ¿ //
cprintf("%c",218);
gotoxy(28+20,y);
cprintf("%c",191);
// Ú & ¿ //

//Horiz. Boundary
for(x=29;x<=28+19;x++)
{
gotoxy(x,y);
cprintf("%c",196);
gotoxy(x,y+12);
cprintf("%c",196);
}
//End of Horiz. Bound

// Ù & À //
cprintf("%c",217);
x=28;y=y+12;
gotoxy(x,y);
cprintf("%c",192);
//End of Ù & À //

//Vertic. Bound.

for(y=6;y<=16;y++)
{
gotoxy(x,y);
cprintf("%c",179);
gotoxy(x+20,y);
cprintf("%c",179);
}
//End of Vertic Bou.

y=6;

for(x=30;x<=30+16;x++)
{
gotoxy(x,y);
cprintf("%c",196);
gotoxy(x,y+2);
cprintf("%c",196);
}

gotoxy(30,y+1);
cprintf("%c %c",179,179);

gotoxy(30,y);
cprintf("%c",218);
gotoxy(30+16,y);
cprintf("%c",191);

gotoxy(30,y+2);
cprintf("%c",192);
gotoxy(30+16,y+2);
cprintf("%c",217);

//End of Vertic Bound.

//OutPut at X=30,Y=8//

char ch;
char operand1[15]="",operand2[15]="",BLANK[15]=" ";
char operator1,first='y';
long double num1=0,num2=0;
int i=0,ERROR=0; //Digits
int MAX=10;
DISPNUM(0);
do
{
ch=getch();
if(ch=='x1b')
{
for(int i=1000;i>=200;i=i-50)
{
sound(i);
delay(100);
}
nosound();
break;
}

//Numeric//
if((ch>='0')&&(ch<='9'))
{
if(i<MAX)
{
if(first=='y')
{
operand1[i]=ch;
DISPNUM(operand1);
i++;
}
else
{
operand2[i]=ch;
DISPNUM(operand2);
i++;
}
}
else //More than 8 digit
{
ERROR=1;
}
}
else if(ch=='.')
{
if(first=='y')
{
if(strchr(operand1,'.')==NULL)
{
operand1[i]=ch;
i++;
}
DISPNUM(operand1);
}
else
{
if(strchr(operand2,'.')==NULL)
{
operand2[i]=ch;
i++;
}
DISPNUM(operand2);
}
}
//Non Numeric
else if (ch=='*')
{
operator1='*';
first='n';
i=0;
}
else if (ch=='/')
{
operator1='/';
first='n';
i=0;
}
else if (ch=='+')
{
operator1='+';
first='n';
i=0;
}
else if (ch=='-')
{
operator1='-';
first='n';
i=0;
}
else if ((ch=='=')||(ch=='
'))
{
//Store in Floating
if(strcmpi(operand1,BLANK)!=0)
{
num1=_atold(operand1);
}
if(strcmpi(operand2,BLANK)!=0)
{
num2=_atold(operand2);
}
//Now Calculate
switch (operator1)
{
case '+':
num1=num1+num2;
break;
case '-':
num1=num1-num2;
break;
case '*':
num1=num1*num2;
break;
case '/':
num1=num1/num2;
break;
}
//ltoa(num1,operand1,10);
gcvt(num1,12,operand1);
DISPNUM(operand1);
i=0;
first='y';

strcpy(operand1,BLANK);
strcpy(operand2,BLANK);
}
else //Invalid Choice
{
ERROR=1;
}
//Beep On ERROR else ------ //
if (ERROR==0)
{
sound(920);
}
else
{
sound(100);
ERROR=0;
}
delay(250);
nosound();

gotoxy(1,1);
cprintf("%d",i);

}while(1);

// clrscr();
gotoxy(30,19);
textcolor(GREEN+BLINK);
cprintf("SIMPLE CALCULATOR");
gotoxy(50,21);
textcolor(BLUE+BLINK);
cprintf("Hello World");
// getch();
}
void DISPNUM(char *num)
{
textbackground(RED);
gotoxy(31,7);
cprintf(" ");
gotoxy(31,7);
cprintf("%s",num);}

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