Thursday, 7 March 2013

c program for adding and displaying nodes in link list

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int data ;
struct node *link;

}node;

node *start;

void main()
{
char ch ;
start = '\0';
printf("\nwant to create node?");
ch = getche();
while (ch=='y'||ch=='Y')
{
create();
printf("\nwant to add more y/n");
ch = getche();
}

printf("\nwant to display ?y/n");
ch = getche();
if(ch =='y'||ch =='Y')
{
display();
}
getch();
}


create()
{
node *x,*t;
x=(node*)malloc(sizeof(node));
printf("\nenetr data ");
scanf("%d",&x->data) ;
printf("%d",x->data) ;
if (start =='\0')
{
x->link = '\0' ;
start=x;
}
else
{
t=start;
while(t->link!='\0')
{
t=t->link;
}
t->link=x;
x->link='\0';
}
}

display()
{
node *t;
t=start;
while(t!='\0')
{
printf("\ndata = %d",t->data);
t=t->link;
}
}

constructer overloading (string)

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
int len;
char *str;
public:
string()
{
len=0;
str=new char[len+1];
strcpy(str,"");
}
string(char *ch)
{
len=strlen(ch);
str=new char[len+1];
strcpy(str,ch);
}
void showstring ()
{
cout<<"\nstring is : "<<str;
}
void getlength()
{
cout<<"\nlength of given string : "<<len;
}
void getat(int pos)
{
cout<<"\ncharacter at given position is : "<<str[pos-1];
}
void setupper()
{
strupr(str);
cout<<"\nuppercase string is : "<<str;
}
void setlower()
{
strlwr(str);
cout<<"\nlower case string is : "<<str;
}
};
void main()
{
clrscr();
int x;
char z;
string s,s1("dynamic initialisation of string");
cout<<"\ndefault constructor: ";
s.getlength();
s.showstring();
cout<<"\nparameterized constructor: ";
s1.showstring();
s1.getlength();
cout<<"\nenter the position for finding character : ";cin>>x;
s1.getat(x);
s1.setupper();
s1.setlower();
getch();
}

output:

default constructor:
length of given string : 0
string is :
parameterized constructor:
string is : dynamic initialisation of string
length of given string : 32
enter the position for finding character : 6
character at given position is : i
uppercase string is : DYNAMIC INITIALISATION OF STRING
lower case string is : dynamic initialisation of string

c++ program on contructor overloading (person)

 #include<conio.h>

#include<string.h>

#include<iostream.h>

class person

{

char name[10];

int age,sal;

long double phno;

public:

void getvalue()

{

cout<< "\nenter name of person : ";

cin>>name;

cout<<"\nenter age of person : " ;

cin>>age ;

cout<<"\nenter phone number of person : " ;

cin>>phno;

cout<<"\nenter salary of person : " ;

cin>>sal;

}

void putvalue()

{

cout <<"\nname :" <<name;

cout <<"\nage  :" <<age;

cout <<"\nph no :" <<phno;

cout <<"\nsalary :" <<sal;

}

person(void);

person(char*,int,double,int);

person(person *p1);

};

person :: person(void)

{

strcpy(name,"_____");

age=sal=0;

phno=0;

}

person ::person(char *ch,int a,double p,int s)

{

strcpy(name,ch);

age=a;

phno=p;

sal=s;

}

person ::person(person *p1)

{

strcpy(name,p1->name);

age=p1->age;

phno=p1->phno;

sal=p1->sal;

}

void main()

{

person p2,p3("akash",29,98236589,2000),p4(&p3);

clrscr();

p2.putvalue();

p2.getvalue();

p2.putvalue();

p3.putvalue();

p4.putvalue();

getch();

}

//OUTPUT:

name :_____

age  :0

ph no :0

salary :0

enter name of person : ALOK

enter age of person : 28

enter phone number of person : 2469845

enter salary of person : 2000

name :ALOK

age  :28

ph no :2469845

salary :2000

name :akash

age  :29

ph no :2469845

salary :2000

name :akash

age  :29

ph no :2469845

salary :2000

c++ program on constructor overloading (array)

//program based on constructor overloading
#include<stdio.h>
#include<conio.h>
class array
{
int dim,*arr;
public:
void getvalue(void);
void putvalue(void);
array()
{
dim=0;
arr=new int[dim];
}
array(int n)
{
dim=n;
arr=new int[dim];
}
array(array *a)
{
dim=a->dim;
arr=new int[dim];
}
};
void array::getvalue()
{
cout<<"\n enter array elements: ";
for (int i=0;i<dim;i++)
{
cin>>arr[i];
}
}

