p93.cpp
来自「《C++编程指南(续编)》的一些程序源代码」· C++ 代码 · 共 115 行
CPP
115 行
#include <fstream.h>
#include <string.h>
char * makeCopy(char * pszS)
{
char * pszT=new char[strlen(pszS)+1];
strcpy(pszT,pszS);
return pszT;
}
class Student
{
public:
Student(char * pLN,char * pFN,
int nGrd,unsigned long ulId);
virtual ~Student();
void display(ostream& out)const;
protected:
char * pLName;
char * pFName;
int nGrade;
unsigned long ulID;
};
Student::Student(char * pLN,char * pFN,
int nGrd,unsigned long ulId)
{
pLName=makeCopy(pLN);
pFName=makeCopy(pFN);
nGrade=nGrd;
ulID=ulId;
}
Student::~Student()
{
delete pLName;
delete pFName;
}
void Student::display(ostream& out)const
{
static char szGrade[]="(Grade ";
int nWidth=out.width();
out.width(0);
char cFill=out.fill();
long bleft=out.flags()&ios::left;
out.width(0);
nWidth-=strlen(pLName);
nWidth-=2;
nWidth-=strlen(pFName);
nWidth-=3;
nWidth-=9;
nWidth-=strlen(szGrade);
nWidth-=2;
nWidth-=1;
if(bleft==0)
{
while(nWidth-->0)
{
out<<cFill;
}
}
out<<pLName<<" , "
<<pFName<<" - "
<<ulID
<<szGrade;
out.width(2);
out<<nGrade;
out<<")";
if(bleft!=0)
{
while(nWidth-->0)
{
out<<cFill;
}
}
}
ostream& operator<<(ostream& out,const Student& s)
{
s.display(out);
return out;
}
int main(int,char **)
{
Student s1("Moore","John",-2,7890123456L);
cout<<"Width set to 0:\n";
cout<<"["<<s1<<"]\n";
cout<<"\nWidth set to 80:\n";
cout.fill('*');
cout<<"[";
cout.width(80);
cout<<s1<<"]";
cout<<"\nWidth set to 80 , ios:left bit set :\n";
cout.setf(ios::left);
cout<<"]";
cout.width(80);
cout<<s1<<"]\n";
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?