P. 1
Object Oriented Programming in C++ Chapter 2 Answer

Object Oriented Programming in C++ Chapter 2 Answer

|Views: 40|Likes:
Published by Ng Heng Lim
the answer by I on object oriented programming in C++ by Robert Lafore
the answer by I on object oriented programming in C++ by Robert Lafore

More info:

Published by: Ng Heng Lim on Sep 30, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

09/30/2010

pdf

text

original

#include <iostream>

#include <iomanip> //for setw
#include <ctype.h> //for islower()
using namespace std;

class Ch2_1
{
private:float m_fgallons;
public:
void setdata(){
cout<<"please input the quantity of gallons\n";
cin>>m_fgallons;
}
void showfeet(){
cout<<"the number of feet is\n";
float feet=(m_fgallons/7.418);
cout<<feet;
}
};

class Ch2_2
{
public:void showdata()
{
cout<<1990<<setw(10)<<"135\n";
cout<<1991<<setw(10)<<"7290\n";
cout<<1992<<setw(10)<<"11300\n";
cout<<1993<<setw(10)<<"16200\n";
}
};

class Ch2_3
{
private:int var;
public:void ini_var(int d)
{
var = d;
cout<< var<<endl;
var *= 2;
cout<< var<<endl;
--var;
cout<<var<<endl;
}
};

class Ch2_4
{
public:void poem(){cout<<"Candy is dandy\nBut liquor is quicker.";}
};

class Ch2_5
{
private:char data;
public:void test()
{
cout<<"Input a character,Uppercase = 0,while lowercase return non-zero\n";
cin>>data;
cout<<islower(data)<<endl;
}
};

class Ch2_6
{
private:float dollar,pound,franc,deutschemark,yen;
public:
void setrate(float a,float b,float c,float d,float e)
{dollar=a;pound=b;franc=c;deutschemark=d;yen=e;}
void showmoney(){
cout<<"you currently have how much u.s dollar??"<<endl;
cin>>dollar;
cout<<"you currently have $"<<dollar<<"dollar\n"<<"or $";
cout<<dollar*pound<<"pound\n"<<"or $";
cout<<dollar*franc<<"franc\n"<<"or $";
cout<<dollar*deutschemark<<"deutschemark\n"<<"or $";
cout<<dollar*yen<<"yen\n";
}
};

class Ch2_7
{
private:float Celsius,Farenheit;
public:void setandshowdata(){
cout<<"How hot is now(in celsius)?"<<endl;
cin>>Celsius;
Farenheit=Celsius*5/9 + 32;
cout<<"therefore the temperature currently is "<<Farenheit<<" Farenheit";
}
};

class Ch2_8
{
public:void showdata(){
char d='.';
cout<<"Portcity"<<setw(12)<<setfill(d)<<2425785;}
};

class Ch2_9
{
private:int a,b,c,d;char dummychar;float sum;
public:void calcfrac()
{
while(1){
cout<<"Please insert 2 fraction(in a/b form)";
cout<<"1st fraction: ";
cin>>a>>dummychar>>b;
if(dummychar != '/'){cout<<"bad declaration!!";break;}
cout<<"2nd fraction: ";
cin>>c>>dummychar>>d;
if(dummychar != '/'){cout<<"bad declaration!!";break;}
cout<< "the sum is " << (a*d+b*c)<< dummychar <<b*d;
break;}
}
};

class Ch2_10
{
public:void setshowdata()
{
int pound,shilling,pence;
cout<<"Enter pounds:";cin>>pound;
cout<<"Enter shillings:";cin>>shilling;
cout<<"Enter pence:";cin>>pence;
cout<<"Decimal pounds:"<<(float)pound+ (float)shilling/20 +(float)(pence/12)/20;
}
};

class Ch2_11
{
public:void showdata(){

cout<<setiosflags(ios::left)<<setw(12)<<"Last name"<<setw(12)<<"First
name"<<setw(25)<<"Street address"<<setw(10)<<"Town"<<setw(5)<<"state\n"
<<"...............................................................\n"
<<setw(12)<<"Jones"<<setw(12)<< "Bernard"<< setw(30)<<"109 Pine Lane
Littletown"<<setw(2)<<"MI\n"
<<setw(12)<<"OၤBrian"<<setw(12)<<"Coleen"<<setw(30)<<"42 E. 99th Ave.
Bigcity"<<setw(2)<<"NY\n"
<<setw(12)<<"Wong"<<setw(12)<<"Harry"<<setw(30)<<"121-A Alabama St.
Lakeville"<<setw(2)<<"IL\n";
}
};

class Ch2_12
{
float decpounds; // input from user (new-style pounds)
int pounds; // old-style (integer) pounds
float decfrac; // decimal fraction (smaller than 1.0)
float pence;
public:void setshowdata()
{
cout<<"Enter Decimal pounds(eg:12.5543):\n";cin>>decpounds;
pounds = static_cast<int>(decpounds); // remove decimal fraction
decfrac = decpounds - pounds; // regain decimal fraction
pence=decfrac*20;
cout<<"You have "<<pounds<<"."<<pence<<".";
}
};

class chars
{
static const int size=10;
char s[size];
public:
chars():s((char [size]){'a','b','3'}){}
void show() const{
for(int i=0;s[i]>char(0) ;i++){
cout<<"number "<< i <<" is "<<s[i]<<endl;
}
}
};
int main()
{
chars s1;
cout<<"your ";s1.show();
return 0;

}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->