Monday, November 2, 2009

PROGRAM FOR IMPLEMENTING IO STREAMS IN C++

#include"iostream.h"
#include"conio.h"
#include"math.h"
int main()
{
int count=0;
char c;
char name[20];
clrscr();
cout<< "\n\n-------------Get() & put() Function--------\n\n";
cout<< "INPUT Text\n";
cin.get(c);
while(c!='\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<< "\n\n No. of characters="<< count <<"\n";

cout<<"\n\n-------Getline() & Write Function()-------\n\n";
cout<<"Enter name\n";

cin.getline(name,10);
cout<<"Printing name with Write() size 5 characters \n\n";
cout.write(name,5);

cout<< "\n\n------------Width() Dunction--------\n\n";
cout<< "ITEMS";
cout.width(8);
cout<< "COST";
cout.width(15);
cout<<"Total Value"<<"\n";

cout<<"\n\n-------Precision() Function--------\n";
cout.precision(5);
cout<< sqrt(3) << "\n";

cout<< "\n\n------Fill() Function \n";
cout.fill('$');
cout.width(15);
cout<< "SAI";

getch();
return 0;
}

TEMPLATES FOR CONSTRUCTORS...VECTOR PROGRAM



include
#include
const size=3;
template <>
class vector
{
T *v;
public:
vector()
{
v=new T[size];
for(int i=0;i < i="0;iv[i]*y.v[i];
return sum;
}};
int main()
{
clrscr();
float x[3]={1.1,2.2,3.3};
float y[3]={4.4,5.5,6.6};
vector <> v1;
vector <> v2;
v1=x;
v2=y;
float r=v1*v2;
cout<<"r="<<<"\n";
getch();
return 0;
}

//virtual functions usin c++

#include"iostream.h"
class Base
{
public:
void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class Derived : public Base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n show derived";
}
};
int main()
{
Base B;
Derived D;
Base *bptr;
cout<<"\n bptr points to Base \n";
bptr=&B;
bptr->display(); //calls Base version
bptr->show(); //calls Base version

cout<<"\n\n bptr points to Derived \n";
bptr=&D;
bptr->display(); //calls Base version
bptr->show(); //calls Derived version

return 0;
}

// function overloading usin c++

#include"iostream.h"

//Declarations (prototypes)

int volume(int);
double volume(double, int);
long volume(long, int, int);

int main()
{
cout<< volume(10) <<"\n";
cout<< volume(2.5,8) <<"\n";
cout<< volume(100L,75,15) <<"\n";

return 0;
}

//Function definitions

int volume(int s) //cube
{
return(s*s*s);
}
double volume(double r, int h) //cylinder
{
return(3.14519*r*h);
}
long volume(long l, int b, int h) //rectangular box
{
return(l*b*h);
}

// Virtual Base class

#include"iostream.h"

class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll No:"< }
};

class test:virtual public student
{
protected:
float part1,part2;
public:
void get_marks(float x, float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks obtained:"<<"\n"<<"Part1="< }
};

class sports : public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports wt:"< }
};

class result:public test, public sports
{
float total;
public:
void display(void);
};

void result::display(void)
{
total=part1+part2+score;

put_number();
put_marks();
put_score();

cout<<"Total Score:"<}

int main()
{
result student_1;
student_1.get_number(678);
student_1.get_marks(30.5,25.5);
student_1.get_score(7.0);
student_1.display();
return 0;
}

//Runtime polymorphism c++

#include"iostream.h"
#include"string.h"
#include"conio.h"
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
virtual void display() { } //empty vitual function
};

class book:public media
{
int pages;
public:
book(char *s, float a, int p):media(s,a)
{
pages=p;
}
void display();
};
class tape:public media
{
float time;
public:
tape(char *s, float a, float t):media(s,a)
{
time=t;
}
void display();
};
void book::display()
{
cout<<"\n Title:"< cout<<"\n Pages:"< cout<<"\n Price:"<}
void tape::display()
{
cout<<"\n Title:"< cout<<"\n Play time:"< cout<<"\n price:"<}
int main()
{
char *title=new char[30];
float price, time;
int pages;

clrscr();

//Books details
cout<<"\n Enter Book Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price :";
cin>>price;
cout<<"Pages:";
cin>>pages;

book book1(title,price,pages);

//tape Details
cout<<"\n Enter Tape Details\n";
cout<<"Title:";
cin>>title;
cout<<"Price:";
cin>>price;
cout<<"Play time (mins):";
cin>>time;

tape tape1(title,price,time);

media*list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n Media Details:";
cout<<"\n.........BOOK........";
list[0]->display();

cout<<"\n.....TAPE.....";
list[1]->display();
getch();
return 0;
}

