RSS

Category Archives: Classes and Constructors

PROGRAM TO STORE AND DISPLAY THE DETAILS REGARDING A PUBLISHER

/* THIS PROGRAM CREATES A CLASS PUBLISHER TO STORE AND DISPLAY THE
RELATED DETAILS */

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

class publisher
{
private:
long id;
char title[40];
char author[40];
double price, stockqty;
double stockvalue;
double valcal() const      // stockvalue = price * stockqty;
{
return price * stockqty;
}

public:
publisher()
{
price = 0.0;
stockqty = 0.0;
stockvalue = 0.0;
}
void enter();
void takestock(double);
void sale(double);
void outdata();
~publisher()
{}
};

void publisher :: enter()
{
//read id, title, author
cout<<“Enter the publisher id\n”;
cin>>id;
cout<<“\nEnter the title of book\n”;
gets(title);
cout<<“Enter the author of the book\n”;
gets(author);
cout<<“\nEnter the price\n”;
cin>>price;
}

void publisher::takestock(double n)
{
stockqty += n;
stockvalue = valcal();
}

void publisher::outdata()
{
clrscr();
cout<<“ID\n”<<id;
cout<<“\n\nTitle\n”;
puts(title);
cout<<“\nAuthor\n”;
puts(author);
cout<<“\nPrice\n”<<price;
cout<<“\n\nStock Quantity\n”<<stockqty;
cout<<“\n\nStockvalue\n”<<stockvalue<<endl;
}

void publisher::sale(double n)
{
stockqty -= n;
stockvalue = valcal();
}

void main()
{
clrscr();
publisher book1;
book1.enter();
char wish = ‘y’;
do{
clrscr();
cout<<“Choose any option\n\n\n”;
cout<<“1. Increement stock quantity\n”;
cout<<“\n2.Decreement stock quantity\n”;
cout<<“\n3. Display the details\n\n”;
int choice;
cin>>choice;
switch(choice)
{
case 1: cout<<“\nEnter the amount by which the stock has increased\n”;
double val;
cin>>val;
book1.takestock(val);
break;
case 2: cout<<“\nEnter the amount by which the stock is decreased\n”;
cin>>val;
book1.sale(val);
break;
case 3: cout<<“\nDisplaying the details\n”;
book1.outdata();
break;
}
cout<<“DO ANYTHING ELSE?\n”;
cin>>wish;
}
while(wish == ‘Y’ || wish == ‘y’);
getch();
}

Advertisements
 
Leave a comment

Posted by on July 16, 2015 in Classes and Constructors

 

CLASS TO STORE INFORMATION ABOUT A DIRECTORY

// THIS CLASS STORES INFORMATION ABOUT A DIRECTORY

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

class directory
{
private:
char docunames[50];
double freespace;
double occupied;
public:
void newdocuentry();
double retfreespace();
void showfiles();
};

void directory::newdocuentry()
{
cout<<“Name:\n”;
gets(docunames);
cout<<“\nEnter the total number of bytes available in the directory\n”;
cin>>freespace;
cout<<“\nEnter the total bytes occupied\n”;
cin>>occupied;
}

double directory :: retfreespace()
{
double available = (freespace – occupied)/1024;
return available;
}

void directory :: showfiles()
{
cout<<“\nName of document is\n”;
puts(docunames);
}

void main()
{
clrscr();
directory mydirectory[3];
for(int i=0; i<3; i++)
{
cout<<“Directory”<<i+1<<“\n”;
mydirectory[i].newdocuentry();
clrscr();
}
clrscr();
cout<<“\nThe total number of free kilobytes available in each directory is \n”;
for(i=0; i<3; i++)
{
cout<<“Directory “<<i+1<<” “<<mydirectory[i].retfreespace();
cout<<“\n”;
}
getch();
clrscr();
cout<<“\nDisplaying the list of files\n”;
for(i=0; i<3; i++)
{
cout<<“Directory “<<i+1<<“\n”;
mydirectory[i].showfiles();
cout<<“\n”;
}
getch();
}

 
Leave a comment

Posted by on July 16, 2015 in Classes and Constructors

 

CLASS TO RECORD DETAILS REGARDING A TOUR

// CLASS TO RECORD DETAILS REGARDING A TOUR

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

class tour
{
private:
char tcode[25];
int nadults;
int nkids;
int kilometres;
float totalfare;

public:
tour();
void assignfare();
void entertour();
void showtour();
};

tour::tour()
{
strcpy(tcode,”NULL”);
nadults = 0;
nkids =0;
kilometres = 0;
totalfare = 0.0;
}

void tour::assignfare()
{
if(kilometres >= 1000)
fare = nadults* 500  + nkids*250;
else if(kilometres >= 500 && kilometres <1000)
fare = nadults * 300 + nkids *150;
else if (kilometres < 500)
fare = nadults*200 + nkids*100;
else
fare =0;
}

