26.8.15

C++ program that uses a single file

Write a C++ program that uses a single file for both reading and writing the data.

#include<iostream.h>

#include<conio.h>

#include<fstream.h> int main()
{

clrscr();

ofstream outf("ITEM.txt"); cout<<"Enter the filename:"; char name[30];

cin>>name;

outf<<name<<"\n"; cout<<"Enter ITEM cost:"; float cost;

cin>>cost;
outf<<cost;
outf.close();

ifstream inf("ITEM.txt"); inf>>name;
inf>>cost;

cout<<"The name of the item is:"<<name; cout<<"\nItem cost is :"<<cost; inf.close();

getch(); return 0;
}

**************************************OUTPUT****************************

Enter the ITEM name:vicks

Enter ITEM cost:20.3
The name of the item is:vicks

Item cost is :20.299999

Design your own manipulator

Design your own manipulator to provide the following output specification for printing money value:
1)  10 columns width

2)  The character '$' at the beginning

3)  Showing '+' sign.

4)  Two digits precision

5)  Filling of unused spaces with ' * '

6)  Trailing zeros shown


#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

ostream &moneyshow(ostream &output)

{

cout<<'$'; cout.fill('*');

cout.setf(ios::showpos);

cout.setf(ios::showpoint);

cout.width(10);

cout.precision(2); return output;

}

void main()

{

double amount; clrscr();

cout<<"enter the value:"; cin>>amount;

cout<<"\nyour money value:"; cout<<moneyshow<<amount; getch();

}

  


*************************OUTPUT 1***************************************

enter the value:12.2

your money value:$****+12.20

*************************OUTPUT 2***************************************

enter the value:34

your money value:$****+34.00

C++ program to read a list

Write a C++ program to read a list containing item name, item code and cost interactively and display the data in a tabular format as shown below:

NAME  CODE  COST

#include<iostream.h>

#include<conio.h>

#include<iomanip.h> int main()

{

int n,itcode[10]; char item[10][10]; float itcost[10]; clrscr();

cout<<"Enter the no of item:"; cin>>n;
for(int i=0;i<n;i++)

{

cout<<"Enter the item details:"<<i+1; cout<<"\nEnter name:"; cin>>item[i];

cout<”Enter code:"; cin>>itcode[i]; cout<<"Enter cost:"; cin>>itcost[i];

}

cout<<endl<<setw(15)<<"Item name"<<setw(15)<<"item code"<<setw(15)<<"cost"<<endl;


cout<<"\n-------------------------------------
\n";
for(i=0;i<n;i++)

{

cout.fill( ' - ' );

cout.setf(ios::left,ios::adjustfield);

cout.width(15);

cout<<item[i];

cout.setf(ios::left,ios::adjustfield);

cout.width(15);

cout<<itcode[i];

cout.setf(ios::right,ios::adjustfield);

cout<<itcost[i]<<endl;

}

getch();

return 0;

}

***********************************OUTPUT*************************


Enter the no of item:3

Enter the item details:1

Enter name:Rice

Enter code:111

Enter cost:35.4

Enter the item details:2

Enter name:Sugar

Enter code:112

Enter cost:12.3

Enter the item details:3

Enter name:Soap

Enter code:113

Enter cost:60.4

Item name   item code           cost

------------------------------------------------

Rice-----------
111------------
35.400002
Sugar----------
112------------
12.3
Soap-----------
113------------
60.400002

Create a base class called 'SHAPE'

 Create a base class called 'SHAPE' having

-          two data members of type double

-          member function get-data( ) to initialize base class data members

-          pure virtual member function display-area( ) to compute and display the area of

the geometrical object.

Derive two specific classes 'TRIANGLE' and 'RECTANGLE' from the base class. Using these three classes design a program that will accept dimension of a triangle / rectangle interactively and display the area.

#include<iostream.h>

#include<conio.h> class shape