// constructos in Derived class c++


#include"iostream.h"

class alpha
{
int x;
public:
alpha(int i)
{
x=i;
cout<<"alpha initialized \n";
}
void show_x(void)
{
cout<<"x="< }
};

class beta
{
float y;
public:
beta(float j)
{
y=j;
cout<<"beta initialized \n";
}
void show_y(void)
{
cout<<"y="< }
};

class gamma:public beta,public alpha
{
int m,n;
public:
gamma(int a, float b, int c, int d):
alpha(a),beta(b)
{
m=c;
n=d;
cout<<"gamma initialized \n";
}
void show_mn(void)
{
cout<<"m="< }
};

int main()
{
gamma g(5,10.75,20,30);
cout<<"\n";
g.show_x();
g.show_y();
g.show_mn();
return 0;
}

//OverLoading Unary Minus c++


#include"iostream.h"
class space
{
int x;
int y;
int z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator-(); //overloaded unary minus
};

void space::getdata(int a, int b, int c)
{
x=a;
y=b;
z=c;
}
void space::display(void)
{
cout< cout< cout<}
void space:: operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
space S;
S.getdata(10,-20, 30);
cout<<"S:";
S.display();
-S; //activates operator-() function

cout<<"S:";
S.display();
return 0;
}

//static member function usin c++


#include"iostream.h"

class test
{
int code;
static int count; //static member variable

public:

void setcode(void)
{
code=++count;
}
void showcode(void)
{
cout<<"object number:"< }
static void showcount(void) //static member funcion
{
cout<<"count:"< }
};

int test::count;

int main()
{
test t1,t2;
t1.setcode();
t2.setcode();

test::showcount(); //accessing static function

test t3;
t3.setcode();

test::showcount();

t1.showcode();
t2.showcode();
t3.showcode();

return 0;
}

// Objects as Arguments usin c++


#include "iostream.h"
#include"conio.h"

class time
{
int hours;
int minutes;
public:
void gettime(int h, int m)
{
hours=h;
minutes=m;
}
void puttime(void)
{
cout< cout< }

void sum(time,time); //declaration with objects as arguments
};

void time::sum(time t1, time t2) //t1,t2 are objects
{
minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
}

int main()
{
time T1,T2,T3;

T1.gettime(2,45); //get T1

T2.gettime(3,40); //get T2

T3.sum(T1,T2); //T3=T1+T2

cout<<"T1=";
T1.puttime(); //display T1

cout<<"T2=";
T2.puttime(); //display T2

cout<<"T3=";
T3.puttime(); //display T3

getch();

return 0;
}

friend function using c++

#include"iostream.h"

class sample
{
int a;
int b;
public:
void setvalue() {a=50; b=40; }

friend float mean(sample s);
};


float mean(sample s)
{
return float(s.a+s.b)/2.0;
}


int main()
{
sample X; //object X
X.setvalue();

cout<< "Mean value="<
return 0;
}

// OverLoading + Operator c++

#include"iostream.h"
#include"conio.h"
class complex
{
float x; //real part
float y; // imaginarypart
public:
complex(){ } //constructor 1
complex(float real, float imag) //constructor 2
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp; //temporary
temp.x=x+c.x;
temp.y=y+c.y; // these are float additions
return(temp);
}
void complex::display(void)
{
cout<}
int main()
{
complex C1,C2,C3;
clrscr(); //invokes constructor 1
C1=complex(2.5, 3.5);
C2=complex(1.6,2.7); //invokes constructor 2
C3=C1+C2;
cout<<"C1="; C1.display();
cout<<"C2="; C2.display();
cout<<"C3'"; C3.display();
return 0;
}

Arrays of Objects usin c++


#include "iostream.h"
#include"conio.h"

class employee
{
char name[30]; //string as class member
float age;
public:
void getdata(void);
void putdata(void);
};

void employee::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}

void employee::putdata(void)
{
cout<<"Name:"< cout<<"Age:"<
}
const int size=3;

int main()
{
employee manager[size];
clrscr();
for(int i=0;i {
cout<<"\n Details of manager"< manager[i].getdata();
}

cout<<"\n";

for(i=0;i {
cout<<"\n Manager"< manager[i].putdata();
}

getch();

return 0;

}