void array::putvalue()
{
cout<<"\n array elements:\n ";
for (int i=0;i<dim;i++)
{
cout<<"\n"<<arr[i];
}
}
//constructor overloading default,parameterized,copy constructor
void main()
{
int x;
clrscr();
cout<<"\nenter size of array:\n";cin>>x;
array ar1,ar2(x),ar3(&ar2);
ar1.putvalue();
ar2.getvalue();
ar2.putvalue();
ar3.getvalue();
ar3.putvalue();
getch();
}
//output:

value in default object
 array elements:

enter size of array:
3

 enter array elements:  5 4 8

 array elements:

5
4
8
 enter array elements: 9 5 4

 array elements:

9
5
4

c++ program on function overloading (armstrong)

#include<iostream.h>
#include<conio.h>
class type
{

 int n,n1,n2,o,r;
 public:

 void getvalue()
 {
 cout<<"\nenter any value: ";
 cin>>r;
 n=r;
 }

 void putvalue()
 {
 cout<<"\nenterd number: "<<r;
 }
 void is()
 {
 cout << " is a armstrong number\n";
 }
 void isnot()
 {
 cout <<"is not a armstrong number\n";
 }

 void arms()
 {
 int res;
 getvalue();
 o = n ;
 n1 =n%10;
 n = n/10;
 n2 = n%10;
 n = n/10;
 n = n1*n1*n1 + n2*n2*n2 + n*n*n;

 if (o == n )
 {
    putvalue();
    is();
 }
 else
 {
    putvalue();
    isnot();
 }
    getch();
 }


 void arms(int x)
 {

 o = x;
 n1 =x%10;
 x = x/10;
 n2 = x%10;
 x = x/10;
 x = n1*n1*n1 + n2*n2*n2 + x*x*x;

 if (o == x )
 {
    cout<<"enterd number:" <<o;
    is();
 }
 else
 {
    cout<<"enterd number:" <<o;
    isnot();
 }
    getch();
 }


 void arms(type a)
 {
  a.getvalue();
  a.o = a.n ;
  a.n1 =a.n%10;
  a.n = a.n/10;
  a.n2 = a.n%10;
  a.n = a.n/10;
  a.n = a.n1*a.n1*a.n1 + a.n2*a.n2*a.n2 + a.n*a.n*a.n;

 if (a.o == a.n )
 {
    a.putvalue();
    is();
 }
 else
 {
    a.putvalue();
    isnot();
 }
    getch();
 }

};

void main()
{
type b,c;
int x;
clrscr();
b.arms(c);
b.arms();
cout<<"\nenter any value";
cin>>x;
b.arms(x);
}

c++ program on function overloading (palindrom)

#include<iostream.h>
#include<conio.h>
class type
{

 int n,n3,n1,n2,o,r;
 public:

 void getvalue()
 {
 cout<<"\nenter any value: ";
 cin>>r;
 n=r;
 }

 void putvalue()
 {
 cout<<"\nenterd number: "<< r;
 }
 void is()
 {
 cout << " is a palindrom number\n";
 }
 void isnot()
 {
 cout <<"is not a palindrom number\n";
 }

 void pal()
 {
 int res;
 getvalue();
 o = n ;
 n1 =n%10;
 n = n/10;
 n2 = n%10;
 n = n/10;
 res = n1*100 + n2*10 + n;

 if (o == res )
 {
    putvalue();
    is();
 }
 else
 {
    putvalue();
    isnot();
 }
    getch();
 }


 void pal(int x)
 {

 o = x;
 n1 =x%10;
 x = x/10;
 n2 = x%10;
 x = x/10;
 x = n1*100 + n2*10 + x;

 if (o == x )
 {
    cout <<"enterd value : "<< o ;
    is();
 }
 else
 {

    cout <<"enterd value : "<< o ;
    isnot();
 }
    getch();
 }


 void pal(type a)
 {
  a.getvalue();
  a.o = a.n ;
  a.n1 =a.n%10;
  a.n = a.n/10;
  a.n2 = a.n%10;
  a.n = a.n/10;
  a.n = a.n1*100 + a.n2*10 + a.n;

 if (a.o == a.n )
 {
    a.putvalue();
    is();
 }
 else
 {
    a.putvalue();
    isnot();
 }
    getch();
 }

};

void main()
{
type b,c;
int x;
clrscr();
b.pal(c);
b.pal();
cout<<"\nenter any value";
cin>>x;
b.pal(x);
}

c++ program on function overloading (simple)

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

class ovrlod
{
public :
int volume(int);
double volume(double,int);
long volume(long,int,int);
};

int ovrlod::volume(int s)
{
return (s*s*s) ;
}

double ovrlod :: volume (double r,int h)
{
return(3.154*r*h);
}

long ovrlod :: volume(long l,int b,int h)
{
return(l*b*h)  ;
}

void main()
{
ovrlod x;
cout<<"volume of cube =" <<x.volume(10) ;
cout<<"volume of cylinder ="<< x.volume(2.5,8);
cout<<"volume of rectangle ="<< x.volume(100,75,15);
getch();
}