{
protected:double x, y;
public:void getdata(double a, double b)
{
x=a;
y=b;
}
virtual void display_area()=0;
};
class triangle:public shape
{

double triangle_area; void display_area()
{

triangle_area=(1*x*y)/2;
cout<<"area of triangle is:"<<triangle_area<<endl;
}
};
class rectangle:public shape
{
double rectangle_area; void display_area()

{
rectangle_area=x*y;
cout<<"area of rectangle is:"<<rectangle_area;
}
};
int main()
{


clrscr(); shape *p; triangle t; rectangle r;

p=&t; p->getdata(10,30); p->display_area(); p=&r; p->getdata(20,30); p->display_area(); getch();

return 0;
}

*******************************OUTPUT*******************************

area of triangle is:150 area of rectangle is:600


Write a C++ program to illustrate ‘this’ pointer

Write a C++ program to illustrate ‘this’ pointer and pointers to derived classes.


#include<iostream.h>

#include<conio.h> class BC
{

public: int b;

void show()
{
cout<<"b= "<<b<<endl;
}
BC findlarge(BC obj)
{
if(b>obj.b) return *this;

else
return obj;
}
};
class DC:public BC
{
public: int d;

void show()
{
cout<<"b= "<<b<<endl; cout<<"d= "<<d<<endl;

}
};
int main()
{
clrscr(); BC b1,b2; b1.b=10; b2.b=20;

BC Large=b1.findlarge(b2); cout<<"\n Largest is :"; Large.show();

BC *bptr; BC base; bptr=&base; bptr->b=100;

cout<<"Base pointer to base class\n"; bptr->show();
DC derived; bptr=&derived; bptr->b=200;

cout<<"Base pointer to base class\n";
bptr->show(); DC *dptr; dptr=&derived; dptr->d=300;
cout<<"Derived pointer to derived class\n"; dptr->show();
((DC*)bptr)->d=400; cout<<"Type conversion\n"; ((DC*)bptr)->show(); getch();
return 0;

}

**********************OUTPUT********************************

Largest is :b= 20
Base pointer to base class b= 100
Base pointer to base class b= 200
Derived pointer to derived class b= 200
d= 300

Type conversion b= 200
d= 400

Create a 'STRING' class which overloads ‘ = = ' operator

Create a 'STRING' class which overloads ‘ = = ' operator to compare two STRING

objects.

#include<iostream.h>

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

{

char *p; int len;
public:

string(){ } string( char *s)

{

len=strlen(s); p=new char[len+1]; strcpy(p,s);
}
friend int operator = =( string &s,string &t);
};

int operator = = ( string &s1,string &s2)

{
if(strcmp(s1.p,s2.p)= =0) return(1);
else return(0);

}

int main()

{
string s1,s2;

char str1[20],str2[20]; clrscr();

cout<<"\nEnter the first string : "; cin>>str1;
cout<<"\nEnter the second string : "; cin>>str2;
s1=str1;

s2=str2; if(s1= =s2)

cout<<"\nStrings are equal"; else
cout<<"\nStrings are not equal"; getch();
return 0;

}




*******************************OUTPUT 1********************************

Enter the first string : cpc

Enter the second string : cs

Strings are not equal

******************************OUTPUT 2**********************************

Enter the first string : CPC

Enter the second string : CPC

Strings are equal


Illustrate multiple inheritance

Write a c++ program :

b) to illustrate multiple inheritance

#include<iostream.h>

#include<conio.h> class m

{

protected : int m; public:

void getm()

{

cout<<"\nEnter the value for m : "; cin>>m;

}

}; class n

{

protected : int n; public:
void getn()

{

cout<<"\nEnter the value for n : "; cin>>n;
}

};

class p : public m , public n

{ public:

void display()

{

cout<<"\nM="<<m;

cout<<"\nN="<<n;

cout<<"\nM*N="<<m*n;

}


};

int main()

{

clrscr();

p p1;

p1.getm();

p1.getn();

p1.display();

getch();

return 0;

}


************************************OUTPUT******************************

Enter the value for m : 12

Enter the value for n : 10

M=12

N=10

M*N=120