void tour::entertour()
{
cout<<“Enter the tour code\n”;
gets(tcode);
cout<<“Enter the number of adults\n”;
cin>>nadults;
cout<<“\n\nEnter the number of kids\n”;
cin>>nkids;
cout<<“\n\nEnter the number of kilometres\n”;
cin>>kilometres;
assignfare();
}

void tour::showtour()
{
cout<<“CODE:\t”;
puts(tcode);
cout<<“\nADULTS:\t”<<nadults<<‘\n’;
cout<<“\nKIDS:\t”<<nkids<<‘\n’;
cout<<“\nKMS.:\t”<<kilometres<<‘\n’;
cout<<“\nPRICE:\t”<<totalfare;
}

void main()
{
clrscr();
tour t1;
t1.entertour();
clrscr();
t1.showtour();
getch();
}

 
Leave a comment

Posted by on July 16, 2015 in Classes and Constructors

 

CLASS TO MANAGE A JEANS SHOP

// CLASS TO MANAGE A JEANS SHOP

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

class outfit
{
private:
char ocode[50];
char otype[50];
int osize;
char ofabric[50];
float oprice;
void initprice();

public:
outfit();
void input();
void display();
};

void outfit :: initprice()
{
if(strcmpi(ofabric,”DENIM”) == 0)
{
if(strcmpi(otype,”TROUSER”) == 0)
{
oprice = 1500;
}
else if(strcmpi(otype,”JACKET”) == 0)
{
oprice = 2500;
}
else
{
}
}
else
{
if(strcmpi(otype,”TROUSER”) == 0)
oprice = 1500 – (0.25*1500);
else if(strcmpi(otype,”Jacket”) == 0)
oprice = 2500 – (0.25*2500);
}
}

outfit::outfit()
{
strcpy(ocode,”NOT INITIALISED”);
strcpy(otype,”NOT INITIALISED”);
strcpy(ofabric,”NOT INITIALISED”);
osize = 0;
oprice = 0.0;
}

void outfit::input()
{
cout<<“Enter outfit code\n”;
gets(ocode);
cout<<“Enter outfit type\n”;
gets(otype);
cout<<“Enter outfit fabric\n”;
gets(ofabric);
cout<<“Enter outfit size\n”;
cin>>osize;
initprice();
}

void outfit::display()
{
cout<<“\nCODE:\t”;
puts(ocode);
cout<<“\nTYPE:\t”;
puts(otype);
cout<<“\nSIZE:\t”<<osize<<‘\n’;
cout<<“\nFABRIC:\t”;
puts(ofabric);
cout<<“\nPRICE:\t”<<oprice<<‘\n’;
}

void main()
{
clrscr();
outfit myoutfit;
myoutfit.input();
clrscr();
myoutfit.display();
getch();
}

 
Leave a comment

Posted by on July 16, 2015 in Classes and Constructors

 

CLASS TO FIND THE LARGEST PRICE AS WELL AS THE SUM OF ALL PRICES FOR 10 ITEMS

/* THIS CLASS STORES PRICE LIST OF 10 ITEMS AND PRINTS THE LARGEST AS WELL AS SUM  OF ALL PRICES */

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

class item
{
private:
int icode[10];
float iprice[10];
public:
float largest();
float sum();
void read();
};

float item::largest()
{
float max = iprice[0];
for(int i=0; i<10; i++)
{
if(iprice[i]>max)
max = iprice[i];
}
return max;
}

float item::sum()
{
float sum = 0;
for(int i=0; i<10; i++)
{
sum += iprice[i];
}
return sum;
}

void item::read()
{
for(int i=0; i<10; i++)
{
cout<<“ITEM “<<i+1<<“\n”;
cout<<“ITEM CODE:\n”;
cin>>icode[i];
cout<<“PRICE:\n”;
cin>>iprice[i];
cout<<“\n\n\n\n”;
}
}

void main()
{
clrscr();
item myitem;
myitem.read();
float sum = myitem.sum();
float largest = myitem.largest();
cout<<“\nTHE LARGEST PRICE IS:\t”<<largest;
cout<<“\nTHE SUM OF ALL PRICES IS:\t”<<sum;
getch();
}

 
Leave a comment

Posted by on June 28, 2015 in Classes and Constructors

 

PROGRAM TO ASSIGN STREAM TO A STUDENT BASED ON MARKS

// THIS PROGRAM ASSIGNS STREAM TO A STUDENT BASED IN HIS/HER MARKS

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <iomanip.h>
#include <dos.h>

class student {
private:
char name[40];
long int rollno;
float marks;
char stream[80];
void assignstream();

public:
student()
{
strcpy(name,”NOT ASSIGNED”);
rollno = 0;
marks = 0.0;
strcpy(stream,”NOT ASSIGNED”);
}
void read();
void display();
void compare(long int, student *);
static int count;
};

