1.
test.cpp
main.cpp
3.
bank.h
#ifndef bank_H_
#define bank_H_
#include"stdafx.h"
class bank{
private:
char name[40];
char account[20];
double money;
public:
bank();
bank(const char* nm,const char*ac,double mo);
~bank();
void show();
void input(double mo);
void output(double mo);
};
#endif
bank.cpp
#include"stdafx.h"
#include"bank.h"
#include<iostream>
bank::bank()
{
strncpy(name,"no name",39);
name[39]='\0';
strncpy(account,"no account",19);
account[19]='\0';
money=0.0;
}
bank::bank(const char* nm,const char*ac,double mo)
{
strncpy(name,nm,39);
name[39]='\0';
strncpy(account,ac,19);
account[19]='\0';
money=mo;
}
bank::~bank()
{
std::cout<<"Bye";
}
void bank::show()
{
std::cout<<name<<"'s account is : "<<account<<" has "<<money<<std::endl;
}
void bank::input(double mo)
{
money+=mo;
}
void bank::output(double mo)
{
money-=mo;
}
main.cpp
#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
#include "bank.h"
void main()
{
{
bank bk("John Smith","JS0001",5000.0);
bk.show();
bk.input(1200.0);
bk.show();
bk.output(200.0);
bk.show();
}
std::system("pause");
}
2.test.h
#ifndef TEST_H_
#define TEST_H_
#include"stdafx.h"
#include<string>
using namespace std;
class Person{
private:
static const int LIMIT =25;
string lname;
char fname[LIMIT];
public:
Person(){lname="";fname[0]='\0';};
Person(const string & ln,const char *fn="Heyyou");
void Show() const;
void FormalShow() const;
~Person();
};
#endif
test.cpp
#include"stdafx.h"
#include"test.h"
#include<iostream>
#include<string>
Person::Person(const string & ln ,const char *fn)
{
std::cout<<"\n";
std::cout<<"lname is :"<<ln<<" fname is : "<<fn;
lname=ln;
int n=strlen(fn);
for(int i=0;i<n;i++ )
fname[i]=fn[i];
fname[n]='\0';
}
Person::~Person()
{
}
void Person::Show() const
{
std::cout<<std::endl<<"lname: "<<lname<<" fname: " <<fname<<std::endl;
}
void Person::FormalShow() const
{
std::cout<<" fname: " <<fname<<" lname: "<<lname<<std::endl;
}
main.cpp
#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
#include "test.h"
int main()
{
{ Person one;
one.Show();
one.FormalShow();
Person two("Smythecraft");
two.Show();
two.FormalShow();
Person three("Dimwiddy","Sam");
three.Show();
three.FormalShow();
std::system("pause");
}
return 0;
}
3.
test.h
#ifndef TEST_H_
#define TEST_H_
#include"stdafx.h"
#include<string>
using namespace std;
const int Len=40;
class Golf{
private:
char fullname[Len];
int handicap;
public:
Golf();
~Golf();
void setgolf(const char*name,int hc);
int setgolf();
void Handicap(int hc);
void showgolf();
};
#endif
test.cpp
#include"stdafx.h"
#include"test.h"
#include<iostream>
#include<string>
using namespace std;
Golf::Golf()
{
}
Golf::~Golf()
{
}
void Golf::setgolf(const char*name,int hc)
{
strcpy(fullname,name);
handicap=hc;
}
int Golf::setgolf()
{
cout<<"Please enter the name: ";
cin.getline(fullname,40);
if(strlen(fullname)==0)
return 0;
else
cout<<"Please enter the handicap: ";
cin>>handicap;
return 1;
}
void Golf::Handicap(int hc)
{
handicap=hc;
}
void Golf::showgolf()
{
cout<<"the name is : "<<fullname<<endl;
cout<<"the handicap is : "<<handicap<<endl<<endl<<endl;
}
main.cpp
#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
#include "test.h"
using namespace std;
int main()
{
{ Golf ann;
ann.setgolf("Ann Birdfree",24);
ann.showgolf();
ann.Handicap(26);
ann.showgolf();
int i=0;
Golf gf[5] ;
int test=gf[i].setgolf();
while((test==1)&&(i<4))
{
i++;
cin.get();
test=gf[i].setgolf();
}
for(int j=0;j<=i;j++)
{
cout<<"the "<<j+1<<" is : "<<endl;
gf[j].showgolf();
cout<<endl;
}
cout<<"Bye!\n";
std::system("pause");
}
return 0;
}
4.test.h
#ifndef TEST_H_
#define TEST_H_
#include"stdafx.h"
#include<string>
using namespace std;
namespace SALES
{
const int QUARTERS =4;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public :
Sales(const double ar[],int n);
Sales();
void showSales();
};
}
#endif
test.cpp
#include"stdafx.h"
#include"test.h"
#include<iostream>
#include<string>
using namespace std;
namespace SALES
{
Sales::Sales(const double ar[],int n)
{
double sum=0;
max=ar[0];
min=ar[0];
for(int i=0;i<n;i++)
{
sales[i]=ar[i];
sum=sum+sales[i];
if(sales[i]>max)
max=sales[i];
if(sales[i]<min)
min =sales[i];
}
average=sum/n;
/*max=max;
min=min; */
}
Sales::Sales()
{
double sum=0;
for(int i=0;i<4;i++)
{
std::cout<<"Plesae enter the number:";
std::cin>>sales[i];
sum=sum+sales[i];
}
max=sales[0];
min=sales[0];
for(int i=0;i<4;i++)
{
if(sales[i]>max)
max=sales[i];
if(sales[i]<min)
min =sales[i];
}
average=sum/4;
/* max=max;
min=min; */
}
void Sales::showSales()
{
int n =sizeof(sales)/8;
std::cout<<"The sales of Sales is :";
for(int i=0;i<n;i++)
std::cout<<sales[i]<<" ";
std::cout <<std::endl;
std::cout<<"The average of Sales is :"<<average<<std::endl;
std::cout<<"The max of Sales is :"<<max<<std::endl;
std::cout<<"The min of Sales is :"<<min<<std::endl;
}
}
main.cpp
#include "test.h"
using namespace std;
int main()
{
using namespace SALES;
double st[4]={3.08,2.01,10.234,1.097};
Sales s1(st,3);
s1.showSales();
Sales s2;
s2.showSales();
system("pause");
return 0;
}
5.
<pre name="code" class="cpp">Stack.h
#ifndef STACK_H_
#define STACK_H_
struct customer
{
char fullname[35];
double payment;
};
typedef customer Item;
class Stack
{
private:
enum{MAX =10};
Item items [MAX];
int top;
public:
Stack();
bool isempty() const;
bool isfull() const;
bool push(const Item &item);
bool pop(Item &item);
};
#endif
Stack.cpp
Stack::Stack()
{
top=0;
}
bool Stack::isempty()const
{
return top==0;
}
bool Stack::isfull()const
{
return top==MAX;
}
bool Stack::push(const Item &item)
{
if (top<MAX)
{
items[top++] =item;
return true;
}
else
{
return false;
}
}
bool Stack::pop(Item &item)
{
if (top>0)
{
item =items[--top];
return true;
}
else
return false;
}
main.cpp
using namespace std;
int main()
{
Stack st;
customer cs[10]={{"AA",100},{"BB",200},{"CC",300},{"DD",400},{"EE",500},{"FF",600},{"GG",700},{"HH",800},{"II",900},{"JJ",1000}};
for(int i=0;i<=10;i++)
{ if(st.isfull())
{
cout<<"the stack is full!\n";
}
else
{
st.push(cs[i]);
cout<<"push the "<<i<<" customer "<<cs[i].fullname<<"'s payment is "<<cs[i].payment<<endl;
}
}
cout<<"\n\n\n";
int total=0;
for(int i=0;i<=10;i++)
{
if(st.isempty())
{
cout<<"the stack is empty!\n";
}
else
{
st.pop(cs[i]);
total+=cs[i].payment;
cout<<"total="<<total<<endl;
}
}
system("pause");
return 0;
}
6.
Move.h
class Move
{
private:
double x;
double y;
public:
Move (double a=0,double b=0);
void showmove() const;
Move add(const Move &m);
void reset(double a=0,double b=0);
};
Move.cpp
using namespace std;
Move::Move (double a,double b)
{
x=a;
y=b;
}
void Move:: showmove() const
{
cout<<"\nthe current x is :"<<x<<endl;
cout<<"the current y is :"<<y<<endl;
}
Move Move::add(const Move &m)
{
x=x+m.x;
y=y+m.y;
return *this;
}
void Move:: reset(double a,double b)
{
x=a;
y=b;
}
main.cpp
using namespace std;
int main()
{
Move mv(1,2);
mv.showmove();
Move mt(3,4);
mt.showmove();
mv.add(mt);
mv.showmove();
mt.showmove();
mv.reset();
mt.reset();
mv.showmove();
mt.showmove();
system("pause");
return 0;
}
7.
test.h
#ifndef TEST_H_
#define TEST_H_
class Plorg
{
private:
char name[19];
int ci;
public:
Plorg(char *ch="Plorg",int c=0);
void newplorg(char*ch);
void changeplorg(int n);
void showplorg()const;
};
#endif
test.cpp
#include"stdafx.h"
#include"test.h"
#include<iostream>
#include<string>
using namespace std;
Plorg::Plorg(char *ch,int c)
{
strcpy(name,ch);
ci=c;
}
void Plorg::newplorg(char*ch)
{
cout<<"the new name !\n" ;
strcpy(name,ch);
ci=50;
}
void Plorg::changeplorg(int n)
{
cout<<"change the ci !\n" ;
ci=n;
}
void Plorg::showplorg()const
{
cout<<"the Name is :"<<name<<" the CI is : "<<ci<<endl<<endl;
}
main.cpp
#include "stdafx.h"
#include<iostream>
#include <string>
#include<cctype>
#include "test.h"
using namespace std;
int main()
{
Plorg pl("Test",10);
pl.showplorg();
pl.newplorg("ChangeTest");
pl.showplorg();
pl.changeplorg(20);
pl.showplorg();
system("pause");
return 0;
}
10.8