void student :: compare(long int num, student *stu)
{
for(int i=0; i<100; i++)
{
if(num == stu->rollno)
{
stu->display();
break;
}
stu++;
}

cout<<“NO SUCH STUDENT EXISTS\n”;
}

int student :: count = 0;

void student :: assignstream()
{
if(marks >= 300)
strcpy(stream,”SCIENCE”);
else if(marks >= 250)
strcpy(stream,”COMMERCE”);
else if(marks >= 200)
strcpy(stream,”HUMANITIES”);
else
strcpy(stream,”NULL”);
}

void student :: read()
{
count++;
clrscr();

cout<<“ENTER DATA FOR STUDENT “<<count;

cout<<“\n\nNAME:\t\t”;
gets(name);

cout<<“\nROLL NO:\t”;
cin>>rollno;

cout<<“\nMARKS:\t\t”;
cin>>marks;

cout<<“\n\nDATA SUCCESSFULLY READ. YOU’LL SHORTLY BE ALLOTED STREAM\n”;
cout<<“\n\n\n(Press any key to continue)\t”;
getch();

assignstream();
}

void student ::  display()
{
clrscr();
cout<<setw(50)<<“STUDENT DETAILS\n”;

cout<<“\n\nNAME: \t”;
puts(name);

cout<<“\nROLL NO. “<<rollno;

cout<<“\n\nMARKS: \t”<<marks;

cout<<“\n\nSTREAM:\t”;
puts(stream);

cout<<“\n\n\nDATA SUCESSFULLY DISPLAYED.\n\n\n(Press any key to continue)\t”;
getch();
}

int main()
{
clrscr();
student stu[100];
char wish;
int i=0;
long int rollno = -1;

cout<<“Reading the data of students\n”;

do
{
stu[i].read();
cout<<“\n\nRead Data of More Students\n”;
cin>>wish;
i++;
}
while(wish == ‘Y’ ||  wish == ‘y’);

cout<<“\n\nNumber of students\n”<<student :: count;
delay(2);
clrscr();
cout<<“\nDisplaying the data\n”;
wish = ‘Y’;

while(wish == ‘Y’  ||  wish ==’y’)
{
cout<<“\nEnter the Roll Number of Student whose data you want to view\n”;
cin>>rollno;

student().compare(rollno,stu);

cout<<“\n\nTHAT WAS ALL. Print Data of another Student?”;
cin>>wish;
}

if(wish == ‘N’  ||  wish == ‘n’)
{
cout<<“\n\nTHANKS!\n\n(Press any key to terminate)\t”;
getch();
}
else
{
cout<<“\nWrong input\nTerminating”;
delay(3000);
}
return 0;
}

 
Leave a comment

Posted by on May 18, 2015 in Classes and Constructors

 

PROGRAM TO RECORD AND DISPLAY DETAILS OF A STUDENT

// THIS PROGRAM READS AND PRINTS DETAILS OF A STUDENT

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

int ctr = 0;

class student {
private:
int rno;
char name[30];
int clas;
int marks[5];

protected:
char school[50];

public:
void read()
{
cout<<“\n\nENTER ROLL NUMBER:\t”;
cin>>rno;
cout<<“\nENTER NAME:\t\t”;
gets(name);
cout<<“\nENTER CLASS:\t\t”;
cin>>clas;
cout<<“\nENTER MARKS OF 5 SUBJECTS:\t”;

for(int j=0; j<5; j++)
{   cin>>marks[j];
}

cout<<“\nENTER SCHOOL NAME:\t”;
gets(school);
}

void print(int);

} stu[5];

void student :: print(int id)
{
if(rno == id)
{
cout<<“\n\nNAME:\t\t”;
puts(name);
cout<<“\nCLASS:\t\t”<<clas;
cout<<“\n\nSCHOOL:\t\t”;
puts(school);
cout<<“\nMARKS ARE:\t”;
for(int j=0; j<5; j++)
{
cout<<marks[j]<<‘ ‘;
}
ctr++;
}
}

void main()
{
clrscr();
char wish=’Y’;
ctr=0;

for(int i=0; i<5 && wish == ‘Y’; i++)
{
clrscr();
cout<<“STUDENT “<<i+1;
stu[i].read();
cout<<“CONTINUE?\t”;
cin>>wish;
}
clrscr();
cout<<“PRINTING RECORDS…\n”;
cout<<“\n\nENTER THE ROLL NUMBER OF STUDENT WHOSE RECORD YOU WANT TO READ: \t”;
int rno;
cin>>rno;

for(int k=0; k<5; k++)
{
stu[k].print(rno);
if(ctr == 1)
{
break;
}
}

if(ctr == 0)
cout<<“\nNO SUCH STUDENT EXISTS\n”;

cout<<“\n\nTHANKS “;
getch();
}

 
Leave a comment

Posted by on May 15, 2015 in Classes and Constructors